Thread: array char with pointers

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    123

    array char with pointers

    write down this code:

    Code:
    #include <ctype.h>
    #include <stdio.h>
    #define N 100
    
    void main()
    {
    	char string[N], *output;
    	puts ("\nenter a char");
    
    	for (output=gets(string);*output;output++)
    	{
    		if (isalpha(*output))
    		{
    		      if (islower(*output))
    			*output=toupper(*output);
    		      if (isupper(*output))
    			*output=tolower(*output);
    		}
    	}
    
    
    puts (output);
    printf ("\n");
    }
    the thing is I don't get anything in output. debugger shows output is empty at the end of program.
    what did I miss?

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    You leave the loop when the pointer output is pointing to the string terminating '\0'.

    So the argument to puts is pointing to '\0', and puts doesn't put anything.

    Regards,

    Dave

  3. #3
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Smile Ofcourse It wont Work

    Of course It wont work.....

    1. Change void main() to int main() before Salem sees it...

    2. Add a
    Code:
    return 0;
    3. What you are doing is:

    Start with pointer pointing to first char.
    See if it is a char.
    If it is, check if it is a lower case char
    if it is, change it to Upper case
    Now see if it is Upper case
    If it is, change it to lower case
    Advance the pointer to point to the next char
    Continue till end of string.

    Now, do you read it>??? After the entire loop is over.... Your pointer will be pointing to the end of the string and all the characters entered in lower case remain to be in lower case....... So, no output...
    Help everyone you can

  4. #4
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Thumbs up

    a minor correction is:
    Code:
    #include <ctype.h>
    #include <stdio.h>
    #define N 100
    
    int main()
    {
    	char string[N], *output;
    	puts ("\nenter a char");
    
    	for (output=gets(string);*output;output++)
    	{
    		if (isalpha(*output))
    		{
    		      if (islower(*output))
                          {
    			   *output=toupper(*output);
                           }
                          else
                           {
            		      if (isupper(*output))
    		                	*output=tolower(*output);
                           }
    		}
    	}
            puts (string);
            printf ("\n");
            return 0;
    }
    Hint: Avoid puts and gets as much as possible...

    -Harsha
    Help everyone you can

  5. #5
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Hint: Avoid puts and gets as much as possible...
    what's wrong with puts?
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  6. #6
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192
    You cant use it to print anything but strings. I mean not as a substitute for printf (with exceptions ofcourse, like when there is nothing more to print than strings and you need a \n at the end by default).
    Ok, maybe its not bad afterall.

    -Harsha.
    Help everyone you can

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I don't really know why you would, but you could replace:
    Code:
    		      if (islower(*output))
                          {
    			   *output=toupper(*output);
                           }
                          else
                           {
            		      if (isupper(*output))
    		                	*output=tolower(*output);
    with:
    Code:
    *output = (*output & 0xDF) | (~(*output & 0x20) & 0x20);
    I don't know...just thought it was interesting
    If you understand what you're doing, you're not learning anything.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >replace [...] with [...]
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
       const char text[] = "Hello World 125!", *ptr;
       puts("A:");
       for ( ptr = text; *ptr; ++ptr )
       {
          int result = (*ptr & 0xDF) | (~(*ptr & 0x20) & 0x20);
          printf(isprint(result) ? "%c" : "<%d>", result);
       }
       puts("\nB:");
       for ( ptr = text; *ptr; ++ptr )
       {
          int result = islower(*ptr) ? toupper(*ptr) :
                       isupper(*ptr) ? tolower(*ptr) : *ptr;
          printf(isprint(result) ? "%c" : "<%d>", result);
       }
       putchar('\n');
       return 0;
    }
    
    /* my output
    A:
    hELLO<0>wORLD<0><17><18><21><1>
    B:
    hELLO wORLD 125!
    */
    Last edited by Dave_Sinkula; 08-26-2004 at 01:48 PM.
    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.*

  9. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    maybe it is my disability, but I couldn't get anything productive from all the things been writen.
    Please be more specific & positive of what should I do to fix it; not only what not to do...

    TIA

    Ronen

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Dave_Sinkula
    >replace [...] with [...]
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
       const char text[] = "Hello World 125!", *ptr;
       puts("A:");
       for ( ptr = text; *ptr; ++ptr )
       {
          int result = (*ptr & 0xDF) | (~(*ptr & 0x20) & 0x20);
          printf(isprint(result) ? "%c" : "<%d>", result);
       }
       puts("\nB:");
       for ( ptr = text; *ptr; ++ptr )
       {
          int result = islower(*ptr) ? toupper(*ptr) :
                       isupper(*ptr) ? tolower(*ptr) : *ptr;
          printf(isprint(result) ? "%c" : "<%d>", result);
       }
       putchar('\n');
       return 0;
    }
    
    /* my output
    A:
    hELLO<0>wORLD<0><17><18><21><1>
    B:
    hELLO wORLD 125!
    */
    That's because you didn't follow directions. You should have left the isalpha() check in there. I didn't say to replace that :P
    If you understand what you're doing, you're not learning anything.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to learn How to ask questions the smart way. Don't just slap some code up and say "write this down..." and not tell us what it's supposed to do. Whatever happened to actually telling people what the problem is so you can get some help?

    You don't just walk into the doctor and say "fix me" without telling them what's wrong, do you? You don't take your car into the shop and just say "fix it", without any sort of hint of what you want fixed, do you? Well, the same goes for posting here. Don't just throw some code out there and say "what's wrong"? YOU TELL ME.

    What is it supposed to do? What doesn't it do that it should? What does it do that it shouldn't? Answer all of that, then perhaps you'll get some help, because I don't even waste my time reading arbitrary code. If you haven't spent the time telling me about the program, why should I spend time reading it to figure it out for you?

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by itsme86
    That's because you didn't follow directions. You should have left the isalpha() check in there. I didn't say to replace that :P
    Actually you did. You gave a block of code and said to replace it with another block of code. If the conditional was suppose to be left in you should have left it out of block 1 or put it in block 2

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I did leave it out of block 1. Look and you'll see no reference to the isalpha() check in the original code that I said to replace, therefore it should be left there. Like I said, directions weren't followed correctly.
    If you understand what you're doing, you're not learning anything.

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by itsme86
    That's because you didn't follow directions. You should have left the isalpha() check in there. I didn't say to replace that :P
    My bad (I usually stop reading a post after I encounter gets, fflush(stdin), etc.).

    I prefer the standard library functions -- they will be correct and possibly faster.
    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.*

  15. #15
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Opps saw islower() and read isalpha()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  2. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM