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

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

المطلوب

قم بإنشاء برنامج يطلب من المستخدم إدخال نصّين, بعدها يقوم بطباعة ما إن كان النص الأول ينتهي بالنص الثاني أم لا.

مثال

Enter text 1: Welcome to my world
Enter text 2: world
--------------------------------
Text 1 is end with 'world'
	


الحل بلغة C

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#define MAX_SIZE 1000
void main() {
char text1[MAX_SIZE];
char text2[MAX_SIZE];
int text2Counter = 0;
bool found = true;
printf("Enter text 1: ");
gets(text1);
printf("Enter text 2: ");
gets(text2);
for (int i = strlen(text1) - strlen(text2); i < strlen(text1) && found == true; i++)
{
if (text1[i] != text2[text2Counter])
{
found = false;
}
text2Counter++;
}
printf("--------------------------------\n");
if (found)
{
printf("Text 1 end with '%s'", text2);
}
else
{
printf("Text 1 is not end with '%s'", text2);
}
}
#include <stdio.h> #include <string.h> #include <stdbool.h> #define MAX_SIZE 1000 void main() { char text1[MAX_SIZE]; char text2[MAX_SIZE]; int text2Counter = 0; bool found = true; printf("Enter text 1: "); gets(text1); printf("Enter text 2: "); gets(text2); for (int i = strlen(text1) - strlen(text2); i < strlen(text1) && found == true; i++) { if (text1[i] != text2[text2Counter]) { found = false; } text2Counter++; } printf("--------------------------------\n"); if (found) { printf("Text 1 end with '%s'", text2); } else { printf("Text 1 is not end with '%s'", text2); } }

سنحصل على النتيجة التالية في حال تم إدخال نفس القيم التي تم تعليمها باللون الأصفر عند التشغيل.

Enter text 1: Welcome to my world
Enter text 2: world
--------------------------------
Text 1 is end with 'world'

الدورات

أدوات مساعدة

أقسام الموقع

دورات
مقالات كتب مشاريع أسئلة