Thread: Converting to lowercase and counting words (pointers)

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    20

    Converting to lowercase and counting words (pointers)

    Hey everybody. I am writing a program when the beginning it you have to pick which step to preform out of a few steps. Once you choose the step. The user is asked to input what is asked and it should preform it. I have completed most of my steps but the one step giving me a hard time is converting a user inputed sentence into all lowercase letters, and then count the words of it. My program starts out like this if you pick this step
    Code:
    else
    if (choice == 3) {
    
                
    /* Gather user's input */
    
    printf("\n You have chosen to convert a sentence to all lowercase letters and count the words\n"
    "Enter the sentence: \t");
     gets ( sentence );
    
    
                
    /* Call the function to convert the whole sentence to lowercase and count the number of words */
    
    lowercase_string ( &sentence);
    countSentence ( &sentence);
     } /* End if statement */
    
    


    this is the easy part in which I presume I did correctly, but now this is where I am getting the problems:

    Code:
     
     
     
    /* Prompts user to enter a sentence and convert it to lowercase and count the number of words */
     
     
    
    void 
     countSentence (char *sentence)
    void 
     lowercase_string (char*)
    {
    printf ("The original string is %s\n", sentence);
     
     lowercase_string (sentence);
    printf("The string in lowercase is \"%s\"\n", sentence);
    }
     void lowercase_string(char *sentence)
    {
    while(*sentence)
     {
            
    if (*sentence>= 'A' && *sentence <= 'Z')
     {
       *sentence = *sentence + 32;
     }
      sentence++;
     }
    }
        
    /*Define new variables*/
    
    char *tokenPtr
    int i;
    int counter = 0;
     {
    
    tokenPtr = strtok( sentence, " \n"); /* Space before new line */
    
    /* continue tokenizing sentence until tokenPtr becomes NULL */
       
    while ( tokenPtr ) 
     
      {
         ++counter;
         tokenPtr = strtok( NULL, " \n" ); /* get the next token */
    
       }  /* end while */
    
    printf("\nThe total number of words is %d\n\n", counter);
    
     } 
    /* End of string


    I want to output the original sentence, then the sentence with all lower case letters, then at last output the number of words in the sentence. I declared all my variables up top, and I am wondering where my mistake is. When trying to compile it, I only get syntax errors which I don't understand. Thanks for looking at this at least, and I would really appreciate some help!!!
     
     
     





  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It is good to see that you posted your code in code tags, but please post it without any extra markup (e.g., your own syntax highlighting, or setting the font colour). When your code is posted as-is within code tags, the forum software will apply its own syntax highlighting and line numbering. Also, post code with proper formatting, in particular, good indentation.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Please post code in plain text format inbetween the code tags. You may have to copy from a plain text program like notepad to remove any special formatting.

    If you need help with your lowercase_string function, just post that code by itself. As it stands, you have a lot of other stuff which is probably not related to your question. If you have syntax errors you also have to tell us what the first error was and on what line it occured.

  4. #4
    Registered User
    Join Date
    Mar 2014
    Posts
    20
    I'm trying to learn how to transfer my code from Microsoft onto here the way I see it, but that seems to be yet another one of my problems. Any suggestions though?

  5. #5
    Registered User
    Join Date
    Mar 2014
    Posts
    20
    *****My code gets its input from this step.
    Code:
    else if (choice == 3) {
            /*Gather user's input */
    
                printf("\nYou have chosen to convert a sentence to all lowercase letters and count thewords\n"
                            "Enter the sentence: \t");
                              gets( sentence );
    
            /*Call the function to convert the whole sentence to lowercase and count thenumber of words */
    
             lowercase_string( &sentence);
              countSentence( &sentence);
    
     }/* End if statement */
    
    ****This Is the part in which Im getting trouble
    /* Prompts user to enter a sentence and convert it tolowercase and count the number of words */
    void countSentence (char *sentence)
    void lowercase_string (char*)
    
    {
    printf ("The original string is %s\n", sentence);
    
    lowercase_string (sentence);
    
    printf("The string in lowercase is\"%s\"\n", sentence);
    
    }
    
    void lowercase_string(char *sentence)
    {
                    while(*sentence)
                    {
                                    if(*sentence>= 'A' && *sentence <= 'Z')
                                    {
                                                    *sentence= *sentence + 32;
                                    }
                                    sentence++;
                    }
    }
    
                    /*Definenew variables*/
                                    char *tokenPtr;                    // Saying syntax error                                
     int i;
                                    intcounter = 0;
                    {
    
                                    tokenPtr= strtok( sentence, " \n"); /* Space before new line */
    
                                    /*continue tokenizing sentence until tokenPtr becomes NULL */
                                    while( tokenPtr ) 
                                    {
                                                    ++counter;
                                                    tokenPtr = strtok( NULL, "\n" ); /* get the next token */ Saying syntax error
                                    }/* end while */
    
                    printf("\nThetotal number of words is %d\n\n", counter);
    
                    } /* End of sentence*/Saying Fatal error unexpected end
    
    Last edited by Steveqb14; 04-12-2014 at 09:23 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Steveqb14
    I'm trying to learn how to transfer my code from Microsoft onto here the way I see it, but that seems to be yet another one of my problems. Any suggestions though?
    I presume that by "Microsoft" you mean "Microsoft Word", in which case there is a potential problem there: a word processor is usually not an appropriate tool for programming as programming source code should be as-is, not littered with word processor specific markup and such. Rather, use an IDE or a text editor suited to programming.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Mar 2014
    Posts
    20
    Sorry for the misunderstanding I am using Microsoft Visual Studio 2010 for this program!

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Err... in that case just copy and paste (CTRL+A, CTRL+C, CTRL+V in that sequence could well work) and post the code in [code][/code] bbcode tags.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Mar 2014
    Posts
    20
    I did that and it posted the same way as the first one. I have attached my full program!
    Attached Files Attached Files

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting lowercase letters in string
    By askinne2 in forum C Programming
    Replies: 14
    Last Post: 09-29-2010, 02:58 PM
  2. Counting uppercase and lowercase letters in a text
    By Tintu in forum C Programming
    Replies: 2
    Last Post: 02-06-2008, 10:15 PM
  3. Counting number of lowercase.[NEWBIE]
    By gtr_s15 in forum C++ Programming
    Replies: 18
    Last Post: 11-30-2005, 11:00 PM
  4. converting strings to lowercase
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 05-10-2002, 01:03 PM
  5. converting chars to uppercase/lowercase
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 05-08-2002, 07:55 PM