w3resource

C Programming: Split string by space into words


31. Split String by Spaces

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;
}

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


For more Practice: Solve these Related Problems:

  • Write a C program to split an input string into words using a manual parsing approach without strtok().
  • Write a C program to separate words in a sentence by detecting spaces and storing them in an array.
  • Write a C program to split a string into tokens and then print each token on a new line.
  • Write a C program to split a string by spaces and then sort the resulting words alphabetically.

Go to:


PREV : Check Digit Character.
NEXT : Find Repeated Character.

C Programming Code Editor:



Improve this sample solution and post your code through Disqus.

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.