tolower

This is my code to take an input and determine if it is a palindrome or not. In my strConvert function I use putchar, but it takes the uppercase letters and lowers then and just puts them in front of the string. I want it to just lower the uppercase letters in the string. I feel my code is pretty close to being correct. Please Help!


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

bool isPalindrome(string&);
string strConvert(string, string&);

int main() {
//init vars
string str;
string newStr;

//prompt user for palindrome
cout << "Please enter a possible palindrome. ";
cin >> str;

strConvert(str, newStr);

cout << newStr << endl; //test

if(isPalindrome(newStr) == true) {
cout << "That was a Palindrome!";
} else {
cout << "That was not a Palindrome!";
}

//pause and exit
getchar();
getchar();
return 0;
}

//fuction that detrmines if input is a palindrome or not.
bool isPalindrome(string& str) {
int length = str.length();

for(int c = 0; c < length / 2; c++) {
if(str[c] != str[length -1 - c])
return false;
}
return true;
}

string strConvert(string str1, string& str2) {

int length = str1.length();
char ch;

for(int c = 0; c <= length; c++) {
ch = str1[c];
if(isalpha(ch)) {
if(islower(ch)) {
str2 += ch;
}else if(isupper(ch)) {
putchar(tolower(ch));
str2 += ch;
}
}
}
return str2;
}
You really made this much more difficult than it has to be. Simplified code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

bool isPalindrome(string);
void strConvert(string&);

int main() {
//init vars
   string str;

//prompt user for palindrome
   cout << "Please enter a possible palindrome. ";
   cin >> str;

   strConvert(str);

   cout << str << endl; //test

   if (isPalindrome(str))
      cout << "That was a Palindrome!";
   else
      cout << "That was not a Palindrome!";

//pause and exit
   return 0;
}

//fuction that detrmines if input is a palindrome or not.
bool isPalindrome(string str) {
   for (unsigned int c = 0; c < str.length() / 2; c++)
      if (str[c] != str[str.length() - 1 - c])
         return false;
   return true;
}

void strConvert(string &str) {
   for (unsigned int c = 0; c <= str.length(); c++)
      str[c] = tolower(str[c]);
}
Topic archived. No new replies allowed.