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<string.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;
}
}
}
int main() {
char str[100],ch;
int i,j,len;
printf("Enter the string : \n");
fgets(str, 100, stdin);
printf("Enter a char you want to replace with space : ");
scanf("%c",&ch);
replaceSpace(str,ch);
printf("String after removing ' ' String Become: \n %s", str);
return 0;
}
Output :
Enter the string :
Quescol is website
Enter a char you want to replace with space : T
String after removing ' ' String Become:
QuescolTisTwebsite
Also Prepare Below Important Question
- Python Program to add two number using Recursion
- Python Program to Find Highest Frequency Element in Array
- Python Program to Merge two Arrays
- Perform left rotation by two positions in Array Using Python
- Python Program to Delete Element at Given Index in Array
- Python Program to Delete element at End of Array
- Python Program to Copy one String to Another String
- Python Program to Remove Repeated Character from String
- Print highest frequency Character in String in Python
- Python Program to Convert lowercase vowel to uppercase
- Convert Celsius to Fahrenheit in Python
- Leap Year Program in Python
- Python Program to convert Decimal to Octal number
- Python Program to Convert Decimal Number into Binary
- Find all Pairs Whose Sum is Equal to Given number in Python
- Python Program to Replace First Vowel With ‘-‘ in String
- Python Program to find Prime factors of given integer
- Python Program to Print Prime Number in given range
- Java Program to Perform Left Rotation on Array Elements by Two
- Java Program to Perform Right Rotation on Array Elements by Two
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