Programming Basics SQL HTML CSS JavaScript Python C++ Java JavaFX Swing Problem Solving English English Conversations Computer Fundamentals Learn Typing

تحديات برمجيةالتحدي الثاني - حل التمرين الرابع بلغة C

المطلوب

قم بإنشاء برنامج يطلب من المستخدم إدخال ثلاث نصوص و يخزنهم في ثلاث متغيرات نصية هي S1, S2 و S3 و بعدها ينفذ التالي:

  1. يخبره ما إن كان دمج S1 مع S2 يساوي S3 أم لا.
  2. يخبره ما إن كان S1 يمثل جزء من S2 أو يساويه ( أي S1 == s2 ).
  3. إذا كان طول S1 أكبر من طول S2 قم بإضافة نص S2 على S1 و خزن الناتج في متغير نصي جديد إسمه S4.
  4. إذا كان طول S2 أكبر من طول S1 قم بإضافة نص S1 على S2 و خزن الناتج في متغير نصي جديد إسمه S4.
  5. يخبره ما إن كان S2 يمثل جزء من ثاني نصف في S1.
  6. يعرض له الأحرف الموجودة في أول نصف في 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]);
}
}
#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