Thread: Comparing special characters/Ignoring escape sequences

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    38

    Question Comparing special characters/Ignoring escape sequences

    Hello there,

    I am trying to compare two strings, namely:
    str1 = "hello\0"
    str2 = "hello\200"

    When I input the above two strings into strcmp, the output correctly shows that the output integer of str2 is greater than str1.

    I dont understand why, when I compare my strings, when I access the 6th element, str1[6] or str[6], they give me different answers. What is being referenced?

    Str1[6] returns nothing whilst str2[6] return a funny C character.

    I understand that a backslash suggests, refers to an "escape sequence", but I am not sure how to compare the strings whilst ignoring escape sequences... If i compare str1[6] to '\\', it also does not correspond.

    Thanks for your help!

    My code is as follows:
    Code:
    #include <stdio.h>int		ft_strcmp(char *s1, char *s2)
    {
    	int i;
    
    
    	i = 0;
    	while (s1[i] == s2[i] && ((s1[i] && s2[i]) != '\0'))
    	{
    	printf("s1[%d] == %c\ns2[%d] == %c\n\n", i, s1[i], i, s2[i]);
    		if (s1[i] != '\0')
    			i++;
    	}
    	printf("s1[%d] == %c\ns2[%d] == %c\n\n", i, s1[i], i, s2[i]);
    	return ((int)s1[i] - (int)s2[i]);
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The '\0' character is a special character, the end of string character to be exact. That would make the strlen() equal to 5.

    Also be careful, since your two strings are not the same length you are trying to access one of the strings (the shorter one) out of bounds.

    Str1[6] returns nothing whilst str2[6] return a funny C character.
    Well it doesn't actually return nothing, it returns a non-printing character (decimal 0). And the second is returning the escaped character, which could be one or more characters (not including the backslash) depending on the characters involved.

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    38
    Thanks for your reply.. Completely missed the fact that there was a "\0" in front of my eyes.

    I am confused as to how the standard library returns a -1 for the above scenario.

    I am getting str1[6] = NULL and str2[6] = -128.
    str1[6] - str2[6] = 0 - (-128) = 128

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. escape sequences
    By cfanatic in forum C Programming
    Replies: 2
    Last Post: 08-10-2012, 10:49 PM
  2. Escape sequences in c
    By linuxlover in forum C Programming
    Replies: 3
    Last Post: 11-03-2010, 02:39 PM
  3. C Escape sequences
    By Tool in forum C Programming
    Replies: 10
    Last Post: 11-20-2009, 09:38 PM
  4. Escape sequences in VC++
    By emilyh in forum Windows Programming
    Replies: 7
    Last Post: 09-26-2001, 07:02 AM
  5. Escape Sequences
    By Me-Again-Again in forum C Programming
    Replies: 3
    Last Post: 09-05-2001, 06:24 AM

Tags for this Thread