-1

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?��
Jayaram18
  • 9
  • 1
  • 8
  • 1
    you can use the inbuilt ```strtok()```function to split the string. refer here for the [reference](https://stackoverflow.com/questions/3889992/how-does-strtok-split-the-string-into-tokens-in-c) – gretal Mar 16 '22 at 06:46
  • Please focus your question on the specific problems encountered while trying yourself. For that please show a [mre] of the best attempt, the closest you got. – Yunnosch Mar 16 '22 at 07:29
  • Look at each character of your string, if it is a comma, increase a counter by one and if the counter reaches the number `4`, insert/print a newline, set the counter to `0` and continue looking. Alternatively use `strchr` (or `strcspn` for a more sophisticated search). – Erdal Küçük Mar 16 '22 at 07:50

2 Answers2

0

you can create a function which will split with comma and then slice from 0 index to 4 index. With this you can create a div and append them anywhere. Here is the code:

const comaSplit = () => {
    const text = document.getElementById('main').innerText
    const array = text.split(',')
    let init = 0
    for (i = 0; i < array.length; i += 4) {
        const first = array.slice(init, (init + 4))
        const string = first.join(',')
        init = init + 4
        const div = document.createElement('div')
        div.innerText = string
        document.getElementById('root').appendChild(div)
    }
}
comaSplit()
0
#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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Jayaram18
  • 9
  • 1
  • 8
  • I've answered only this question. I've verified the output as well and any reviewers are welcome to test this in their local machine. Why am I not able to post any answers and I'm revoked of my answering privilege. Please help me with this issue. Any help is appreciated. – Jayaram18 Jun 01 '22 at 15:39