تحديات برمجيةالتحدي الثالث - حل التمرين الرابع بلغة C
المطلوب
قم بإنشاء برنامج يطلب من المستخدم إدخال نصّين, بعدها يقوم بطباعة ما إن كان النص الأول يبدأ بالنص الثاني أم لا.
مثال
Enter text 1: I like programming. Enter text 2: Hello -------------------------------- Text 1 is not start with 'Hello'
الحل بلغة C
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#define MAX_SIZE 1000
void main() {
char text1[MAX_SIZE];
char text2[MAX_SIZE];
bool found = true;
printf("Enter text 1: ");
gets(text1);
printf("Enter text 2: ");
gets(text2);
for (int i = 0; i < strlen(text2) && found == true; i++)
{
if (text1[i] != text2[i])
{
found = false;
}
}
printf("--------------------------------\n");
if (found)
{
printf("Text 1 start with '%s'", text2);
}
else
{
printf("Text 1 is not start 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];
bool found = true;
printf("Enter text 1: ");
gets(text1);
printf("Enter text 2: ");
gets(text2);
for (int i = 0; i < strlen(text2) && found == true; i++)
{
if (text1[i] != text2[i])
{
found = false;
}
}
printf("--------------------------------\n");
if (found)
{
printf("Text 1 start with '%s'", text2);
}
else
{
printf("Text 1 is not start with '%s'", text2);
}
}
سنحصل على النتيجة التالية في حال تم إدخال نفس القيم التي تم تعليمها باللون الأصفر عند التشغيل.
Enter text 1: I like programming.
Enter text 2: Hello
--------------------------------
Text 1 is not start with 'Hello'
Enter text 2: Hello
--------------------------------
Text 1 is not start with 'Hello'