basic c++ question

Discussion in 'OT Technology' started by Shayes, Feb 12, 2010.

  1. Shayes

    Shayes OT Supporter

    Joined:
    Aug 9, 2008
    Messages:
    42,641
    Likes Received:
    1
    Location:
    Knoxville, TN
    ima noob.
    so i need to write a program that converts a user input string to
    1. all upper case letters
    2. all lower case letters
    3. the string in reverse form
    4. counts the number of vowels in the string
    5. counts the number of consonants in the string

    any help is appreciated


    so i found out about toupper(), tolower(), isupper(), islower(). so i guess i will be using those, but i just dont know how to approach this.
     
  2. Shayes

    Shayes OT Supporter

    Joined:
    Aug 9, 2008
    Messages:
    42,641
    Likes Received:
    1
    Location:
    Knoxville, TN
    so what i am having trouble with is:

    these functions i named cannot be used with full strings, correct?

    do i have to use them towards each character in the string? and how?
     
  3. DouggieJ

    DouggieJ OT Supporter

    Joined:
    Nov 1, 2007
    Messages:
    8,126
    Likes Received:
    0
    Location:
    New York, NY USA
    Code:
    char string[] ="Hello World";
    
    for (int i = 0; string[i]; i++)
      string[i] = toupper(string[i]);
    
    cout << string << "\n";
    
    Will yield "HELLO WORLD"

    HTH
     
  4. Shayes

    Shayes OT Supporter

    Joined:
    Aug 9, 2008
    Messages:
    42,641
    Likes Received:
    1
    Location:
    Knoxville, TN
    how do i incorporate user input into this?
     
  5. Shayes

    Shayes OT Supporter

    Joined:
    Aug 9, 2008
    Messages:
    42,641
    Likes Received:
    1
    Location:
    Knoxville, TN
    and can you explain what you are doing inside the for() ?
     
  6. Wonderboy

    Wonderboy New Member

    Joined:
    Aug 31, 2004
    Messages:
    18,615
    Likes Received:
    0
    Location:
    Toronto, Ont
    for (int i = 0; string; i++)
    string = toupper(string);
    do you know what for loops do? if you do, you should see that that loop goes through each letter in that string and converts it to upper case with the toupper() function...

    for input you use the same loop but you need to you "cin" for reading command line input.. google it and you will find help
     
  7. DouggieJ

    DouggieJ OT Supporter

    Joined:
    Nov 1, 2007
    Messages:
    8,126
    Likes Received:
    0
    Location:
    New York, NY USA
    You can use cin or scanf or gets -- are you supposed to be using C++ Object strings or regular C strings?

    The for() loop iterates through each character in the string and makes it upper case.
     
  8. CodeX

    CodeX Guest

    That is clever, terminating the loop with the null terminator of the string... never thought of that :cool:

    (you're trying to confuse the TS, aren't you?) :mamoru:
     
  9. Shayes

    Shayes OT Supporter

    Joined:
    Aug 9, 2008
    Messages:
    42,641
    Likes Received:
    1
    Location:
    Knoxville, TN
    so i took the code from above and added this in to add input. but it resulted in an error
    C2133 'string' : unknown size
    Code:
    char string[];
    cin >> string;
    for (int i = 0; string[i]; i++)
      string[i] = toupper(string[i]);
    cout << string << "\n";
    
     
  10. CodeX

    CodeX Guest

    You have to give your array a length, make it some large upper bound like 256 or 1024 or something. Powers of 2 are always yummy.
     
  11. Shayes

    Shayes OT Supporter

    Joined:
    Aug 9, 2008
    Messages:
    42,641
    Likes Received:
    1
    Location:
    Knoxville, TN
    ahh thanks. but when i run and input something, like "hello world", it only caps a returns the first word. how do i make it scan the whole string?
     
  12. Shayes

    Shayes OT Supporter

    Joined:
    Aug 9, 2008
    Messages:
    42,641
    Likes Received:
    1
    Location:
    Knoxville, TN
    so i tried to use getline(), but it wont work. what are the three arguments i place inside the () ?
     
  13. Shayes

    Shayes OT Supporter

    Joined:
    Aug 9, 2008
    Messages:
    42,641
    Likes Received:
    1
    Location:
    Knoxville, TN
    so i think i got it to work. thanks everyone... for now
     
  14. Shayes

    Shayes OT Supporter

    Joined:
    Aug 9, 2008
    Messages:
    42,641
    Likes Received:
    1
    Location:
    Knoxville, TN
    ok so i need to do what you guys did for me above, but i need to make it lowercase. so what i am having problems with is that i already used the user input string variable in the first loop. how can i reuse that variable through every loop i create.
     
  15. Shayes

    Shayes OT Supporter

    Joined:
    Aug 9, 2008
    Messages:
    42,641
    Likes Received:
    1
    Location:
    Knoxville, TN
    Code:
    char string[];
    cin >> string;
    for (int i = 0; string[i]; i++)
      string[i] = toupper(string[i]);
    cout << string << "\n";
    
    how can i add another loop in here and use the same original variable

    Code:
    char string[];
    cin >> string;
    for (int i = 0; string[i]; i++)
      upperstring[i] = toupper(string[i]);
    for (int i = 0; string[i]; i++)
      lowerstring[i] = tolower(string[i]);
    cout << upperstring << "\n";
    cout << lowerstring << "\n";
    
    how can i make this work?
     
  16. mobbarley

    mobbarley Active Member

    Joined:
    Mar 4, 2005
    Messages:
    9,256
    Likes Received:
    2
    Location:
    Sydney
    try:
    Code:
    int i;
    char string[];
    cin >> string;
    for (i = 0; string[i]; i++)
      upperstring[i] = toupper(string[i]);
    for (i = 0; string[i]; i++)
      lowerstring[i] = tolower(string[i]);
    cout << upperstring << "\n";
    cout << lowerstring << "\n";
    
    I guess you should start with a C++ tutorial, particularly the sections on variables, arrays, control structures (for loops)
     
  17. Shayes

    Shayes OT Supporter

    Joined:
    Aug 9, 2008
    Messages:
    42,641
    Likes Received:
    1
    Location:
    Knoxville, TN
    we havent gone over arrays yet. also, i am using getline instead of cin because cin only copies over the first word.
     
  18. Fase

    Fase Your Face, In A Pickle Jar.

    Joined:
    Apr 6, 2004
    Messages:
    29,540
    Likes Received:
    0
    Location:
    Windsor, Ont, Canada.
    .NET

    string.ToUpper()
    string.ToLower()

    :mamoru:
     
  19. mobbarley

    mobbarley Active Member

    Joined:
    Mar 4, 2005
    Messages:
    9,256
    Likes Received:
    2
    Location:
    Sydney
    and if you look at the underlying implementation you will see something similar to the c++ :ugh:
     
  20. CodeX

    CodeX Guest

    And if you look under that you'll see the same machine code no matter what language or library you use :mamoru:
     
  21. DouggieJ

    DouggieJ OT Supporter

    Joined:
    Nov 1, 2007
    Messages:
    8,126
    Likes Received:
    0
    Location:
    New York, NY USA
    I was bored and couldn't sleep so I think I pretty much completed this for you. This compiles and seems to work using g++ on Mac OS X.

    I haven't tested it a whole lot but it appears to be pretty much OK.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    void runloop (void);
    void getString (string&);
    void printUpperCase (const char *);
    void printLowerCase (const char *); 
    void printInReverse (const char *);
    void printNumberOfVowels (const char *);
    void printNumberOfConsonants (const char *);
    
    int main (int argc, char * argv[]) {
       runloop();
       cout << "Goodbye" << endl;
       return (0);
    }
    
    void runloop (void) {
      string theString;
      string choice;
    
    
      theString = "";
    
      while (true) {
          cout << "Current string: " << theString << endl;
          cout << "Please choose an operation by entering the operation number" << endl << endl;
          cout << "1. Enter a string" << endl;
          cout << "2. Print in all caps" << endl;
          cout << "3. Print in all lowercase" << endl;
          cout << "4. Print in reverse" << endl;
          cout << "5. Print number of vowels" << endl;
          cout << "6. Print number of consonants" << endl;
          cout << "7. Quit" << endl;
    
          cout << "Enter choice: ";
          getline(cin, choice);
          cout << endl;
          if (choice.length() != 1)
              choice = "0";
          switch (choice.c_str()[0]) { 
                case ('1'):
                    getString(theString);
                    break;
                case ('2'): 
                    printUpperCase(theString.c_str());
                    break;
                case ('3'):
                    printLowerCase(theString.c_str());
                    break;
                case ('4'):
                    printInReverse(theString.c_str());
                    break;
                case ('5'):
                    printNumberOfVowels(theString.c_str());
                    break;
                case ('6'):
                    printNumberOfConsonants(theString.c_str());
                    break;
                case ('7'):
                    return;
                default:
                    cout << "Undefined option" << endl;
                }
           cout << endl << endl;
         }
    }
    
    
    void getString (string& myString) {
       cout << "Enter a string: ";
       getline (cin, myString);
    }
    
    void printUpperCase (const char *string) {
       
         char upperstring[strlen(string)];
         long i;
         for (i = 0; string[i]; i++)
             upperstring[i] = toupper(string[i]);
    
         upperstring[i] = 0;
    
         cout << upperstring << endl;
    }
    
    void printLowerCase (const char *string) {
       
         char lowerstring[strlen(string)];
         long i;
         for (i = 0; string[i]; i++)
             lowerstring[i] = tolower(string[i]);
    
         lowerstring[i] = 0;
    
         cout << lowerstring << endl;
    }
    
    void printInReverse(const char *string) {
       
       for (long i = strlen(string) - 1; i >= 0; i--) 
            cout << string[i];
       cout << endl;
    }
    
    
    void printNumberOfVowels (const char *string) {
    
       long vowels = 0;
    
       for (long i = 0; string[i]; i++) 
          switch (tolower(string[i])) {
               case 'a':
               case 'e':
               case 'i':
               case 'o':
               case 'u':
               case 'y': // y may not always be a vowel though, the specifications are incomplete
                  vowels++;
           }
    
       cout << "There are " << vowels << " vowels in the string \"" << string << "\"" << endl;
    }
    
    void printNumberOfConsonants (const char *string) {
    
        long consonants = 0;
    
       for (long i = 0; string[i]; i++) 
          switch (tolower(string[i])) {
               case 'b':
               case 'c':
               case 'd':
               case 'f':
               case 'g':
               case 'h':
               case 'j':
               case 'k':
               case 'l':
               case 'm':
               case 'n':
               case 'p':
               case 'q':
               case 'r':
               case 's':
               case 't':
               case 'v':
               case 'w':
               case 'x':
               case 'z':
                  consonants++;
           }
    
       cout << "There are " << consonants << " consonants in the string \"" << string << "\"" << endl;
    
    }
    
    
    So just save that as myprogram.cxx and do g++ myprogram.cxx -o myprogram and then run myprogram and it should do everything you want to.
     
    Last edited: Feb 13, 2010
  22. Shayes

    Shayes OT Supporter

    Joined:
    Aug 9, 2008
    Messages:
    42,641
    Likes Received:
    1
    Location:
    Knoxville, TN
    holy shit lol thanks a lot, but my brother helped me and i finished it. i think you are using a lot of stuff i havent learned yet too, so yea...

    but thanks everyone
     
  23. samm

    samm Next in Line

    Joined:
    Dec 22, 2000
    Messages:
    2,630
    Likes Received:
    0
    Location:
    San Jose, CA
    boost has a string transformation library to make this easier than std::to_upper

    Code:
    #include <boost/algorithm/string/case_conv.hpp>
    
    #include <iostream>
    
    int
    main()
    {
        std::string buf;
        while( std::getline( std::cin, buf ) ) {
            boost::to_upper( buf );
            std::cout << buf << std::endl;
        }
    }
    
     
  24. White Stormy

    White Stormy Take that, subspace!

    Joined:
    Sep 17, 2002
    Messages:
    85,486
    Likes Received:
    69
    Location:
    Sparkopolis
    you sure do have a lot of people willing to do your homework for you.

    I hope you have a plan for passing the tests that you'll be entirely unprepared for
     
  25. CodeX

    CodeX Guest

    why the hell do you think that is any easier than using the C standard library function?
     

Share This Page