Thread: Help w/ comparings two strings case sensitive

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    6

    Help w/ comparings two strings case sensitive

    This is what i have for comparing two strings case sensitive. And i added a printf statement to test the numerical values of teh strings and the values are supposed to be the same but they are not. The plan was to use the numerical values to test, but i get different outputs.
    Code:
    #include <stdio.h>
    #include <conio.h>
    main()
    {
    	int loop=0, strone, strtwo; 
    	char str[80]={"cprog"};
    	char str2[80]={"cprog"};
    	
    
    strone=str;
    strtwo=str2;
    
    
    printf("%d", strone);
    printf("\n%d", strtwo);
    			
    /*this tests whether the two are same or not*/
    	/*	if(strone==strtwo)
    		{
    			printf("The strings are equal");
    		}		
    		else
    		printf("The strings are not equal");
    	
    	*/	getch();
    	
    
    }
    
    /*OUTPUT
    
    1244964
    1244884
    
      */
    -ikkin
    Last edited by ikkin; 11-12-2003 at 06:21 PM.

  2. #2
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    strcmpi(str1, str2) returns 1 if 2 strings are the same, excluding case, otherwise it returns 0 if they are different.

    EDIT: Don't forget to include string.h!!
    Last edited by Lurker; 11-12-2003 at 06:23 PM.
    Do not make direct eye contact with me.

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    6
    ya i figured out how to compare two srings ignoring the case difference but i need it to be case sensitive__ any ideas?


    -ikkin

  4. #4
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    strcmp(str1, str2) is case sensitive - sorry, misread post .
    strcmpi(str1, str2) works the same way, but isnt case sensitive .
    Do not make direct eye contact with me.

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    6

    Smile

    Thats okay

    But the problem is that i have to write my own function that compares the two strings

    Thanx.
    Last edited by ikkin; 11-12-2003 at 06:36 PM.

  6. #6
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    void my_strcmp(char *string1,char *string2); /*compares string one to string 2*/
    int main(){
            char strone[BUFSIZ];
            char strtwo[BUFSIZ];
            printf("Enter a string!\n");
            fgets(strone,sizeof(strone),stdin);
            printf("Enter another string!\n");
            fgets(strtwo,sizeof(strtwo),stdin);
            printf("%s%s",strone,strtwo);
            my_strcmp(strone,strtwo);
            return 0;
    }
    void my_strcmp(char *string1,char *string2){
            int str1=0,str2=0,diff,;
            while(string1[str1]!='\0'){
                    str1++;
            }
            while(string2[str2]!='\0'){
                    str2++;
            }
            diff=str1-str2;
            printf("diff = %i\n",diff);
            printf("str1 = %i\nstr2 = %i\n",str1,str2);
    }
    This is a start I don't feel like doing the whole case sensitive side of this program if anyone wants to build off of this I wouldn't mind
    Last edited by linuxdude; 11-12-2003 at 10:19 PM.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Since this is homework, you should figure this out on your own, right?

    The basic idea is that uppercase letters and and their lowercase equivalent are always a certain distance apart on an ASCII chart.

    Here's the code for a simple comparison function you can easily modify into strcmp() (and eventually, strcasecmp()):

    Code:
    int
     streq(const char s1[], const char s2[])
    {
     int index;
    
        for(index = 0; (s1[index] != 0) && (s2[index] != 0); ++index)
       {
            if(s1[index] != s2[index])  return 0;
       } 
      
     // make sure we've reached the end of both strings:
    
        if((s1[index] != 0) || (s2[index] != 0))  return 0; 
    
     return 1;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    .
    Join Date
    Nov 2003
    Posts
    307
    Since this uses pointers, I doubt the teacher will accept it - but this is how string comparisons work
    Code:
    int my_strcmp(const char *a, const char *b){
           int i=0;
           while (*(a+i)==*(b+i) && *(a+i) && *(b+i)) i++;
           return *(a+i)-*(b+i);
    
    }
    Note that you return the difference at the end.
    This my_strcmp returns 0 if equal, -1 if a < b and 1 if a > b

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  3. Basic Data types continue
    By viryonia in forum C Programming
    Replies: 6
    Last Post: 03-10-2003, 10:21 AM
  4. rand()
    By serious in forum C Programming
    Replies: 8
    Last Post: 02-15-2002, 02:07 AM