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
[wpusb]
Also Prepare Below Important Question
- Sum of digits of Given Number in Java
- Hibernate Interview Questions for 2+ years of experience
- 68 Most Important Microservices Interview Questions
- 60 Most Important Git Interview Questions
- 50+ Mostly asked Java Interview Questions for 6 Years Exp.
- 60+ Mostly Asked Java Interview Questions for 4 Years Exp
- 60+ Mostly asked Java Interview Question for 3+ Years
- 60+ Spring Boot interview questions for 4+ years Exp.
- 60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
- Scenario Based Java 8 Coding Interview Questions (For Experienced)
- Python Program to add two numbers without addition operator
- Mostly Asked Java Interview Questions For 2 Yrs Experience
- Find All Pairs in Array whose Sum is Equal to given number Using Java
- Java Program to find GCD of two Numbers using Recursion
- Python Program to Separate Characters in a Given String
- 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
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