Case 1 : Enter string : Well begun is half done. Resultant string : Well%begun%is%half%done. Case 2 : Enter string : Greece Resultant string : Greece Case 3 : Enter string :12 23 34 45 Resultant string : 12%23%34%45
Users Online
· Guests Online: 126
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Newest Threads
No Threads created
Hottest Threads
No Threads created
Latest Articles
Articles Hierarchy
C++ Program to Replace All Spaces in a String with %
C++ Program to Replace All Spaces in a String with %
This is a C++ Program to Replace all Spaces in a String with %.
Problem Description
The program takes a string and replaces all spaces in it with %.
Problem Solution
1. The program takes a string.
2. The string is checked for spaces and replaced with %.
3. The result is printed.
4. Exit.
C++ Program/Source code
Here is the source code of C++ Program to Replace all Spaces in a String with %. The program output is shown below.
-
#include<iostream>
-
#include<string.h>
-
using namespace std;
-
int main ()
-
{
-
int i;
-
char str[50];
-
cout << "Enter string : ";
-
gets(str);
-
for (i = 0; str[i] !='\0'; i++)
-
{
-
if (str[i] == ' ')
-
str[i]='%';
-
}
-
cout << "Resultant string : " << str;
-
return 0;
-
}
Program Explanation
1. The program takes a string and stores it in the array ‘str’.
2. Using a for loop, the string is checked for spaces and replaced with ‘%’.
3. The result is then printed.
Runtime Test Cases
Comments
No Comments have been Posted.
Post Comment
Please Login to Post a Comment.