Write a program in C++ to find the length of a string without using the library function

  • برمجة سي بلس بلس
  • برمجة

Write a program in C++ to find the length of a string without using the library function

Sample Output:

 Find the length of a string:                                          
---------------------------------                                      
 Input a string: w3resource.com                                        
The string contains 14 number of characters.                           
So, the length of the string w3resource.com is:14 

الأجوبة

#include <iostream>
#include <string>
using namespace std;

int main()
{
    char str1[50];
    int i, l = 0;
    cout << "\n\n Find the length of a string:\n";
    cout << "---------------------------------\n";
    cout << " Input a string: ";
    cin >> str1;
    for (i = 0; str1[i] != '\0'; i++) {
        l++;
    }
    cout << "The string contains " << l << " number of characters." << endl;
    cout << "So, the length of the string " << str1 << " is:" << l << endl;
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...