المطلوب
قم بإنشاء برنامج يطلب من المستخدم إدخال ثلاث نصوص و يخزنهم في ثلاث متغيرات نصية هي S1, S2 و S3 و بعدها ينفذ التالي:
يخبره ما إن كان دمج
S1معS2يساويS3أم لا.يخبره ما إن كان
S1يمثل جزء منS2أو يساويه ( أيS1 == s2).إذا كان طول
S1أكبر من طولS2قم بإضافة نصS2علىS1و خزن الناتج في متغير نصي جديد إسمهS4.إذا كان طول
S2أكبر من طولS1قم بإضافة نصS1علىS2و خزن الناتج في متغير نصي جديد إسمهS4.يخبره ما إن كان
S2يمثل جزء من ثاني نصف فيS1.يعرض له الأحرف الموجودة في أول نصف في
S1.
الحل بلغة C
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 1000
void main() {
char S1[MAX_SIZE];
char S2[MAX_SIZE];
char S3[MAX_SIZE];
char * S4 = (char*)malloc(sizeof(MAX_SIZE) + 1);
char * concatenated = (char*)malloc(sizeof(MAX_SIZE) + 1);
char * temp = (char*)malloc(sizeof(MAX_SIZE) + 1);
int counter = 0;
int concateIndex = 0;
printf("Enter S1: ");
gets(S1);
printf("Enter S2: ");
gets(S2);
printf("Enter S3: ");
gets(S3);
printf("---------------------------------------\n");
// Part 1
for (int i = 0; i < strlen(S1); i++)
{
concatenated[concateIndex] = S1[i];
concateIndex++;
}
for (int i = 0; i < strlen(S2); i++)
{
concatenated[concateIndex] = S2[i];
concateIndex++;
}
concatenated[concateIndex] = '\0';
printf("The concatenation of S1 and S2 is equal S3? ");
printf("%s\n", (strcmp(S3, concatenated) == 0)? "True": "False");
// Part 2
printf("S1 is part of S2 or S1=S2? ");
printf("%s\n", (strstr(S2, S1) != NULL)? "True": "False");
// Part 3
if (strlen(S1) > strlen(S2))
{
int index = 0;
for (int i = 0; i < strlen(S2); i++)
{
S4[index] = S2[i];
index++;
}
for (int i = 0; i < strlen(S1); i++)
{
S4[index] = S1[i];
index++;
}
S4[index] = '\0';
}
// Part 4
if (strlen(S2) > strlen(S1))
{
int index = 0;
for (int i = 0; i < strlen(S1); i++)
{
S4[index] = S1[i];
index++;
}
for (int i = 0; i < strlen(S2); i++)
{
S4[index] = S2[i];
index++;
}
S4[index] = '\0';
}
// Part 5
printf("S2 is part of the second half of S1? ");
for (int i = strlen(S1) / 2; i <= strlen(S1); i++)
{
temp[counter] = S1[i];
counter++;
}
temp[counter] = '\0';
printf("%s\n", (strstr(temp, S2) != NULL)? "True": "False");
// Part 6
printf("The first half characters of S1 are: ");
for (int i = 0; i < strlen(S1) / 2; i++)
{
printf("%c", S1[i]);
}
}
سنحصل على النتيجة التالية في حال تم إدخال نفس القيم التي تم تعليمها باللون الأصفر عند التشغيل.
Enter S1: Mhamad
Enter S2: Harmush
Enter S3: MhamadHarmush
---------------------------------------
The concatenation of S1 and S2 is equal S3? true
S1 is part of S2 or S1=S2? false
S2 is part of the second half of S1? false
The first half characters of S1 are: Mha
Enter S2: Harmush
Enter S3: MhamadHarmush
---------------------------------------
The concatenation of S1 and S2 is equal S3? true
S1 is part of S2 or S1=S2? false
S2 is part of the second half of S1? false
The first half characters of S1 are: Mha


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