Saraswathi Apavoo
Saraswathi Apavoo

Reputation: 371

c++ looping through string

Finally i find solutions in convert lowecase to uppercase and to identify whether the string is alphabet or numeric code as follow:

#include <cctype>
#include <iostream>
using namespace std;
int main()
{
  char ch;
  cout<<"Enter a character: ";
  gets(ch);

  if ( isalpha ( ch ) ) {
    if ( isupper ( ch ) ) {
      ch = tolower ( ch );

      cout<<"The lower case equivalent is "<< ch <<endl;
    }
    else {
      ch = toupper ( ch );
      cout<<"The upper case equivalent is "<< ch <<endl;
    }
  }
  else
    cout<<"The character is not a letter"<<endl;
  cin.get();
} 

How can i improve the code above to get string rather than a single character? Looping keeps print same statements many times. Thanks

Upvotes: 1

Views: 2343

Answers (3)

kamituel
kamituel

Reputation: 35950

Update: Here's the cleaner solution which outputs one, single word.

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

char switch_case (char ch) {
  if ( isalpha ( ch ) ) {
      if ( isupper ( ch ) ) {
        return tolower ( ch );
     }
     else {
       return toupper ( ch );
     }
   }
  return '-';
}

int main()
{
  string str;
  cout<<"Enter a word: ";
  cin >> str;

  transform(str.begin(), str.end(), str.begin(), switch_case);
  cout << str << "\n";
}

The std::transform is being used in this example.


Just read an entire word and use std::string::iterator to iterate over one letter at a time:

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

int main() 
{
  string str;
  cout<<"Enter a word: ";
  cin >> str;

  for ( string::iterator it = str.begin(); it != str.end(); ++it ) {
    char ch = *it;
    if ( isalpha ( ch ) ) {
      if ( isupper ( ch ) ) {
        ch = tolower ( ch );

        cout<<"The lower case equivalent is "<< ch <<endl;
     }
     else {
       ch = toupper ( ch );
       cout<<"The upper case equivalent is "<< ch <<endl;
     }
   }
   else
     cout<<"The character is not a letter"<<endl;
 }
 cin.get();
}

Upvotes: 2

masoud
masoud

Reputation: 56479

C++11 :

#include <cctype>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s;
    cout << "Enter data: ";
    cin >> s;

    for (auto &ch : s)
    {
        if (isalpha(ch))
        {
            if (isupper(ch))
            {
                ch = tolower(ch);
                cout << "The lower case equivalent is " << ch << endl;
            }
            else
            {
                ch = toupper(ch);
                cout << "The upper case equivalent is " << ch << endl;
            }
        }
        else
            cout << "The character is not a letter" << endl;
    };
    cin.get();
} 

or

#include <cctype>
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;
int main()
{
    string s;
    cout << "Enter a string: ";
    cin >> s;

    transform(s.begin(), s.end(), s.begin(), [](char ch)
    {
       return isupper(ch)? tolower(ch) : toupper(ch);
    });
} 

If you have g++ try : g++ test.cpp -o test -std=c++11 to compile.

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409166

Firs use the input operator to read into a string:

std::string input;
std::cin >> input;

Optionally you can use std::getline to get more than a single word.

Then you can use std::transform to convert the string to upper- or lower-case.

You can also use a range-based for loop to iterate over the characters in the string.

Upvotes: 2

Related Questions