-3

how can i find a length of a string using data type " char " i tried it many time but i did not work . i also see cplusplus tutorial but could not fix example under below

#include <stdio.h>
#include <iostream>
#include<string>

using namespace std;

int main() {
    char str1[80];
    char str2[80];

    cout << "Enter string 1 : ";
    cin.getline(str1,80); cout << endl;
    cout << "Enter string 2 : ";
    cin.getline(str2,80);cout << endl;
    cout << "String 1  is :-> " << (unsigned)strlen(str1) << endl;
    cout << "String 1  is :-> " << (unsigned)strlen(str2) << endl;
}
CJCombrink
  • 3,738
  • 1
  • 22
  • 38
ABC
  • 13
  • 1
  • 2
  • 9

1 Answers1

2

If you are using C++ I would strongly suggest move away from char* for strings and start using std::string.

string str1;
string str2;

cout << "Enter string 1 : ";
cin >> str1; cout << endl;
cout << "Enter string 2 : ";
cin >> str2; cout << endl;
cout << "String 1  is :-> " << str1.size() << endl;
cout << "String 2  is :-> " << str2.size() << endl;

PS: Your posted code works for me.

CJCombrink
  • 3,738
  • 1
  • 22
  • 38