C Program Pointers Help

This is a discussion on C Program Pointers Help within the C Programming forums, part of the General Programming Boards category; Hello, I've been working with this code for about a week and I'm totally stuck right now. I've been through ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Location
    Boston
    Posts
    7

    C Program Pointers Help

    Hello,
    I've been working with this code for about a week and I'm totally stuck right now. I've been through my notes, book, and countless online articles trying to figure this out. I'm trying to get the pointers in my program to work. When I run each output individually, meaning I comment out the other 2, they work fine but I can't seem to get them to work together. I'm sure I'm creating the pointers incorrectly or I'm setting up the loop wrong or both. I was wondering if someone could please take a look at my code and let me know what I'm doing wrong, and please explain it as simply as possible because I'm at my wits end trying to understand pointers, and possibly show me how to fix it? I have included the code and a screen shot of the output.

    Thank you, in advance, for any help that you can provide!

    P.S. I know my code may not be the best way to program but my professor is very particular about how we write our programs. We cannot use anything in our code that we have not covered in class and we cannot deviate from the way he teaches us.

    Here is the assignment:

    Write a C program that prompts the user to enter a line of text (up to 50 characters). It should then convert the text entered to uppercase letters and then to lowercase letters, and then to sentence case.

    Your program should use a character array: text[51]. It should use the safer_gets function to prompt the user for the line of text.

    Your program must use pointer notation.


    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    void safer_gets (char array[], int max_chars);
    
    int main(void)
    
    {
    
          /* Declare variables */
          /* ----------------- */
    
    	   char  text[51];
           char *text_ptr = text;
           int   i, up;
    
          /* Prompt user for line of text */
          /* ---------------------------- */
    
          printf ("Enter a line of text (up to 50 characters):\n");
          safer_gets( text, 51 );
    
    
          /* Convert and output the text in uppercase characters. */
          /* ---------------------------------------------------- */
    
          printf ("\nThe line of text in uppercase is:\n");
          i = 0;
          while ( *text_ptr!='\0' )            
              putchar( toupper(text_ptr[i++]) );
    
     
    
          /* Convert and output the text in lowercase characters. */
          /* ---------------------------------------------------- */
    
          printf ("\n\nThe line of text in lowercase is:\n");
          i = 0;
          while ( *text_ptr!='\0' )            
                putchar( tolower(text_ptr[i++]) );
          printf ("\n"); 
          
          
          /* Convert and output the text in sentence case. */
          /* ---------------------------------------------------- */
    	  	  
    	  printf ("\nThe line of text in sentence case is:\n");
    	  i = 0;
    	  up = 1;
    	  while ( *text_ptr!='\0' )     
          {
    	  	if (!up)
    	    if (text[i-1]==' ' && toupper(text[i])=='I' &&  text[i+1]==' ')
     		up = 1;   /* capitalize i if all alone */
       		if(up)
     		if     (text[i] != ' ')
     		{
       			 putchar(toupper(text_ptr[i++]));
        		up = 0;
     		}/* end if */
     		else
     		  putchar(tolower(text[i++]));
      		else
            {
              putchar(tolower(text[i]));
            if (text[i] == '?' || text[i] == '.' || text[i] == '!')
            up = 1;
            i++;
            }/* end else*/
          } /* end while*/
        
                
    
     	return 0;	
    	getchar();
    
    } /* end main */
    
    /* Function safer_gets */
    /* ******************* */
    void safer_gets (char array[], int max_chars)
    {
     int i;
     
     for (i = 0; i < max_chars; i++)
     {
        array[i] = getchar();
       
        if (array[i] == '\n')
        {
          array[i] = '\0';
          break;
        }
      } /* end for */
      
      if (i == max_chars )
        if (array[i-1] != '\0')
          while (getchar() != '\n');
     
      array[max_chars-1] = '\0';
    } /* end safer_gets */
    Attached Images Attached Images  

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    	  while ( *text_ptr!='\0' )
    text_ptr never changes, so you just run off the end of your array until your program feels like crashing. Either use i as an index for text_ptr[] or increment text_ptr and use it instead of what you are doing now.


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

  3. #3
    Registered User
    Join Date
    Feb 2011
    Location
    Boston
    Posts
    7
    Thanks Quzah,
    I will give it a try and see what happens. Thanks for taking a look.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Payroll Pointers program
    By tiger6425 in forum C Programming
    Replies: 1
    Last Post: 08-24-2010, 03:59 PM
  2. need some pointers on program
    By noob2c in forum C++ Programming
    Replies: 7
    Last Post: 12-14-2003, 04:16 AM
  3. Replies: 2
    Last Post: 03-13-2003, 08:40 AM
  4. Help In Visualising Pointers ( C Program )
    By Evilelmo in forum C Programming
    Replies: 5
    Last Post: 01-25-2003, 12:48 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21