Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
The program I'm trying to get working is supposed to allow the user to enter a sentence in CAPS. Then the program is supposed to print each word of the sentence in lowercase, backwards order on different lines. i.e. if the user enters HELLO WORLD, the output would be:
olleh
dlrowHere's what I have, but it's not working. I'm using microsoft visual C++. ANY feedback would be GREATLY appreciated.
#include
#includevoid prtWords (char *pSentence);
int str_len3 (char *s1);void main ()
{
char *pSentence, choice;
do {
printf ("Enter a sentence: ");
fflush (stdin);
gets (pSentence);
prtWords (pSentence);
/* .. . . */fflush (stdin); /* Flush buffers to standard input */
printf ("\nWould you like to enter another sentence? ");
choice = getchar ();
printf ("\n");
} while (choice !='n' && choice !='N');
getchar ();}
void prtWords (char *pSentence)
{
char tmpstrg [80];
char pT;
int length, n, j, sum = 0;
length = str_len3 (pSentence);for (n=0; n = 0; j--)
{
tmpstrg[j] = *pSentence;
++pSentence;
}
for (j = 0; j = 0; j--)
{
tmpstrg[j] = *pSentence;
++pSentence;
}
for (j = 0; j=length-sum-1; j++)
{
pT = tmpstrg[j];
pT = tolower (pT);
printf ("%c", pT);
}
}int str_len3 (char *s1)
{
char *cp = s1;
while (*cp)
++cp;return (cp - s1);
}

A major problem is that you are passing an uninitialized pointer(pSentence) to gets().
Your declaration of pSentence is local to main() which means that the variable is allocated on the stack and is not initialized. The part of the last sentence that is really important to understand is that the variable is NOT initialized. It has whatever value happens to be in the memory where it is allocated. So when you pass pSentence to gets(), it may very well crash the program if the value of the pSentence pointer points to memory that your program can't access.
You either need to declare pSentence as a char array or allocate some memory for it using malloc/calloc.
char pSentence[100];
or
pSentence = malloc(100);
or
pSentence = calloc(1,100);
Ideally, you should check the value of pSentence after the malloc/calloc call to make sure that it is not NULL (which indicates that the alloc function failed).

for (n=0; n = 0; j--)
{
tmpstrg[j] = *pSentence;
++pSentence;
}This loop appears messed up. Assuming that you meant the middle section to be "n ==0" rather than "n = 0" (there is a major difference), the loop is coded to initialize n to 0, go as long as n is 0, then decrement j after each loop iteration. Since, nowhere in the for statement or inside the loop is n changed, this loop will go forever.
A very good programming practice is to write the program in managable chunks. Many peopel write entire programs or hundreds of lines of code before compiling and/or testing. That makes it very hard to find errors.
For example, if I were to write this program, I would write the main loop first, just to input the string, then output it back again without changing anything. This may mean putting in a little bit of code that isn't 'required' and will be taken out later. For example, there doesn't seem to be a requirement to display the input text. So, you'll end up taking that code out at some point. But I'd usually leave it in for quite a while, just so that I always know that I haven't messed up the input section of code.Code a little bit, then test to make sure that code works (usually by putting in some output statements).
I've been coding for 20 years and I still approach even easy programs this way.

Thanks for replying to my message. It is greatly appreciated. I declared pSentence to an array char pSentence[100]. I looked over what got printed after I posted my original message and things got messed up from what I had intended. The loop wasn't supposed to be n = 0; n = 0; j--
I've been trying to get what I had wanted to post to show up, but it keeps getting messed up once I preview the post. Here's what I had wanted to show up for the loop that you mentioned:
for ( j = end - 1; j >= 0; j--)
{
tmpstrg[j] = *pSentence;
++pSentence;
}

HEre's my latest. It works a little better, but not all inputs work correctly.
void prtWords (char *pSentence);
int str_len3 (char *s1);void main ()
{char pSentence[100], choice;
do {
printf ("Enter a sentence: ");
fflush (stdin);
gets (pSentence);
prtWords (pSentence);
/* .. . . */fflush (stdin); /* Flush buffers to standard input */
printf ("\nWould you like to enter another sentence? ");
choice = getchar ();
printf ("\n");
} while (choice !='n' && choice !='N');
getchar ();}
void prtWords (char *pSentence)
{char tmpstrg [80];
char pT;
int length, n, end, j, sum = 0;
length = str_len3 (pSentence);for (n=0; n = 0; j--)
{
tmpstrg[j] = *pSentence;
++pSentence;
}
for (j = 0; j=length-sum-1; j++)
{
pT = tmpstrg[j];
pT = tolower (pT);
printf ("%c", pT);
}}
int str_len3 (char *s1)
{
char *cp = s1;
while (*cp)
++cp;return (cp - s1);
}

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |