Thursday, September 19, 2019

Search and Replace a String in C++


Write a program that will ask the user to give a sentence and then the program will ask the user what word in the given sentence to be replaced and the program will ask the user again what is the replacement word. After which the program will search and replace the word from the sentence and display the new sentence on the screen.

I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me in the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.

My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.

Here in Bacolod I also accepting computer repair, networking and Arduino Project development at a very affordable price.

My personal website is http://www.jakerpomperada.com.


Sample Program Output


Program Listing

 search.cpp

// search.cpp
// Author    : Mr. Jake R. Pomperada, BSCS,MAED-IT
// Date      : September 2, 2018   Sunday  7:06 PM
// Location  : Bacolod City, Negros Occidental Philippines.
// Website   : http://www.jakerpomperada.com
// Email     : jakerpomperada@jakerpomperada.com and jakerpomperada@gmail.com


#include <iostream>
#include <string.h>

using namespace std;


int main()
{
   char str[100],s[100],r[100],result[100];
   int a=0,b=0,c=0,m=0,k=0;

   system("COLOR F0");  
   cout << "\n\n";
    cout << "\tSearch and Replace a String";
    cout << "\n\n";
    cout << "\tGive a String : ";
    gets(str);
    cout <<"\n";
    cout << "\tGive the string to be search : ";
    gets(s);
    cout <<"\n";
    cout <<"\tEnter replace string : ";
    gets(r);
a = m = c = b = 0;
while ( str[c] != '\0')
{
if (str[m] == s[a] ) 
{
a++;
m++;
if ( s[a] == '\0') 
{
for(k=0; r[k] != '\0';k++,b++)
result[b] = r[k];
a=0;
c=m;
}
}
else 
{
result[b] = str[c];
b++;
c++;
m = c;
a=0;
}
}
    result[b] = '\0';
cout <<"\n\n";
cout <<"\tDISPLAY RESULT";
cout <<"\n\n";
cout << "\tThe result string :=> " << result;
cout <<"\n\n";
return 0;
} // End of Program


No comments:

Post a Comment