im trying to compare two strings length and print the longest but because i had to define the string as a static one i cant find a way to do it...help??
#include <iostream>
#include <string>
usingnamespace std;
class String
{
staticchar s[255];
int len;
public:
void assign(constchar*);
int length()const;
void print()const;
};
void String::assign(constchar* str)
{
strcpy(s,str);
len=strlen(str);
}
int String::length()const
{
return len;
}
void String::print()const
{
cout<<"The string: "<<s<<endl;
}
int main()
{
String s1,s2;
s1.assign("Goodbye");
s2.assign("Hello");
if (s1.length()>=s2.length())
s1.print();
else
s2.print();
}
after this code the output is always s2's string. i understand its because the string declared as static. my question is how can i make the if-else work?? thx..
i was asked to create a class with a static char array limited to 255 and and another data member representing the string's length. then what im asked to do its creating two objects and compare them both and print the string with the bigger length...
I bet confusing terms: Not static, but static as in non-dynamic.
char array[255]; vs char * array = newchar [255];
Remove the word 'static' from the class.
Note: I could call s1.assign( text_of_entire_LOTR() ); and you would happily overwrite whatever happens to be after your 255 bytes. Modify your assign to check and guard against such input.