Issue
I’m working on a string formatting problem where I’m trying to display the string to the specified size. I need to split the string after 4 commas and display the string in the next line.
INPUT:
char *str = "0-2025,0-2024,0-2023,0-2022,0-2021,0-2020,0-2019,0-2018,0-2017,0-2016";
EXPECTED OUTPUT:
0-2025,0-2024,0-2023,0-2022
0-2021,0-2020,0-2019,0-2018
0-2017,0-2016
I’m trying to add a special character ‘*’ so that I can tokenize and use if for display.
My approach and code is given below, but I’m facing some issue when special character is added at the end.
#include <stdio.h>
#include <string.h>
int nthChar(char *str, char ch, int N){
int occur = 0;
for (int i = 0; i < strlen(str); i++) {
if (str[i] == ch) {
occur += 1;
}
if (occur == N)
return i;
}
}
int main()
{
char *str = "0-2025,0-2024,0-2023,0-2022,0-2021,0-2020,0-2019,0-2018,0-2017,0-2016,0-2015,0-2014,0-2013,0-2012,0-2011,0-2010";
char ch = ',';
int N = 4, res;
res = nthChar(str,ch,N);
printf("%d\n", res);
char priority[128];
strcpy(priority,str);
int size=strlen(str);
printf("size = %d\n",size);
int d = size/res;
printf("DIV =%d\n",d);
printf("%s\n", priority);
for(int i=0;i<strlen(str)&&res<strlen(str);i++){
for(int j=1,k=0;j<=d,k<d;j++,k++){
priority[res*j+k] = '*';
}
}
printf("%s", priority);
return 0;
}
Current Output :
27
size = 111
DIV =4
0-2025,0-2024,0-2023,0-2022,0-2021,0-2020,0-2019,0-2018,0-2017,0-2016,0-2015,0-2014,0-2013,0-2012,0-2011,0-2010
0-2025,0-2024,0-2023,0-2022*0-2021,0-2020,0-2019,0-2018*0-2017,0-2016,0-2015,0-2014*0-2013,0-2012,0-2011,0-2010*�O?��
Solution
#include <stdio.h>
#include <string.h>
int nthChar(char *str, char ch, int N){
int occur = 0;
for (int i = 0; i < strlen(str); i++) {
if (str[i] == ch) {
occur += 1;
}
if (occur == N)
return i;
}
}
int main()
{
char *str = "0-2025,0-2024,0-2023,0-2022,0-2021,0-2020,0-2019,0-2018,0-2017,0-2016,0-2015,0-2014,0-2013,0-2012,0-2011,0-2010";
char ch = ',';
int N = 4, res;
res = nthChar(str,ch,N);
char priority[128];
strcpy(priority,str);
int size=strlen(str);
//printf("size = %d\n",size);
int d = size/res;
//printf("DIV =%d\n",d);
//printf("%s\n", priority);
for(int j=1,k=0;j<=d,k<d && res<strlen(str);j++,k++){
if((res*j+k)==size){
//printf("End of the string check\n");
priority[res*j+k] = '\0';
}else {
//printf("index =%d\n",(res*j+k));
priority[res*j+k] = '*';
}
}
char* token = strtok(priority, "*");
while (token != NULL) {
printf("%s\n", token);
token = strtok(NULL, "*");
}
return 0;
}
OUTPUT:
0-2025,0-2024,0-2023,0-2022
0-2021,0-2020,0-2019,0-2018
0-2017,0-2016,0-2015,0-2014
0-2013,0-2012,0-2011,0-2010
I’ve tried an approach to find nth comma and then replace that with a distinct special character to tokenize it later for display purpose.
By reading blogs and more about char array I understood that the last character of the string should be ‘\0’ which is by default handled.
I’ve tried a approach of changing every 4th comma to a special character and then tokenizing with that to display the output.
Answered By – Jayaram18
Answer Checked By – Robin (BugsFixing Admin)