المطلوب
قم بتعريف دالة إسمها RemoveAll, عند استدعاءها نمرر لها نصيّن, فترجع نسخة من النص لا تحتوي على النص الثاني.
بعدها قم بتجربة هذه الدالة في البرنامج.
مثال: إذا قمنا باستخدام الدالة RemoveAll() و تمرير النص "I like cats. I have one cat." و الكلمة "cat" لها فإنها سترجع النص "I like s. I have one .".
الحل بلغة C
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
char * removeAll(char * s1, char * s2)
{
int s1Len = strlen(s1);
int s2Len = strlen(s2);
char * newS = (char*)malloc(sizeof(s1) + 1);
bool found;
int newSIndex = 0;
int s2Index;
if (s1Len < s2Len)
{
return s1;
}
for (int i = 0; i < s1Len;)
{
found = true;
s2Index = 0;
for (int j = i; j < i + s2Len && j < s1Len; j++)
{
if (s1[j] != s2[s2Index])
{
found = false;
}
s2Index++;
}
if (found == true)
{
i += s2Len;
}
else
{
newS[newSIndex] = s1[i];
newSIndex++;
i++;
}
}
newS[newSIndex] = '\0';
return newS;
}
void main() {
char * s1 = "I like cats. I have one cat.";
char * s2 = "cat";
char * newS;
printf("Before: %s \n", s1);
newS = removeAll(s1, s2);
printf("After: %s \n", newS);
}
سنحصل على النتيجة التالية عند التشغيل.
Before: I like cats. I have one cat. After: I like s. I have one .


محرر الويب
نظام الألوان
محول الوحدات
محلل عناوين الشبكات