hi,
How do i check if a character is the same as another character ?
The characters are in a character array.
thnx
hi,
How do i check if a character is the same as another character ?
The characters are in a character array.
thnx
there are heaps of ways...
iterate through and compare them... it's that simple.
Quidquid latine dictum sit, altum sonatur.
Whatever is said in Latin sounds profound.
i tried
results in printing nothingCode:char string[ 3 ] = "ee"; if ( string[ 0 ] == string[ 1 ] ) printf( "match" );
works for me...
i'm using MSVC++ v6.0 SP5
Quidquid latine dictum sit, altum sonatur.
Whatever is said in Latin sounds profound.
well can u pls post the code you use for me to study ?
thnx
Try the string compare function held in <string.h>
i think the syntax is like
strcmp(ptr_1, ptr_2);
where ptr_1 & ptr_2 are pointers //duh
to the array's, if you need to check the length of the string you can use
len = strlen(array_1)
this massures the size of the string up to & not including the NULL char
then compare like
strcmnp(ptr_1, ptr_2, len)
note the added n in strcmnp
Before you judge a man, walk a mile in his
shoes. After that, who cares.. He's a mile away and you've got
his shoes.
************William Connoly
how about not using other functions ?
thats what i'm expected and supposed to do
when i tried doin it on its own it seems to have no problems. WHen i tried it on my palindrome program it doesn' work. Pls look at the other thread to help me
thnx in advance
If you're tring to compare character STRINGS (arrays of characters) use strcmp, etc in string.h
Computers represent single characters internally as numbers -- so you just compare them by using the double equals sign for testing equality:
if( mystring[0] == mystring[1] ) printf("Same!\n");
A few notes. Some compilers don't allow "aggregate array initialization" meaning your string declaration can't also put a value in the string.
Try declaring, then in your code add the value:
char mystring[3];
sprintf( mystring, "ee" );
One final note, this gets beginners into trouble ALL THE TIME -- your print statement was not terminated with a backslash-n (\n). It's possible
that your program ran beautifully, but because the OS buffers output (collects a bunch of output and then dumps it all to the screen in batches instead of after every print statement) you might not be seeing the output. Always end your print statements with \n and you will generally have
an easier time debugging. To make absolutely certain, put an exit(); immediately after that and
your program will terminate there, but that means the print statement will show up, since any later crashes your program was going to do could interrupt the output.
#include <stdio.h>
int main() {
char mystring[3];
sprintf( mystring, "ee" );
if( mystring[0] == mystring[1] ) printf("same!\n");
else printf("Not the same.\n");
}
I tried this with GCC. And it works. Besides, even when not compiling and running this little thing you can see that it is correct. Your string[0] == 'e' and string[1] == 'e'. So there shouldn't be problems.
Code:#include <stdio.h> int main (void) { char string[ 3 ] = "ee"; if ( string[ 0 ] == string[ 1 ] ) printf( "match" ); }
yeah but my program still isn't workin. Well, guess thats another problem i'm havin....
instead of
char string[ 3 ] = "ee";
try this...
char string[ 3 ] = { "ee" };
you are initialising an array an as such you reaaly need the chicken lips.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
It is not really needed in this case. It would be in case of an array of strings.
But Nutshell, you've tried the little progam I posted? In case it doesn't compile, then it could be that your compiler is not ANSI compatible. Another reason is that the code you posted is part of a larger program where things happen which have bad influence on this all.