943,865 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3130
  • C RSS
Apr 30th, 2006
0

Help! C program w/Pointer Notation to Convert Text

Expand Post »
Hi - The assignment is to write a C program that prompts the user to enter a line of text up to 69 chars, then convert the text to upper and lower case. The program is required to use pointer notation, not array notation. The program needs to setup a pointer to the text array. I am having a hard time understanding pointers.. below is my code thus far which is not working (it only outputs garbage). Please HELP !! It is due very soon!!!!!

  1.  
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <ctype.h>
  5.  
  6.  
  7. void main(void)
  8. {
  9.  
  10. /* Declare variables*/
  11.  
  12. char text[70], *text_ptr;
  13. int i;
  14.  
  15. text_ptr = text;
  16.  
  17. /* Prompt user for line of text*/
  18.  
  19. printf ("\nEnter a line of text (up to 69 characters):\n");
  20. gets(text);
  21.  
  22.  
  23. /* Convert and output the text in uppercase characters.*/
  24.  
  25. printf ("\nThe line of text in uppercase is:\n");
  26.  
  27. i = 0;
  28. while ( text[i] != '\0')
  29. putchar( toupper[*text_ptr]++]) );
  30.  
  31. /*Convert and output the text in lowercase characters. */
  32.  
  33. printf ("\n\nThe line of text in lowercase is:\n");
  34.  
  35. i = 0;
  36. while ( text[*text_ptr] != '\0')
  37. putchar( tolower(text[(*text_ptr)++]) );
  38.  
  39. getch();
  40.  
  41.  
  42. } // end main
Last edited by Dave Sinkula; Apr 30th, 2006 at 7:05 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tippytoe is offline Offline
5 posts
since Oct 2005
May 1st, 2006
0

Re: Help! C program w/Pointer Notation to Convert Text

> void main(void)
http://faq.cprogramming.com/cgi-bin/...&id=1043284376

> gets(text);
http://faq.cprogramming.com/cgi-bin/...&id=1043284351

Here's a little program to show the same thing using arrays and pointers
  1. #include <stdio.h>
  2. int main ( ) {
  3. char test[] = "this is a test\n";
  4. char *p;
  5. int i;
  6. for ( i = 0 ; test[i] != '\0'; i++ ) putchar(test[i]);
  7. for ( p = test ; *p != '\0'; p++ ) putchar(*p);
  8. return 0;
  9. }
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Simplified Computer(CS): escalonamento algorithms, FCFS, SJP, Round-Robin [was: Help]
Next Thread in C Forum Timeline: bitwise operations in C





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC