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

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

المطلوب

قم بإنشاء برنامج يطلب من المستخدم إدخال نص و من ثم إدخال حرف واحد.
بعدها سيقوم البرنامج بطباعة مكان كل ( Index ) موجود عليه هذا الحرف في النص.

مثال: إذا قام المستخدم بإدخال النص "Harmash is the best site to learn programming" و من ثم أدخل الحرف "a" فيجب أن يطبع له النتيجة التالية عند التشغيل.

'a' found at index: 1
'a' found at index: 4
'a' found at index: 30
'a' found at index: 39
	

الحل بلغة C

#include <stdio.h>
#include <string.h>
#define MAX_SIZE 1000
void main() {
char text[MAX_SIZE];
char character;
printf("Enter any text: ");
gets(text);
printf("Enter a character: ");
scanf("%c", &character);
for (int i = 0; i < strlen(text); i++)
{
if (text[i] == character)
{
printf("'%c' found at index: %d\n", text[i], i);
}
}
}
#include <stdio.h> #include <string.h> #define MAX_SIZE 1000 void main() { char text[MAX_SIZE]; char character; printf("Enter any text: "); gets(text); printf("Enter a character: "); scanf("%c", &character); for (int i = 0; i < strlen(text); i++) { if (text[i] == character) { printf("'%c' found at index: %d\n", text[i], i); } } }

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

Enter any text: Harmash is the best site to learn programming
Enter a character: a
'a' found at index: 1
'a' found at index: 4
'a' found at index: 30
'a' found at index: 39