Thread: String Conversion

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    68

    String Conversion

    Im having a bit of trouble converting a string to lower case then to upper case.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
    	
    	char buf[80], *ptr, *string;
    	
    	while (1)
    	{
    		
    		puts("Enter a line of text, a blank to exit.");
    		fgets(buf, sizeof buf, stdin);
    		
    		if ((ptr=strchr(buf,'\n'))!=NULL)
    			*ptr='\0';
    		
    		if (strlen(buf)==0)
    			break;
    		
    		string = buf;
    		
    		tolower(string);
    		puts(buf);
    		toupper(string);
    		puts(buf);
    		
    	}
    	
    	return 0;
    	
    }
    The only error is
    upper.c:32:2: warning: no newline at end of file
    Line 32 is the last brace.

    [edit]
    If its any help I am using gcc
    [/edit]

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    It's nothing big. Just go to line 32 and press 'Enter'. I don't know why that causes a problem...

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    68
    Ok that stopped the error however the strings arent being converted say if I typed testing

    the first show is

    testing - which is correct

    then for upper case
    testing - which is suppose to be TESTING

  4. #4
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Code:
    for (int i = 0; buf[i] != '\0'; i++) {
        buf[i] = toupper(buf[i]);
    }

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    68
    Thanks Brian that worked a charm. So from viewing that toupper and tolower only take one character at a time?

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Correct.

  7. #7
    Quote Originally Posted by gsoft
    Im having a bit of trouble converting a string to lower case then to upper case.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
    	
    	char buf[80], *ptr, *string;
    	
    	while (1)
    	{
    		
    		puts("Enter a line of text, a blank to exit.");
    		fgets(buf, sizeof buf, stdin);
    		
    		if ((ptr=strchr(buf,'\n'))!=NULL)
    			*ptr='\0';
    		
    		if (strlen(buf)==0)
    			break;
    		
    		string = buf;
    		
    		tolower(string);
    		puts(buf);
    		toupper(string);
    		puts(buf);
    		
    	}
    	
    	return 0;
    	
    }
    If its any help I am using gcc
    Clearly your gcc calling is too lax. Add this:
    Code:
    -W  -Wall  -ansi  -O2
    and you will get that:
    Code:
    main.c: In function `main':
    main.c:23: warning: implicit declaration of function `tolower'
    main.c:25: warning: implicit declaration of function `toupper'
    That denotes the lack of prototypes for the quoted functions. Actually, an include of <ctype.h> is missing.

    Once fixed, the things become more obvious:
    Code:
    main.c: In function `main':
    main.c:24: warning: passing arg 1 of `tolower' makes integer from pointer without a cast
    main.c:26: warning: passing arg 1 of `toupper' makes integer from pointer without a cast
    Of course, your code is wrong, because you passed an address to an int parameter. The tolower()/toupper() functions only process one character, as written into your C-book...

    Lesson of the day
    - Teach your compiler to tell the truth
    - Fix your code the right way until you have no errors or warnings (typecast is not the right way).
    - Read you manual before using a new function.

    A working solution have been given in this thread.
    Emmanuel Delahaye

    "C is a sharp tool"

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    68
    Hmm Ok i guess I learnt something about my Compiler thanks. Ill go over my other small programs ive made to see if they work.

  9. #9
    Registered User
    Join Date
    Jul 2004
    Posts
    68
    Ok may I ask one question why cannot I not use

    // for comments ? gcc says
    error: syntax error before '/' token
    However when I change it to /* */ it works, I thought // was added in the new C99 version of ANSI C.

  10. #10
    Quote Originally Posted by gsoft
    Ok may I ask one question why cannot I not use

    // for comments ? gcc says
    However when I change it to /* */ it works, I thought // was added in the new C99 version of ANSI C.
    Yes, but your compiler is probably working in C90 mode. You can try to replace -ansi by -std=c99, but I don't recommend it because (debatable):

    - It's ugly
    - It trends to add comments at the end of the lines that makes the code unreadable and unpritable without wrapping (ugly too).
    - Most of the compilers are still C90 and don't support the C99 comments, specially on embedded systems (the playyard of the C-language)
    Emmanuel Delahaye

    "C is a sharp tool"

  11. #11
    Registered User
    Join Date
    Jul 2004
    Posts
    68
    Thank you very much you have been a terrific help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM