Thread: Can anyone figure this out?

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    3

    Can anyone figure this out?

    My program is supposed to allow the user to enter a sentence in CAPS. Then the program will change the letters to lowercase and print each of the words backwards on separate lines. For example, if the user entered HELLO WORLD, the output would be:
    olleh
    dlrow

    My code has a problem in that for some sentences, two words get printed on one line. For example, if I enter HELLO MY NAME IS JOHN, the output is:
    olleh
    eman ym
    nhoj si

    I'd be very grateful to anyone who knows how to fix this problem!

    Here's my code:

    Code:
    / * This program will use character strings, functions, and pointer notation
     * to display each word of a sentence in lower-case, reverse order on
     * separate lines.
     */
    
    #include <stdio.h>
    #include <ctype.h>
    
    void prtWords (char *pSentence);
    int str_len (char *s1);
    
    void main (void)
    {
    	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_len (pSentence);
    	
    	for (n=0; n<length; n++)
    	{
    		if (isspace (pSentence[n]))
    		{
    			end = n;
    			sum = sum + n;
    			for ( j = end - 1; j >= 0; j--)
    			{
    				tmpstrg[j] = *pSentence;
    				++pSentence;
    			}
    			for (j = 0; j<=end-1; j++)
    			{
    				pT = tmpstrg[j];
    				pT = tolower (pT);
    				printf ("%c", pT);
    			}
    			printf ("\n");
    			++pSentence;
    			sum++;
    		}
    	}
    	for ( j = length - sum -1; 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_len (char *s1)
    {
    	char *cp = s1;
    	while (*cp)
    		++cp;
    	return (cp - s1);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    void main (void)

    This is wrong. Main always returns an int. Nothing else, ever.


    fflush (stdin);

    This is wrong. You cannot flush input steams. It is undefined behaviour.


    gets (pSentence);

    This is wrong. You should never use gets. It has no boundary checking and will crash your program if used (by the user) to input more than you expect.


    int str_len (char *s1)

    Why? Why don't you just use strlen?


    You should split your functions up.
    Make one that put a '\n' between each word.
    Make another that lower cases the entire string.
    Finally, make one that prints them in reverse order.

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

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    3
    quzah--Thanks for replying. I have to do all the things I did because that's what the teacher is requiring. She didn't show us strlen, so if I use it, she'll likely think that someone else wrote my program. We have to use gets () to allow the user to enter a sentence; tmpStrg which is a local temporary string to hold each word from the sentence; and pT which is a local character pointer to traverse tmpStrg. The teacher likes to make things more complicated than they should be. Got any more suggestions? I'm still stuck.

  4. #4
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Dude, change what Quzah said, about main(), gets().
    Instead getting string with gets(), use fgets() and see in the man page the 'bugs' session, to understand you shouldn you use gets(), fix your code, and post your new version.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    3
    Okay, I'm now using fgets () and int main ()

    This is my new version with those changes but I still have the same problem arises with two words getting printed on the same line. Thanks for your help guys.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    void prtWords (char *pSentence);
    int str_len3 (char *s1);
    
    int main ()
    {
    	char pSentence[100], choice;
    	do {
    		printf ("Enter a sentence: ");
    		fflush (stdin);
    		fgets (pSentence, 100, stdin);
    		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<length; n++)
    	{
    		if (isspace (pSentence[n]))
    		{
    			end = n;
    			sum = sum + n;
    			for ( j = end - 1; j >= 0; j--)
    			{
    				tmpstrg[j] = *pSentence;
    				++pSentence;
    			}
    			for (j = 0; j<=end-1; j++)
    			{
    				pT = tmpstrg[j];
    				pT = tolower (pT);
    				printf ("%c", pT);
    			}
    			printf ("\n");
    			++pSentence;
    			sum++;
    		}
    	}
    	for ( j = length - sum -1; 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);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Figure out how method was called?
    By jcafaro10 in forum C++ Programming
    Replies: 2
    Last Post: 02-07-2009, 10:43 AM
  2. 3 dimensional figure volume
    By thekautz in forum C++ Programming
    Replies: 2
    Last Post: 01-20-2009, 05:22 PM
  3. trying to figure out someone's code for a plugin
    By paulpars in forum C++ Programming
    Replies: 4
    Last Post: 07-20-2006, 10:57 AM
  4. newb to C, cant figure something out.
    By Phate4219 in forum C Programming
    Replies: 16
    Last Post: 03-06-2006, 01:47 AM
  5. ahh i can't figure out this loop
    By blindleaf in forum C Programming
    Replies: 1
    Last Post: 03-18-2003, 09:42 AM