حذف یک رشته در رشته دیگر - هفت خط کد انجمن پرسش و پاسخ برنامه نویسی

حذف یک رشته در رشته دیگر

0 امتیاز

با سلام دوستان

میخوام تابعی بنویسم که دورشته دریافت کند و در رشته اول به دنبال رشته دوم جسنجو کنه و در صورت هر بار پیدا شدن ان را حذف کنه ..

اینو نوشتم ولی نمیدونم اشتباهم کجاست ..

#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;
}

 

سوال شده آذر 30, 1392  بوسیله ی Azar (امتیاز 628)   29 42 61
دوباره تگ گذاری شد آذر 30, 1392 بوسیله ی BlueBlade

2 پاسخ

0 امتیاز

از 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;
}

 

پاسخ داده شده آذر 30, 1392 بوسیله ی BlueBlade (امتیاز 15,315)   15 18 89
نمیخوام از memmove استفاده کنم ..
0 امتیاز
این هم بدون 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;
}

 

پاسخ داده شده دی 1, 1392 بوسیله ی Azar (امتیاز 628)   29 42 61
...