C program to replace the string space with a given character

In this tutorial we will learn writing the C program to replace the spaces from the string with given character. This program is little bit logical. Here we have to find the available space in the input string and then replace that space with the given character by user.

How string space replacement with character program work?

  • This program will take a String as an input. Also program will take one character to replace with space.
  • Now we will find the spaces available in the string using if check. And if space found then replace with given character.
  • String is an array of character, so we can easily iterate the string using for loop and compare with the space. And if space found then replace with this index with the character given as an input by the user.

C Program to replace space of String with user given character

void replaceSpace(char * str, char charToReplace){
    char c;
    int i;
    int len = strlen(str);
    for(i=0; i<len; i++)
        if(str[i] == ' ')
            str[i] = charToReplace;


void main(){
	 char str[100],ch,c;
	 int i,j,len;
	 printf("enter the string : \n");
	 printf("enter a char you want to replace with space : ");
	 printf("String after removing ' ': %s", str);
	 return 0;


C program to replace the string space with a given character

Share on

Also Prepare Below Important Question

Interview Questions Categories

C Programming Interview Preparation

Core Java Programming Interview Preparation

Python Programming Interview Preparation