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

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

المطلوب

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

مثال

Enter text 1: I like programming.
Enter text 2: Hello
--------------------------------
Text 1 is not start with 'Hello'
	


الحل بلغة C++

#include <iostream>
#include <string>
#include <stdbool.h>
int main() {
std::string text1, text2;
bool found = true;
std::cout << "Enter text 1: ";
getline(std::cin, text1);
std::cout << "Enter text 2: ";
getline(std::cin, text2);
for (int i = 0; i < text2.length() && found == true; i++)
{
if (text1[i] != text2[i])
{
found = false;
}
}
std::cout << "--------------------------------\n";
if (found)
{
std::cout << "Text 1 start with '" << text2 << "'";
}
else
{
std::cout << "Text 1 is not start with '" << text2 << "'";
}
char end; std::cin >> end;
return 0;
}
#include <iostream> #include <string> #include <stdbool.h> int main() { std::string text1, text2; bool found = true; std::cout << "Enter text 1: "; getline(std::cin, text1); std::cout << "Enter text 2: "; getline(std::cin, text2); for (int i = 0; i < text2.length() && found == true; i++) { if (text1[i] != text2[i]) { found = false; } } std::cout << "--------------------------------\n"; if (found) { std::cout << "Text 1 start with '" << text2 << "'"; } else { std::cout << "Text 1 is not start with '" << text2 << "'"; } char end; std::cin >> end; return 0; }

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

Enter text 1: I like programming.
Enter text 2: Hello
--------------------------------
Text 1 is not start with 'Hello'

الدورات

أدوات مساعدة

أقسام الموقع

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