hello I have been trying to get parts of this code to work.
I had it compling before but not anymore(i think bracket problem). Mainly Ive been trying to get it to convert the words from lowercase to uppercase by using null and a loop.
Secondly i been trying to create a function that reads the words backwards and determines if it is a palindrone. I would appreciate if some of you more advanced guys can take a look it.
****************************************************
here are the instructions if it helps:
Declare an array to store one word.
Prompt operator to enter a word.
1.Call the string length library function (strlen) to display the length of the word.
This replaces your string length function.
2.Convert the word to uppercase. (Display from main).
HOW: Pass the word.
Set up a loop looking for the NULL.
Within the loop, call the library function (toupper) to convert each character to uppercase.
3.Determine if the word is a palindrome (spelled same backward as forward -- example: radar, deed, civic).
HOW: Write a function to copy the word in reverse order.
Use the string compare library function (strcmp) to compare the original word with the reversed word.
Report the results in main.
Test Data:
level
racecar
banana
peep
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
#include<iostream>
#include<cstring>
using namespace std;
void palindrome();
int main()
{
char word1[] = "level";
char word2[] = "racecar";
char word3[] = "banana";
char word4[] = "peep";
char ch;
string str = "level";
string str = "racecar";
string str = "banana";
string str = "peep";
// count letters in words
cout << "how many letters are in level? " << strlen(word1) << endl;
cout <<"how many letters are in racecar? " << strlen(word2) << endl;
cout << "how many letters are in banana? " << strlen(word3) << endl;
cout << "how many letters are in peep? " << strlen(word4) << endl;
cout << wordReverse(str);
for (int i = 0; i < strlen(word1); i++) {
// convert str[i] to uppercase
ch = toupper(word1[i]);
cout << ch ;
}
//convert words to uppercase
for (int i = 0; i < strlen(word2); i++) {
// convert str[i] to uppercase
ch = toupper(word2[i]);
cout << ch;
}
for (int i = 0; i < strlen(word3); i++) {
// convert str[i] to uppercase
ch = toupper(word3[i]);
cout << ch;
}
for (int i = 0; i < strlen(word4); i++) {
// convert str[i] to uppercase
ch = toupper(word4[i]);
cout << ch;
}
return 0;
}
// read words in reverse to find a pailindrone
void palindrome()
{
string wordReverse(string str)
int i = str.length() -1;
int start, end = i + 1;
string result = "";
while (i >= 0){
if (str[i] == ' '){
start = i + 1;
while (start != end)
result += str[start++];
result += ' ';
end = i;
}
i--;
}
start = 0;
while (start != end)
result += str[start++];
return result;
}
}
|