View Full Version : سوال: حذف یک رشته در رشته دیگر
Azar.099
یک شنبه 01 دی 1392, 13:34 عصر
با سلام دوستان
میخوام تابعی بنویسم که دورشته دریافت کند و در رشته اول به دنبال رشته دوم جسنجو کنه و در صورت هر بار پیدا شدن ان را حذف کنه ..
اینو نوشتم ولی نمیدونم اشتباهم کجاست ..
#include<iostream>
using namespace std;
void func(char *a, char b[])
{
while(a!= NULL)
{
char *pch;
pch = strstr(a,b);
strcpy (pch," ");
a++;
}
puts(a);
}
int main ()
{
char *a;
a = new char[100];
char b[100];
gets(a);
gets(b);
func(a,b);
return 0;
}
Azar.099
دوشنبه 02 دی 1392, 19:50 عصر
اینم جواب با استفاده از memmove
#include <stdio.h>
#include <cstring>
#include <memory.h>
void removeFromString(char *str, char *word)
{
int wordSize=strlen(word);
int strSize=strlen(str);
char *wordStart=strstr(str,word);
while(wordStart!=NULL)
{
int findIndex=wordStart-str;
memmove(wordStart,wordStart+wordSize,strSize-findIndex);
wordStart=strstr(str,word);
}
}
int main ()
{
char *a=new char[100];
char *b=new char[100];
gets(a);
gets(b);
removeFromString(a,b);
puts(a);
delete[] a;
delete[] b;
}
Azar.099
دوشنبه 02 دی 1392, 19:51 عصر
این هم بدون memmove....
#include <string>
#include <iostream>
using namespace std;
void tedad(char s1[100], char s2[10] ,char *natije )
{
char final[500] ="-" ;// matne natije
int s1lengh=0;
int s2lengh=0;
int ps1 = 0; // eshare be akharin harf dar s1
int pf = 0 ; // eshare be akharin harf dar final
s1lengh = strlen(s1);
s2lengh = strlen(s2);
int b = 0 ;
s1lengh = s1lengh+ s2lengh +1;
for(int h = (s1lengh - s2lengh) ; h <= s1lengh ; h++)
{
s1[h] = s2[b];
b++;
}
for(int i = 0; i <= s1lengh ; i++)
{
int count = 0;
int j =0;
while(j!=s2lengh){
if(s1[i] == s2[j])
{
if(i <= s1lengh - 1 )
{
count++;
i++;
j++;
}
else
{
break;
}
}
else
{
break;
}
if(count == s2lengh )
{
for(int m = pf ; m < i-s2lengh ; m++)
{
final[ps1] = s1[m];
ps1++;
}
pf =i;
final[ps1] = ' ';
}
}
}
for(int i = 0 ; i < 500 ; i++)
{
natije[i]= final[i];
}
}
int main()
{
char str1[500];
char str2[20 ];
cout <<"matne morede nazar ra vared konid : ";
gets(str1);
cout<<"kalameye ke bayad peyda konim :";
cin>>str2;
char natije[500] ;
tedad(str1,str2,natije);
cout << natije <<endl;
return 0;
}
Azar.099
جمعه 06 دی 1392, 18:58 عصر
سلام دوستان
میشه کمک کنین بگین اشتباه برنامه من چیه ؟؟
void func(char a[] , char b[])
{
int i = 0;
int j = 0;
int s = 0;
int t = strlen(b);
while(a[i] != NULL)
{
while(b[j] != NULL)
{
if(a[i] == b[j])
{
j++;
i++;
s = j;
}
else
break;
}
if( s == j)
{
a[i] = a[i+t];
i++;
}
else
i++;
}
cout << a << endl;
}
int main()
{
char s[100];
char d[30];
gets(s);
gets(d);
func(s,d);
return 0;
}
vBulletin® v4.2.5, Copyright ©2000-1404, Jelsoft Enterprises Ltd.