Sunday, August 2, 2009

C++ help with character arrays?

I am supposed to compare two strings and write out a message that says"they are the same or the 1st string is greater. the first c-strign I can use the %26lt;%26lt;operator, but on the 2nd I have to use for loop. I worte the program,but it doesn't work correctly. Even if I enter a larger string than the 1st string it gives me that the first is larger than the second. I am also to declare an array w/20 char and initialize it w/ a string constant. then declare a 2nd array of 20 char. this is what I have.


#include %26lt;iostream%26gt;


#include %26lt;iomanip%26gt;


#include %26lt;cstring%26gt;


using namespace std;


int main ()


{


char a[21]= "Have a nice day!";


char cstring[21];


cout%26lt;%26lt;"Please enter a word: ";


cin.getline (cstring,21);


if (strcmp (a,cstring)%26gt;0)


{


cout%26lt;%26lt;"The first string is greater than the other.\n";


cout%26lt;%26lt;a%26lt;%26lt;" is greater than: "%26lt;%26lt;cstring%26lt;%26lt;endl;


}


for (int index=0; index%26lt;21;index++)


{


cout%26lt;%26lt;cstring[index]%26lt;%26lt;endl;








}








return 0;


}

C++ help with character arrays?
i am not an expert C++ programmer but from what I know, while using strcmp you must type the exact same string in as before but with a different number of charectors. From you assignment it sound like you need to compare any two strings. I would then use strlen instead. Heres a good example i made:





#include %26lt;iostream%26gt;


#include %26lt;iomanip%26gt;


#include %26lt;cstring%26gt;


using namespace std;


int main(int argc, char *argv[])


{





char cstring1[21];


char cstring2[21];





cout %26lt;%26lt; "This Program Compares 2 Strings and Tells Which is Greater in length\n";


cout %26lt;%26lt; " 1st Phrase: ";


cin.getline (cstring1,21); //Recieves input for string one


cout %26lt;%26lt; " 2nd Phrase: ";


cin.getline (cstring2,21); //Recieves input for string 2





if (strlen(cstring1) %26gt; strlen(cstring2)) //is string one larger?


{


cout %26lt;%26lt; cstring1 %26lt;%26lt; " is Greater in Length than the Sting " %26lt;%26lt; cstring2;


}else if(strlen(cstring1) == strlen(cstring2)){ //are the strings equal?


cout %26lt;%26lt; "The phrases are equal!";


}else{ //if all other fails do this!


cout %26lt;%26lt; cstring2 %26lt;%26lt; " is Greater in Length than the Sting " %26lt;%26lt; cstring1;


}


cout %26lt;%26lt; "\n\nPhrase 1\tPhrase 2\n";


for (int index=0; index%26lt;21;index++)


{


cout %26lt;%26lt; " " %26lt;%26lt; cstring1[index] %26lt;%26lt; "\t\t " %26lt;%26lt; cstring2[index] %26lt;%26lt; endl;


}


return 0;


}


------------------------Outputs-------...


This Program Compares 2 Strings and Tells Which is Greater in length


1st Phrase: string 1


2nd Phrase: string 22





string 22 is Greater in Length than the String string 1





String 1 String 2


s s


t t


r r


i i


n n


g g





1 2


2





Hope this helps ya!
Reply:When strcmp return 1 or -1 it does not say something bout the length!





Strcmp compares the characters from left to right. When a character is different (see ascii table) then the other one, it will stop processing. It returns 1 or -1 depending on the differences in ASCII value of the characters.





See the ascii table :http://www.asciitable.com/ for values of characters.
Reply:same string return 0;


larger string return -1;


shorter string return 1;


I can not see anything wrong, check my output.


----------------


int main(int argc, char *argv[])


{





char a[21]= "Have a nice day!";


char cstring[21];


cout%26lt;%26lt;"Please enter a word: ";


cin.getline (cstring,21);





cout %26lt;%26lt; "strcmp()=" %26lt;%26lt; strcmp (a,cstring) %26lt;%26lt; "\n";





if (strcmp (a,cstring)%26gt;0)


{


cout%26lt;%26lt;"The first string is greater than the other.\n";


cout%26lt;%26lt;a%26lt;%26lt;" is greater than: "%26lt;%26lt;cstring%26lt;%26lt;endl;


}





for (int index=0; index%26lt;21;index++)


{


cout%26lt;%26lt;cstring[index]%26lt;%26lt;endl;


}





return 0;


}





------------


Please enter a word: Have a nice day!


strcmp()=0


H


a


v


e





a





n


i


c


e





d


a


y


!


-----------------------


Please enter a word: Have a nice day!Have a nice day!


strcmp()=-1


H


a


v


e





a





n


i


c


e





d


a


y


!


H


a


v


e


------------------------


Please enter a word: Have a nice d


strcmp()=1


The first string is greater than the other.


Have a nice day! is greater than: Have a nice d


H


a


v


e





a





n


i


c


e





d


No comments:

Post a Comment