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.


11 Responses to “To replace a word by another word in a given string”

  1. Profile photo of soha sheikh

    mujhe koi ye program smjha do,means every step kaise kaise ho rha ek array box example me le k plz plz!
    Q-wap input a string then find out occurence of each character?
    void main()
    {
    int i ,j,k,c;
    printf(“enter a string”);
    gets(a);
    for(i=0;i<strlen(a);i++)
    {
    c=1;
    for(j=i+1;j<strlen(a);j++)
    {
    c++;
    for(k=j;k<strlen(a);k++)
    a[k]=a[k+1];
    j–;
    }
    }
    printf("\n%c=%dtimes",a[i],c);
    }
    getch();
    }

    ReplyReport user
  2. faras raoof

    buddy, try “debugging” using the “watch” option .if you are using turbo C, go to menu bar, select debugger , add “watch” values and have the pleasure of learning it yourself :)

    Reply
  3. faras raoof

    #include
    #include
    #include
    void main()
    {
    int k=0,i,j,l,t;
    char a[20],b[20],c[20],d[20];
    char name[5][6];
    clrscr();
    printf(“enter the main string\n”);
    gets(a);
    printf(“enter the word to be deleted\n”);
    gets(b);
    printf(“enter the word to be replaced with\n”);
    gets(c);
    l=strlen(a);
    for(i=0;i<l;i++)
    {
    j=0;
    while(a[i]!=' '&& a[i]!='')
    {
    d[j]=a[i];
    i++;
    j++;
    }
    d[j]='';
    strcpy(name[k],d);
    k++;
    }
    for(k=0;k<5;k++)
    {
    t= strcmp(name[k],b);
    if(t==0)
    {
    strcpy(name[k],c);
    }
    }
    for(k=0;k<5;k++)
    {
    printf(" %s",name[k]);
    }
    getch();
    }

    Reply
  4. faras raoof

    and by the way , that program uses the concept of string as a two dimensional array . the limitation is that you got to decide the maximum number of letters in the word and the exact number of words in the sentence at the beginning of the program ……in the above case, being 5 words(exactly) and maximum of 6 letters in the word.

    Reply
  5. Ganeshkumar

    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…

    Reply
  6. SOURAV BANERJEE

    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…

    Reply
  7. 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

    Reply
  8. guyz actually i am doing one program same like replace word.
    Q: read the line text more than 4 word and then replace all the blank space with an underscore(_)
    please help me.

    Reply

Leave a Reply