str_capitalize UDF problem

This is a discussion on str_capitalize UDF problem within the C Programming forums, part of the General Programming Boards category; Hi all, this is my UDF Code: void str_capitalize(char s[]) { char c; int up = 1; while (s) { ...

  1. #1
    Registered User sbaginov's Avatar
    Join Date
    May 2010
    Location
    Italy
    Posts
    19

    Question str_capitalize UDF problem

    Hi all, this is my UDF
    Code:
    void str_capitalize(char s[]) {
    	char c;
    	int up = 1;
    
    	while (s) {
    		if (up) {
    			if (!isspace(*s)) {
    				c =  toupper(*s);
    				*s = c;
    				up = 0;
    			}
    		} else {
    			if (isspace(*s))
    				up = 1;
    			else
    				*s = tolower(*s);
    		}
    		++s;
    	}
    }
    When c contains the upper case character pointed by s, I cannot assign it back to s because the program ends.
    Might someone please explain me why? What's wrong with that?
    Thanks in advance.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you pass in a constant bit of memory as s, then trying to write to it would cause a crash. (I'm assuming that's what you mean by the program ending, as opposed to the program ending because you didn't give it anything else to do.)

  3. #3
    Registered User sbaginov's Avatar
    Join Date
    May 2010
    Location
    Italy
    Posts
    19
    Quote Originally Posted by tabstop View Post
    If you pass in a constant bit of memory as s, then trying to write to it would cause a crash. (I'm assuming that's what you mean by the program ending, as opposed to the program ending because you didn't give it anything else to do.)
    Well, thx tabstop, after lunch this bit of code works completely unchanged...

    Here follows a version that does not need ctype.h inclusion any more because toupper(), tolower() and isspace() functions have been removed:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void str_capitalize(char[]);
    
    int main() {
        const char author_ok[] = "Sigmund Baginov";
        char author[] = "SIGMUND BAGINOV";
        str_capitalize(author);
        printf("%s is %sas expected\n", author,
                (0 == strcmp(author, author_ok) ? "" : "not "));
    
        return EXIT_SUCCESS;
    }
    
    void str_capitalize(char s[]) {
        int up = 1;
    
        while ('\0' != *s) {
            if (up) {
                if (' ' != *s && '\t' != *s && '\n' != *s) {
                    *s += (*s >= 'a' && *s <= 'z' ? 'A' - 'a' : 0);
                    up = 0;
                }
            } else {
                if (' ' == *s || '\t' == *s || '\n' == *s)
                    up = 1;
                else
                    *s += (*s >= 'A' && *s <= 'Z' ? 'a' - 'A' : 0);
            }
            ++s;
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

Tags for this Thread


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