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?��