case convertion from lower ro upper and vice versa

+1 ragmar thomas · July 19, 2014
how to convert a random string from the user to upper or lower case just like a dictionary application whether the user typed it in upper case or lower case it search if possible with out any header files.

Post a Reply

Replies

- page 1
Oldest  Newest  Rating
0 Dima Sama · July 19, 2014
Well there is a lot of ways to convert a random string from lower to upper and vice versa. But how do you want to use it? Do you want to automatically change every first letter to upper or you wanted to convert every letter from upper to lower and vice versa?

Tell us how you want to use the convertion and we will gladly help you :)
0 ragmar thomas · July 21, 2014
thanks in advance, just want to change the hole word to be changed in either way just like a dictionary application i have just made one but it its to embarrassing to post it here i just want just when ever the user typed a word whether lower or upper case the program retrieves the meaning of the word if it is saved before <simply just a way of changing the case of the words i have seen one but i can remember where is it now in the internet i think it is like + 32 or -32>
0 Dima Sama · July 21, 2014
Well i don't know how you want to use it but if you want to convert a small letter to capital and vice versa there are two ways. I can give you an example plus an idea.

The first way is to use cctype library


#include <iostream>
#include <cctype>

int main()
    {

        cout << endl << endl;
        cout << "Welcome to the Letter Changing.\n\n"
             << "Enter a sentence and end it with @:\n";

        char letterInput;


        while (cin.get(letterInput) && letterInput != '@') // Loop that will end if either the input is incorrect or with @ sign
        {
            if (isalpha(letterInput)) // If the input is alphabetic execute the statement below, else print the original letter
                cout << char(isupper(letterInput) ? tolower(letterInput) : toupper(letterInput)); // if input is upper change to lower, else change it to upper.
            else
                cout << letterInput;
        }

        return 0;


    }


First you need to do a number reading loops like this code:


while (cin.get(letterInput) && letterInput != '@') 


This code means that as long as the input did not fail the loop will continue. The condition which is cin.get(letterInput) is another way to prompt the user to input. It does the same as an ordinary cin >> letterInput. After the user entered an input cin.get() buffered the next input in line.



            if (isalpha(letterInput)) // If the input is alphabetic execute the statement below, else print the original letter
                cout << char(isupper(letterInput) ? tolower(letterInput) : toupper(letterInput)); // if input is upper change to lower, else change it to upper.
            else
                cout << letterInput;


This code here do all the  convertion. You need cctype library to use the isalpha(), isupper(), tolower() and toupper(). isalpha() checks whether the user enter an alphabet. If the user entered space, number, punctuation or newline the program will just output it.

If the user entered an alphabet that's where the conversion starts. This statement here is like an if else statement:



 char(isupper(letterInput) ? tolower(letterInput) : toupper(letterInput));


isupper(letterInput) checks if the user entered a Capital letter. If the user entered a capital letter isupper() will be true and then tolower() will convert it to lower letter and if the user did not entered capital letter toupper() will convert it to capital letter. The char in the first statement encapsulate them all because after the evaluation they will changed to int type so we need to typecast it to char type and send it to cout to print.


The other way is using the ascii number of the letter. It is as the same with the previous example you just need to change the contents of if else statement.
It's like this


if(letterInput >= 'A' && letterInput <= 'z')
      cout << char((letterInput >= 'A' && letterInput <= 'Z') ? letterInput + 32 : letterInput - 32)


The reason for using +32 and -32 because the distance between capital "A" to lower capital "a"  is 32.



You can also use nested if else but it will be too long.

I hoped i helped. :)

Tell me if there's something you didn't understand.
0 Brandon Elliott · July 21, 2014
hmm... all this for a task so simple in Java.

String.toLowerCase() or String.toUpperCase()


:)
0 Dima Sama · July 21, 2014
I don't know how to use java but i searched about the string.tolower, it says that it convert all string to lower, right? well that's convenient. :)


Maybe ill need to learn some java. It seems really popular.
0 ragmar thomas · July 30, 2014
the code below try to resemble for a dictionary that helps the user to enter new words but it wont over write i used string data type but it want overwrite new words there is a way to solve this with fstream but it impossible for me to make it work and i have a feeling the data type must be char to work with fstreams and i have limitation using char very hard to deal with 


#include <iostream>
using namespace std;
int main(){
   string meaning, index, enter, add, newWord;
cout << "Dictionary" << endl;
cout << "Search word" << endl;
cout << "Enter word" << endl;
cout << "To quit dictionary press q" << endl;
do{there:
cin >> index;
if(index == "car")
    cout << " manned vehicle " << endl;
    else
        if(index=="computer")
        cout << " a machine use for calculating bulky numbers accurately and faster" << endl;
    else
        if(index=="kickass")
        cout << " just having supper nice whatever stuffs" << endl;
   else
        if(index==add){goto x;
        cout << meaning;}
    else
        if((index!="car")&&(index!="computer")&&(index!="kickass")){
    cout << "word not found!" << endl;
    cout << "you want to add word to dictionary (y/n): ";
    cin >> enter;
    cin.ignore();
       if(enter=="y")
        cout << "please enter your new word: " << endl;
    getline(cin, add);
    cout << "please enter your new word's meaning: " << endl;
    getline(cin, meaning);
    cout << "your new word's meaning: " << endl;x:
    cout << meaning << endl;
if(enter=="n")
    cout << "enter any word to search for: ";
goto there;}
}while(index!="q");}


0 Dima Sama · August 1, 2014
I don't understand what you are trying to accomplish to your program.
-1 Dima Sama · August 1, 2014
I doubt that he's getting paid for his program.
0 Dima Sama · August 1, 2014
I explain and showed to him how to convert uppercase and lowercase. How he uses the code is for him to decide. I don't think his work is just to convert strings from lower to upper and vice versa.
0 Dima Sama · August 1, 2014
Yes, i understand what you are trying to imply, but explaining things for him without examples on how to do it especially in a text format like this is hard. If you can help him understand things just by explaining it, be my guest. But for now i'll gladly help them with free examples.
  • 1
  • 2

C++

107,190 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator