C program to remove spaces in a sentence using string concepts.

In C, removing spaces from a string involves iterating through the string and eliminating space characters. This can be accomplished using string manipulation techniques where we shift characters to overwrite spaces.

Syntax

while(string[i] != '\0') {
    if(string[i] == ' ') {
        // Shift characters left to remove space
    }
    i++;
}

String Declaration and Initialization

An array of characters is called a string. Given below is the declaration of a string āˆ’

char stringname[size];

For example, char string[50]; declares a string of length 50 characters.

Initialization Methods

  • Using single character constants:
char string[10] = {'H', 'e', 'l', 'l', 'o', '\0'};
  • Using string constants:
char string[10] = "Hello";

Method 1: Character Shifting Approach

This method shifts all characters to the left when a space is found āˆ’

Note: This example uses fgets() instead of the deprecated gets() function for safe input.
#include <stdio.h>
#include <string.h>

int main() {
    char string[100];
    int i = 0, j;
    
    printf("Enter any statement: ");
    fgets(string, sizeof(string), stdin);
    
    /* Remove newline character from fgets */
    string[strcspn(string, "<br>")] = '\0';
    
    while(string[i] != '\0') {
        if(string[i] == ' ') {
            j = i;
            while(string[j] != '\0') {
                string[j] = string[j + 1];
                j++;
            }
        } else {
            i++;
        }
    }
    
    printf("Sentence without spaces: %s<br>", string);
    return 0;
}
Enter any statement: Tutorials Point C Programming
Sentence without spaces: TutorialsPointCProgramming

Method 2: Two-Pointer Approach

This method uses two indices to copy non-space characters efficiently āˆ’

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

int main() {
    char string[100];
    int i = 0, j = 0;
    
    printf("Enter any statement: ");
    fgets(string, sizeof(string), stdin);
    
    /* Remove newline character from fgets */
    string[strcspn(string, "<br>")] = '\0';
    
    while(string[i] != '\0') {
        if(string[i] != ' ') {
            string[j] = string[i];
            j++;
        }
        i++;
    }
    string[j] = '\0';  /* Null terminate the result */
    
    printf("Sentence without spaces: %s<br>", string);
    return 0;
}
Enter any statement: Welcome to the world of tutorials
Sentence without spaces: Welcometotheworldoftutorials

Key Points

  • Method 1 shifts characters repeatedly, resulting in O(n²) time complexity in worst case.
  • Method 2 uses two pointers with O(n) time complexity, making it more efficient.
  • Always use fgets() instead of gets() for secure input handling.
  • The control string "%s" is used for accessing strings until it encounters '\0'.

Conclusion

Removing spaces from strings in C can be accomplished through character manipulation. The two-pointer approach is more efficient than character shifting, especially for longer strings with multiple spaces.

Updated on: 2026-03-15T13:57:53+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements