w3resource

C Programming: Split string by space into words

C String: Exercise-31 with Solution

Write a program in C to split strings by space into words.

C Programming: Split string by space into words

Sample Solution:

C Code:

#include <stdio.h>
#include <string.h>

int main() {
    char str1[100];         // String input
    char newString[10][10]; // 2D array to store split strings
    int i, j, ctr;          // Counters and index variables

    printf("\n\n Split string by space into words :\n");
    printf("---------------------------------------\n");

    printf(" Input a string : ");
    fgets(str1, sizeof str1, stdin); // Read input string

    j = 0;    // Initialize column index of newString array
    ctr = 0;  // Initialize word count

    // Loop through each character in the input string
    for (i = 0; i <= (strlen(str1)); i++) {
        // If space or NULL found, assign NULL into newString[ctr]
        if (str1[i] == ' ' || str1[i] == '\0') {
            newString[ctr][j] = '\0'; // Null-terminate the word
            ctr++;  // Move to the next word
            j = 0;  // Reset column index to 0 for the next word
        } else {
            newString[ctr][j] = str1[i]; // Store the character into newString
            j++;  // Move to the next character within the word
        }
    }

    printf("\n Strings or words after split by space are :\n");
    // Display the split words stored in newString array
    for (i = 0; i < ctr; i++) {
        printf(" %s\n", newString[i]);
    }

    return 0;
}

Sample Output:

 Split string by space into words :                                                                                           
---------------------------------------                                                                                       
 Input  a string : this is a test string                                                                                   
                                                                                                                              
 Strings or words after split by space are :                                                                                  
 this                                                                                                                         
 is                                                                                                                           
 a                                                                                                                            
 test                                                                                                                         
 string 

Flowchart :

Flowchart: Split string by space into words

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

Previous: Write a program in C to check whether a character is digit or not.
Next: Write a C programming to find the repeated character in a given string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.