CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2009
    Posts
    1

    Change case in array

    Can somebody please help me. I have an assignment which is stated below of which I cannot seem to figure out and get to work.

    My assignment:
    Problem 1
    Write a program that asks the user for a string, then displays the length of the string, the string with the cases reversed, the string in all lowercase, and the string in all uppercase.

    The main function should prompt the user, read in the string, and find and display the length. The main function should call three other functions (reverse, lower, and upper, in that order) and display the results.

    The upper function should accept a pointer to a C-string as an argument. It should step through each character in the string, converting it to uppercase. The lower function should also accept a pointer to a C-string as an argument. It should step through each character in the string, converting it to lowercase. Like upper and lower, reverse should also accept a pointer to a string. As it steps through the string, it should test each character to determine whether it is upper- or lowercase. If a character is uppercase, it should be converted to lowercase. Likewise, if a character is lowercase, it should be converted to uppercase.

    Sample output:

    Please enter a string.
    This is my string. What do you think?
    Your string is 37 characters long.
    Your string reversed: tHIS IS MY STRING. wHAT DO YOU THINK?
    Your string in lowercase: this is my string. what do you think?
    Your string in uppercase: THIS IS MY STRING. WHAT DO YOU THINK?



    <code>

    //Benjamin Tshudy

    #include <iostream>
    #include <cstring>
    #include <cctype>
    using namespace std;

    char* reverse (char*, int);
    char* lower (char*, int);
    char* upper (char*, int);

    int main ()
    {
    const int SIZE = 80;
    char myString [SIZE];
    int strLength;

    //Prompt user for string
    cout << "Please enter a string.\n";

    //read in string
    cin.getline(myString, SIZE);

    //find and display length
    strLength = strlen(myString);
    cout << "Your string is " << strLength << " characters long\n";

    //call reverse function and output results in main to screen
    //reverse(myString, strLength);
    cout << "Your string reversed: ";
    //for (int count = 0; count < strLength; count++)
    cout << reverse(myString, strLength);
    //cout << myString[count];
    cout << endl;

    //call lower function and output results in main to screen
    lower(myString, strLength);
    cout << "Your string in lowercase: ";
    for (int count = 0; count < strLength; count++)
    cout << myString[count];
    cout << endl;

    //call upper function and output results in main to screen
    upper(myString, strLength);
    cout << "Your string in uppercase: ";
    for (int count = 0; count < strLength; count++)
    cout << myString[count];
    cout << endl;

    return 0;
    }

    // function that reverses the case of string
    char* reverse (char * string, int SIZE)
    {
    for(int count = 0; count < SIZE; count++)
    {
    if (isupper(string[count]))
    tolower(string[count]);
    else if (islower(string[count]))
    toupper(string[count]);
    }
    return string;
    }

    // function that puts entire string in lower case
    char* lower (char * string, int SIZE)
    {
    for(int count = 0; count < SIZE; count++)
    tolower(string[count]);
    return string;
    }

    // function that puts entire string in upper case
    char* upper (char * string, int SIZE)
    {
    for(int count = 0; count < SIZE; count++)
    toupper(string[count]);
    return string;
    }
    <\code>

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Change case in array

    Code tags get wrapped in [] not <>

    toupper and tolower return the argument in the changed case. They don't change the char that's passed in.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured