Thread: strange output

  1. #1
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732

    strange output

    hai everyone, i have been in a small confusion, can any one correct me please. the follwing programe dosn't output more than 3 char
    Code:
    #include<stdio.h>
    char upcase(char []);
    
    int main()
    {
        char str[25];
        
        printf("Enter a string\n?");
        fgets(str,sizeof(str),stdin);
        upcase(str);
        getchar();
    }
    
    char upcase(char *p)
    {
        static int i=0;
        
        if(*(p+i)=='\0')
        return;
        else
        {
             printf("%c",*(p+i)-32);
        return upcase(p+i++);
       
    }    
    }
    can any one tell me whats going worng in the code and please correct me please

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    The problem is that you are incrementing the value of i each call, and incrementing the value of p. This means you are exponentially iterating through the string skipping most of the characters. Either stop incrementing i, or stop incrementing p.

    For instance:
    Code:
    char upcase(char *p)
    {
        static int i=0;
        
        if(*(p+i)=='\0')
    		return;
        else
        {
            printf("%c",*(p+i)-32);
    		i++;
    		return upcase(p);
       
    	}    
    }
    Also keep in mind you are likely to have a '\n' at the end of your string. You don't want to try and turn that into an uppercase letter.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    thax very much bithub i got it right

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Output an array in a textbox
    By Diablo02 in forum C# Programming
    Replies: 5
    Last Post: 10-18-2007, 03:56 AM
  2. Binary Search - Strange Output
    By mike_g in forum C Programming
    Replies: 13
    Last Post: 06-16-2007, 02:55 PM
  3. Connecting input iterator to output iterator
    By QuestionC in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2007, 02:18 AM
  4. Trying to store system(command) Output
    By punxworm in forum C++ Programming
    Replies: 5
    Last Post: 04-20-2005, 06:46 PM
  5. Really strange, unexpected values from allocated variables
    By Jaken Veina in forum Windows Programming
    Replies: 6
    Last Post: 04-16-2005, 05:40 PM