0

I found this c programming code on https://www.includehelp.com/c-programs/c-program-to-split-string-by-space-into-words.aspx

/*C program to split string by space into words.*/
#include <stdio.h>
#include <string.h>

int main()
{
    char str[100];
    char splitStrings[10][10]; //can store 10 words of 10 characters
    int i,j,cnt;

    printf("Enter a string: ");
    gets(str);

    j=0; cnt=0;
    for(i=0;i<=(strlen(str));i++)
    {
        // if space or NULL found, assign NULL into splitStrings[cnt]
        if(str[i]==' '||str[i]=='\0')
        {
            splitStrings[cnt][j]='\0';
            cnt++;  //for next word
            j=0;    //for next word, init index to 0
        }
        else
        {
            splitStrings[cnt][j]=str[i];
            j++;
        }
    }
    printf("\nOriginal String is: %s",str);
    printf("\nStrings (words) after split by space:\n");
    for(i=0;i < cnt;i++)
        printf("%s\n",splitStrings[i]);
    return 0;
}

I had run this code and I have entered the string for an example "The dog is sad" The output will be

The Dog Is Sad

I was wondering if I could count the numbers of the characters after splitting the word; like for an example;

The 3 Dog 3 Is 3 Sad 3

I do not know how to achieve that desired output. Thank you

fulniz
  • 25
  • 3

2 Answers2

0

Your last printf could be:

printf("%s %d ",splitStrings[i], strlen(splitStrings[i]));

You already use strlen() in your first for loop.

donjuedo
  • 2,475
  • 18
  • 28
  • thank you! I got it. But I understand that "Use fgets(buffer, n, stdin) to read a line of text that includes spaces, where buffer is the array into which you want to place the characters and n is the maximum number of characters to read." If so, How can I apply fgets for this type of code for the same desired output? – fulniz Oct 27 '19 at 15:44
0

Use this modified printf:

/*C program to split string by space into words.*/
    #include <stdio.h>
    #include <string.h>

    int main()
    {
        char str[100];
        char splitStrings[10][10]; //can store 10 words of 10 characters
        int i,j,cnt;

        printf("Enter a string: ");
        gets(str);

        j=0; cnt=0;
        for(i=0;i<=(strlen(str));i++)
        {
            // if space or NULL found, assign NULL into splitStrings[cnt]
            if(str[i]==' '||str[i]=='\0')
            {
                splitStrings[cnt][j]='\0';
                cnt++;  //for next word
                j=0;    //for next word, init index to 0
            }
            else
            {
                splitStrings[cnt][j]=str[i];
                j++;
            }
        }
        printf("\nOriginal String is: %s",str);
        printf("\nStrings (words) after split by space:\n");
        for(i=0;i < cnt;i++)
            printf("%s %d ",splitStrings[i],strlen(splitStrings[i]));//modified
        return 0;
    }
aksr
  • 322
  • 1
  • 2
  • 11
  • Thank you for help! I would like to know if it's possible that if I were to use fgets for this type of code for the same desired output? I understand that "Use fgets(buffer, n, stdin) to read a line of text that includes spaces, where buffer is the array into which you want to place the characters and n is the maximum number of characters to read." If so, – fulniz Oct 27 '19 at 15:45
  • Yes you can use fgets() (it has buffer overflow protection) to get your input into a string. – aksr Oct 27 '19 at 15:49
  • Or you can use scanf like this : scanf("%[^\n]", str); – aksr Oct 27 '19 at 15:50
  • For more visit this link : https://stackoverflow.com/questions/1247989/how-do-you-allow-spaces-to-be-entered-using-scanf – aksr Oct 27 '19 at 15:51