Thread: total beginner!!!

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    5

    total beginner!!!

    Hi there,

    could someone help me please, Ive just stared with c and I want the function to accept upto 50 characters but finish when i hit return and then to change the case of them.

    It continues to accept characters after i have hit return up until the array of 50 is finished.

    My next problem is that it seems to loop outside the loop and repeat the whole process again from the initial printf statement.

    any suggestions would be great.

    MAX_LINE_LENGTH = 50
    Code:
    int inverseCharacters()
    {
    
    	char ch[MAX_LINE_LENGTH];
    	int i= 0;
    
    	printf("%s", "\nEnter a line of text combining upper and lower case: \n");
    	//scanf("%s", &ch);
    
    
    	while (i <= MAX_LINE_LENGTH)
    	{
    		ch[i]= getchar();
    
    		if(islower(ch[i]))
    		{
    			ch[i] = toupper(ch[i]);
    			putchar(ch[i]);
    
    		}
    		else
    		{
    			ch[i] = tolower(ch[i]);
    			putchar(ch[i]);
    		}
    		i++;
    	}
    
     	return 0;
    }

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Read this to learn about <return> and getchar().

    If you are seeing the initial printf() more than once, then inverseCharacters() is being called more than once.

    gg

  3. #3
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Hmmm, few comments.
    >>//scanf("%s", &ch);
    I think you meant without the & operator.
    Also, for getting string from a stream, use fgets().

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >while (i <= MAX_LINE_LENGTH)

    Here you are getting MAX_LINE_LENGTH+1 characters instead of MAX_LINE_LENGTH, because you are starting from i = 0.

  5. #5
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    You don't really need all of that coding to convert a string to either all upper or all lower case, and reading it in as opposed to what I did would not be significantly different.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main()
    {
       int i;   
       char ch[] = "aBcDeFgHiJk";
       
       printf("%s\n", ch);
       
       for(i=0; (ch[i] = toupper(ch[i])) != '\0'; i++)
          putchar(ch[i]);
       
       putchar('\n');   
       
       for(i=0; (ch[i] = tolower(ch[i])) != '\0'; i++)
          putchar(ch[i]);   
       
       return 0;
    }
    Last edited by ronin; 03-29-2003 at 01:30 PM.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

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

    help!!!

    I've sorted out where it was calling the function the second time, thanx.

    I still cant seem to work out how to get my code to accept less than the maximum amount of characters [50] one line. eg. i input 10 characters then hit <return>, it inverses those, but will then continue to receive input for the remaining [50] spaces which i DON'T want. I only want the 1 line of text to be accepted and inversed.

    Code:
    int inverseCharacters()
    {
    
    	char ch[MAX_LINE_LENGTH];
    	int i= 0;
    
    	printf("%s", "\nEnter a line of text combining upper and lower case: \n");
    
    
    	while (i < MAX_LINE_LENGTH)//while_2
    		{
    
    			ch[i]= getchar();
    			while (ch != '\n')//while_1
    				{
    				if(islower(ch[i]))
    				{
    					ch[i] = toupper(ch[i]);
    					putchar(ch[i]);
    
    				}//endif
    				else
    				{
    					ch[i] = tolower(ch[i]);
    					putchar(ch[i]);
    				}//endifelse
    
    				i++;
    			}//endwhile_1
    		}//endwhile_2
    	printf("%s", "\nPress enter to return to the menu: \n");
    	//menu();
     	return 0;
    }
    I have tried "while (ch != '\n')" but that just gave me an error 'operands of != have illegal types 'pointer to char' and 'int'

    Any help greatly appreciated!!

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    73
    Just a little info:

    char c[50];

    that only holds a 49 character string. Don't forget the null byte at the end of an array! ('\0') If you want 50, you need an array of 51. (correct me if I'm wrong here.)

  8. #8
    Registered User
    Join Date
    Mar 2003
    Posts
    5

    I finally got it!!

    Thanks for your replys, appreciate it. I finally ended up using


    [code]

    fgets(ch, MAX_LINE, stdin);

    for(i = 0; i < MAX_LINE, ch[i] !='\0'; i++)
    {
    if(islower(ch[i]))
    {
    ...
    }
    else
    {
    ...
    }//end_if_else

    putchar(ch[i]);
    }
    [\code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with day of the week program
    By Punkakitty in forum C++ Programming
    Replies: 10
    Last Post: 01-14-2009, 06:55 PM
  2. Replies: 8
    Last Post: 11-03-2008, 09:48 PM
  3. Total beginner questions
    By Sparky in forum C Programming
    Replies: 5
    Last Post: 06-22-2008, 04:50 AM
  4. long problem
    By loko in forum C Programming
    Replies: 28
    Last Post: 07-22-2005, 09:38 AM
  5. Replies: 4
    Last Post: 04-22-2003, 12:52 PM