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
#include<stdio.h> #include<ctype.h> 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"); scanf("%[^\n]%c",str,&c); printf("enter a char you want to replace with space : "); scanf("%c",&ch); replaceSpace(str,ch); printf("String after removing ' ': %s", str); return 0; }
Output

Share on
Also Prepare Below Important Question
- Java Program to Perform Left Rotation on Array Elements by Two
- Java Program to Perform Right Rotation on Array Elements by Two
- Java Program to Print Odd Numbers from Array
- Java Program to Print All Even Numbers in Array
- Java Program to Find the Sum of Array Elements
- Java Program to Delete Element of Array At Given Location
- Java Program to Delete a given Element of Array
- Java Program to Delete Element at End of Array
- Java Program to Insert Element in Array at given Location
- Java Program to Insert Element At the End of Array
- Java Program to Print Length of an Array
- Reverse Array without using Second Array or inplace Reversal Java Program
- Java Program to Print Array in Reverse Order
- Java Program to Sort String Character in Descending order
- Java Program to Sort String in Ascending Order
- Java Program to Print non Repeating Characters in String
- Java Program to Find Sum of Integers in the String
- Java Program to Remove Duplicates From String
- Java Program to Concatenate two Strings
- Java Program to Check if two Strings are Same
Interview Questions Categories
C Programming Interview Preparation
Core Java Programming Interview Preparation
- Core Java Programming Coding Questions
- Core Java Pattern Programming Questions
- Core Java Programming Interview Questions