I'm trying to write a peice of code that will convert all the uppercase characters in a word to lowercase, and to store that in an array. I know how to convert individual characters, and to output all these characters in the fixed lower case.
The thing i'm unsure about is how to use this information to store the whole lowercase word in an array, because what my program outputs is a sequence of characters, rather then a word.

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

int main(){
	string input;
	char ch;
	cout<<"Enter a word: "<<endl;
	cin>>input;

	for (int i = 0; i < input.size(); i++){
	      if (isupper(input.at(i))){ 
	    	  ch=tolower ( input.at(i) ); 
	    	  cout<<ch;
	      }else{
	    	  cout<<input.at(i);
	      }
	}

}

Here's my code so far. Any help is kindly appreciated.

I'm trying to write a peice of code that will convert all the uppercase characters in a word to lowercase, and to store that in an array. I know how to convert individual characters, and to output all these characters in the fixed lower case.
The thing i'm unsure about is how to use this information to store the whole lowercase word in an array, because what my program outputs is a sequence of characters, rather then a word.

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

int main(){
	string input;
	char ch;
	cout<<"Enter a word: "<<endl;
	cin>>input;

	for (int i = 0; i < input.size(); i++){
	      if (isupper(input.at(i))){ 
	    	  ch=tolower ( input.at(i) ); 
	    	  cout<<ch;
	      }else{
	    	  cout<<input.at(i);
	      }
	}

}

Here's my code so far. Any help is kindly appreciated.

The if-statement and isupper check are unnecessary. A single call to toupper will suffice.

http://www.cplusplus.com/reference/clibrary/cctype/toupper/

Converts parameter c to its uppercase equivalent if c is a lowercase letter and has an uppercase equivalent. If no such conversion is possible, the value returned is c unchanged.


Return Value
The uppercase equivalent to c, if such value exists, or c (unchanged) otherwise. The value is returned as an int value that can be implicitly casted to char.

See red. toupper only converts if the character is a lower case letter. Otherwise it's unchanged. Thus you can do this:

string upperCaseWord = input
for (int i = 0; i < upperCaseWord.size(); i++)
{
     upperCaseWord[i] = toupper (upperCaseWord[i]);
}

cout << upperCaseWord;

Edit: Whoops. I read your post wrong. Looks like you want the result to be lower-case, not upper-case. Same as above, just change upper to lower everywhere.

You do not need to pair isupper and tolower. If the character passed to tolower does not have a lower case equivalent then the same character will be returned:

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

int main()
{
    using namespace std;

    string input;

    cout << "Enter a word: ";
    cin >> input;

    for (int i = 0; i < input.size(); i++)
    {
        input[i] = tolower(input[i]);
    }

    cout << input << '\n';
}

Why does the word need to be added to an array? Can it not be changed in place like my example? If not, a string would be much better than an array because with an array you have to figure out the size and do other stuff that is not necessary in C++.

How about the std::transform algorithm?

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

 using namespace std;

 int main(){
  string input;
  cout<<"Enter a word\n";
  getline(cin,input);
  transform(input.begin(),input.end(),input.begin(),(int(*)(int))tolower);
  cout<<input<<'\n';
}

Hey,
Wow, I didn't expect to get replies so soon! Thanks to everyone who helped out :)

VernonDozier: Thanks for explaining everything clearly. It made things so much more easier to understand. I ended up using something similar to the example you gave and it works it my program. Thanks again!

Tom Gunn: I just made a smaller program so I could try to understand the concept before I implemented it in my program, without having to worry about mixing up functions and things like that. In the bigger project, I get something out of a file, change all characters in the words to lowercase, then put it in an array so words can be search for. I don't know if it's a good way of thinking, but it sometimes helps when i'm really stuck. :)

Asafe: I've never seen transform before, it's something i'm defiantly going to look into and hopefully use in the future.

Hahaha, none of our programs return.

int main(){

//return 0;
}

Hahaha, none of our programs return.

int main(){

//return 0;
}

actually, it did, implicitly :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.