What happens with the following piece of code Why Explain in
What happens with the following piece of code? Why? Explain in detail. string name 1 = \"Mary\"; string name2 = \"Mark\"; if (name1 > name2) cout
Solution
The defined piece of code shows an error because here assignment makes integer from pointer without a cast as A string is a class that contains a char array, but automatically manages it for you so you can\'t compare length of strings defined in string type variable directly you have to use defined methods-
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
int main () {
char str1[12] = \"marry\";
char str2[12] = \"mark\";
int len1,len2 ;
len1 = strlen(str1);
len2 = strlen(str2);
if(len1>len2)
{
cout << \"marry\"<< endl;
}
else
{
cout << \"mark\"<< endl;
}
