Thread: One-Dimensional Array

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    17

    Question One-Dimensional Array

    Can anybody help me with my program? I can't get the example. Here's the question: Create a program that accepts an array of characters. And displays the converted array of characters into Upper case if it is given in
    Lowercase and vice versa. Don't use string, but char.

    Example: mychar ="I am A conQUeror."
    Code:
    //My Codes:
    #include <iostream>
    #include <conio.h>
    using namespace std;
    int main() 
    {
    cout << "Password? \n";
    char input[256];
    cin.get(input, 256);
    for(int i = 0; i < strlen(input); i++)
    {
            input[i] = toupper(input[i]);
            cout << endl << input << endl;
            }
    for(int i = 0; i < strlen(input); i++)
    {
            input[i] = tolower(input[i]);
            cout << endl << input << endl;
            }
    getch ();
    return 0;
    }

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    You currently run 2 loops through your input, 1 which uppercases everything, then another which lowercases everything...so the end result is just going to be everything lowercased, you are also outputting the string way too often for some reason? You should only output the string once after you have modified it in the way I describe below.

    What you really need to do is simply go through the string and check if the letter is uppercase. If the letter is uppercase, lowercase it. If the letter is not uppercase, uppercase it.
    You should use the header files: <cctype> and <cstring> to get the toupper, tolower, isupper() and strlen() functions properly.

  3. #3
    Registered User
    Join Date
    Aug 2014
    Posts
    17
    That was such an advise, after reading it I innovated all my codes specially the looping portion but the sad effect was that the output hasn't met what I am looking for. Any more help dude?
    Code:
    #include <iostream>
    #include <conio.h>
    #include <cctype>
    #include <cstring>
    using namespace std;
    int main ()
    {
    cout << "Password? \n";
    char input[256];
    cin.get(input, 256);
    for (int i = 0; input[i] != '\0'; i++)
    {
        if (isupper(input[i]))
        {
            input[i] = tolower(input[i]);
        }
        else if (islower(input[i]))
        {
            input[i] = toupper(input[i]);
            }
    
    
    cout << input << '\n';
    getch ();
    return 0;
    }
    }

  4. #4
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Good job you got it pretty much right. The only problem is that your cout << input << '\n'; and getch() call are still INSIDE your for() loop. You want to print the data AFTER you have modified it right? So you need to print it after the for loop is done.

    You could also simplify the if/else if case a bit more. If you know isupper() is FALSE then do you really need to test islower() also?

  5. #5
    Registered User
    Join Date
    Aug 2014
    Posts
    17
    Got it! Putted the getch and the return outside, I forgot putting it outside. Thanks.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,550
    Good to hear. By the way, note that <conio.h> and getch are non-standard. You do not actually need to use getch here because you can run your program from a separate command prompt window, or for an IDE, configure it to pause before ending the program.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assigning One dimensional array to two dimensional array
    By lightning_star in forum C Programming
    Replies: 1
    Last Post: 03-19-2014, 09:44 PM
  2. Replies: 12
    Last Post: 09-02-2013, 07:50 PM
  3. Replies: 4
    Last Post: 09-02-2013, 11:19 AM
  4. Replies: 24
    Last Post: 11-11-2008, 01:39 PM
  5. Replies: 1
    Last Post: 04-25-2006, 12:14 AM

Tags for this Thread