Home > Mobile >  Restrict string size and display the rest of the string in next line
Restrict string size and display the rest of the string in next line

Time:03-16

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

CodePudding user response:

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()

CodePudding user response:

#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

yunnosch can you please check if this example is sufficient ?

Erdal 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.

UPDATE : 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. Any other efficient approach is very much appreciated.

Thank You.

  • Related