How to get pointer to char to compare its ascii value pointed to

This is a discussion on How to get pointer to char to compare its ascii value pointed to within the C++ Programming forums, part of the General Programming Boards category; how do I get a pointer to a const char to compare the ascii value of the char to which ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    28

    How to get pointer to char to compare its ascii value pointed to

    how do I get a pointer to a const char to compare the ascii value of the char to which it is pointed, with another similar expression.
    EX: const char *string1 == const char *string2;
    I need to increment these and at the point where the comparison fails return a < or > or ==.
    Last edited by rc7j; 10-28-2001 at 10:14 PM.
    rc7j

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    well, your idea is correct.

    *string1 is the character value pointed to by string1, so it will work just fine.

    Something like this will find the first discrepancy between strings:

    Code:
    for (;*string1 != '\0' && *string2 != '\0';string1++,string2++){
    	if (*string1 < *string2){
    		//stuff here
    	}
    	if (*string1 > *string2){
    		//stuff here
    	}
    }

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    28

    runtime error

    Thanks. This code will compile but produces a runtime error
    /*This program uses a function to compare strings
    */

    #include <stdio.h>

    int myStrcmp(const char *s1, const char *s2);

    int main(void)
    {
    const char *s1 = "a", *s2 = "B";

    if (myStrcmp(s1, s2) < 0)
    printf("%s is less than %s\n", s1, s2);
    else if (myStrcmp(s1, s2) == 0)
    printf("%s is identical to %s\n", s1, s2);
    else
    printf("%s is greater than %s\n", *s1, *s2);

    return(0);
    }

    int myStrcmp(const char *s1, const char *s2)
    {
    for (;*s1 != '\0' && *s2 != '\0';s1++,s2++)
    {
    if (*s1 < *s2)
    return -1;

    if (*s1 > *s2)
    return 1;

    if (*s1 == *s2)
    return 0;
    }
    }
    rc7j

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    One problem -- this only compares the first character of the strings. Remove the == check in the loop, because if the strings are equal at that character, the loop must advance to the next char.

    Your crash is because your third printf() is wring -- eliminate the *'s on the strings, that won't work right.

    Here's a modified myStrcmp that will correctly compare strings. The checks after the loop are to make sure that things like "stringA" < "stringAbutlonger" work properly (the shorter string is < the longer).

    Code:
    int myStrcmp(const char *s1, const char *s2){
    	for (;*s1 != '\0' && *s2 != '\0';s1++,s2++){
      		if (*s1 < *s2)
    			return -1;
    		if (*s1 > *s2)
    			return 1;
    	}
    	if (*s2 != '\0') // this means s2 is longer than s1
    		return -1;   // -1 because s1 < s2
    	else if (*s1 != '\0') // s1 is longer
    		return 1;
    	else return 0;
    }
    Last edited by The V.; 10-29-2001 at 12:16 AM.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    28
    Thanks, this code works with one exception.
    The output for:
    if (s1 < s2)
    return -1;
    if (s1 > s2)
    return 1;
    is backward.
    EX: s1="a" compared to s2="B" shows a < B.
    Using the dereferencing symbol on the string names produces a runtime error.
    Somehow I must get the compiler to recognize and compare the ascii value of the character at *s1 and *s2.
    rc7j

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    This is doing the compares correctly, the code DOES work.

    'a' > 'B' because the ascii code for lowercase letters is higher than the ascii for uppercase.

    So, if you used this code to sort, and listed from least to greatest, you'd have:

    Alpha
    Beta
    Zeta
    aLpha
    alpha
    beta
    betaMax

    To do case insensitive sorting is harder; this function is case sensitive, and does correctly say that "A" < "B" < "C" < "a" < "b" < "c", etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  2. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  3. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 11:36 AM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 09:40 PM

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