Question:
C++ help with character arrays?
novice
2008-03-12 21:17:04 UTC
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 <#include
#include
#include
using namespace std;
int main ()
{
char a[21]= "Have a nice day!";
char cstring[21];
cout<<"Please enter a word: ";
cin.getline (cstring,21);
if (strcmp (a,cstring)>0)
{
cout<<"The first string is greater than the other.\n";
cout< }
for (int index=0; index<21;index++)
{
cout<

}


return 0;
}
Three answers:
gregtheorangeman
2008-03-12 22:35:42 UTC
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

#include

#include

using namespace std;

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

{



char cstring1[21];

char cstring2[21];



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

cout << " 1st Phrase: ";

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

cout << " 2nd Phrase: ";

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



if (strlen(cstring1) > strlen(cstring2)) //is string one larger?

{

cout << cstring1 << " is Greater in Length than the Sting " << cstring2;

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

cout << "The phrases are equal!";

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

cout << cstring2 << " is Greater in Length than the Sting " << cstring1;

}

cout << "\n\nPhrase 1\tPhrase 2\n";

for (int index=0; index<21;index++)

{

cout << " " << cstring1[index] << "\t\t " << cstring2[index] << 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!
iqueen
2008-03-12 21:49:51 UTC
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<<"Please enter a word: ";

cin.getline (cstring,21);



cout << "strcmp()=" << strcmp (a,cstring) << "\n";



if (strcmp (a,cstring)>0)

{

cout<<"The first string is greater than the other.\n";

cout<
}



for (int index=0; index<21;index++)

{

cout<
}



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
Denny Colt
2008-03-13 03:18:29 UTC
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.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...