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 */