How do i check if a character is the same as another character ?

This is a discussion on How do i check if a character is the same as another character ? within the C Programming forums, part of the General Programming Boards category; hi, How do i check if a character is the same as another character ? The characters are in a ...

  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    How do i check if a character is the same as another character ?

    hi,

    How do i check if a character is the same as another character ?
    The characters are in a character array.

    thnx

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    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.

  3. #3
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    i tried

    Code:
    char string[ 3 ] = "ee";
    
    if ( string[ 0 ] == string[ 1 ] ) 
       printf( "match" );
    results in printing nothing

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    works for me...

    i'm using MSVC++ v6.0 SP5
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  5. #5
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    well can u pls post the code you use for me to study ?

    thnx

  6. #6
    Registered User breed's Avatar
    Join Date
    Oct 2001
    Posts
    91
    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

  7. #7
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    how about not using other functions ?

    thats what i'm expected and supposed to do

  8. #8
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    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

  9. #9
    Registered User
    Join Date
    Jan 2002
    Posts
    9

    Comparing characters

    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");
    }

  10. #10
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    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" );
    
    }

  11. #11
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    yeah but my program still isn't workin. Well, guess thats another problem i'm havin....

  12. #12
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    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

  13. #13
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-26-2009, 11:24 AM
  2. Not working in linux...
    By mramazing in forum C++ Programming
    Replies: 6
    Last Post: 01-08-2009, 01:18 AM
  3. allegro issues
    By mramazing in forum C++ Programming
    Replies: 1
    Last Post: 01-07-2009, 10:56 PM
  4. get wide character and multibyte character value
    By George2 in forum C++ Programming
    Replies: 27
    Last Post: 01-27-2008, 04:10 AM
  5. Character problem!!
    By cBegginer in forum C Programming
    Replies: 3
    Last Post: 09-02-2005, 11:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21