#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.
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…
please give a better idea about a program to replace two words
bad program. should have some easy solution
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…
USING POINTERS ARE MORE BETTER THAN NORMAL CODINGS
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