Convert String to Lower Case Crashed

This is a discussion on Convert String to Lower Case Crashed within the C Programming forums, part of the General Programming Boards category; Here I am again, and here is another question. Again I'm trying to write replacement functions of exist ones in ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    8

    Question Convert String to Lower Case Crashed

    Here I am again, and here is another question.

    Again I'm trying to write replacement functions of exist ones in order to understand how C works. Here is my function, and it supposed to do a string conversion to lower cases:

    Code:
    int my_strlwr(char *str)
    {
    	int i;
    
    	for (i = 0; *(str+i) != NULL; i++)
    	{
    		if (*(str+i) > 64 && *(str+i) < 91)
    		{
    			*(str+i) += 32;
    		}
    	}
    
    	return 0;
    }
    The test code is:
    Code:
    int main()
    {
    	char *str = "Some";
    
    	my_strlwr(str);
    
    	printf("%s\n", str);
    
    	return 0;
    }
    Somehow when the program executes to that if statement, it crashes. Please help, thanks in advance.

    ps: and I know the program is written in a crappy manner, I'll make it neater after the major problem is solved.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    This is not a writable string.
    Code:
    char *str = "Some";
    This is.
    Code:
    char str[] = "Some";
    [edit]Here
    Code:
    *(str+i) != NULL
    oughtta be
    Code:
    *(str+i) != '\0'
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    8
    Oh~~~damn it...Thank you so much Dave.

    And btw, I thought NULL can represent '\0'

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    It can. It may be a pointer.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    8
    So '\0' in this case is more strict, right?

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    54
    Quote Originally Posted by woozy View Post
    So '\0' in this case is more strict, right?
    They are the same, but NULL is generally used as a value for pointers (it = 0 though)

    Pointers can use array subscript notation, so
    Code:
    *(str+i) += 32;
    is the same as
    Code:
    str[i] += 32;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  2. Intel syntax on MinGW ?
    By TmX in forum Tech Board
    Replies: 2
    Last Post: 01-06-2007, 09:44 AM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  5. rand()
    By serious in forum C Programming
    Replies: 8
    Last Post: 02-15-2002, 02:07 AM

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