To replace a word by another word in a given string

#include<stdio.h>
#include<conio.h>
int stlen(char str[50])
{
int len = 0;
while(str[len]!=’\0′)
len++;
len–;
return len;
}
void stcat(char str1[50], char str2[50])
{
int i = 0,len = 0;
while(str1[len]!=’\0′)
len++;
while(str2[i]!=’\0′)
{
str1[len] = str2[i];
i++;
len++;
}
str1[len] = ‘\0’;
}
void main()
{
char str1[50], str2[50], str3[50], temp[50];
int len1, len2, len3, i, j, match, k;
clrscr();
printf(“\n\n\t ENTER A SENTENCE…: “);
gets(str1);
len1 = stlen(str1);
printf(“\n\n\t ENTER A STRING WHICH YOU WANT TO DELETE…: “);
gets(str2);
len2 = stlen(str2);
printf(“\n\n\t ENTER A NEW STRING WHICH YOU WANT TO INSERT …: “);
gets(str3);
len3 = stlen(str3);
for(i=0;i<=len1;i++)
{
match = 1;
for(j=0;j<=len2;j++)
if(str2[j]!=str1[i+j])
{
match = 0;
break;
}
if(match)
{
for(k=0,j=i+len2+1;j<=len1;j++,k++)
temp[k] = str1[j];
temp[k] = ‘\0’;
for(j=0;j<=len3;j++)
str1[i+j] = str3[j];
str1[i+j] = ‘\0’;
stcat(str1,temp);
len1 = len1 – len2 +len3;
i = i + j;
}
}
printf(“\n\n\t OUTPUT IS…: “);
puts(str1);
getch();
}
Download exe and source code here.

Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

10 thoughts on “To replace a word by another word in a given string

  1. sir,
    i need c program to delete substing like following string1=”concatenate”,string2=”cat”
    string2 ll be deleted from string1 so result should be string1=”conenate”.can u give me the coding for thos sir…

  2. THIS PROGRAMMING CODE IS MORE EASIER THAN THIS ONE…
    TRY IT GUYS… (^_^)

    #include
    #include
    #include

    #include
    using namespace std;

    int main()
    {

    string str;

    vector a;
    vector::iterator it;

    char word[40];

    cout<<"Please enter something :"<<endl;

    gets(word);

    int pos;

    int length = strlen(word);

    // copy the sentence into a vector
    for(int i=0;i<length;i++)
    {
    str.push_back(word[i]);

    if(word[i] == ' ' || i == length – 1 )
    {
    a.push_back(str);
    str.clear();
    continue;
    }

    }

    cout<<"Please enter another string: "<>str;

    cout<<"Enter the position to replace the word:"<>pos;

    it = a.begin();

    it = a.erase(it+pos);

    it = a.begin();

    a.insert(it+pos,str);

    for(it = a.begin();it <a.end();it++)
    {
    cout<<*it<<" ";
    }

    cout<<endl;

    system("pause");

    }

    A VERY EASY CODE…

  3. Hey faras raoof…Thanx for ur program. But There is something missing.
    I write: A quick brown fox
    // and replace fox with dog
    The output comes like: A quick brown dog ——–fox

    Why is this: ———-fox visible? :/ what should i decrement in order to get that array in place? Is this how u designed the program or its d garbage value which fits into the 2d array? plz help

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in