String Manipulation

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;

		
		
		
		
	}
	
	
	
	
}
Why are you mixing C strings and c++ string? Why not use C++ string?
L15 - 17 - str is already defined on L14

As C++, then perhaps:

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
#include <iostream>
#include <string>
#include <cctype>

bool isPalindrome(const std::string&);

int main() {
	std::string strs[] {"level", "racecar", "banana", "peep"};

	for (const auto& s : strs)
		std::cout << "How many letters are in " << s << "? " << s.size() << '\n';

	for (auto& s : strs) {
		for (auto& c : s)
			c = static_cast<char>(std::toupper(static_cast<unsigned char>(c)));

		std::cout << s << " is " << (!isPalindrome(s) ? "not " : "") << "a palindrome\n";
	}
}

bool isPalindrome(const std::string& str) {
	auto itf {str.cbegin()};
	auto ite {str.crbegin()};

	while (itf < ite.base())
		if (*itf++ != *ite++)
			return false;

	return true;
}



How many letters are in level? 5
How many letters are in racecar? 7
How many letters are in banana? 6
How many letters are in peep? 4
LEVEL is a palindrome
RACECAR is a palindrome
BANANA is not a palindrome
PEEP is a palindrome

Last edited on
Or if you have to use C-style strings, then possibly:

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
#include <iostream>
#include <cstring>
#include <cctype>

bool isPalindrome(const char* st);

int main() {
	char strs[4][10] {"level", "racecar", "banana", "peep"};

	for (const auto& s : strs)
		std::cout << "How many letters are in " << s << "? " << strlen(s) << '\n';

	for (auto& s : strs) {
		for (auto ch {s}; *ch; ++ch)
			*ch = static_cast<char>(std::toupper(static_cast<unsigned char>(*ch)));

		std::cout << s << " is " << (!isPalindrome(s) ? "not " : "") << "a palindrome\n";
	}
}

bool isPalindrome(const char* st) {
	auto itf {st};
	auto ite {st + strlen(st)};

	while (itf < ite)
		if (*itf++ != *--ite)
			return false;

	return true;
}

Last edited on
I think i will work with c++ strings. but im am still getting some errors from your code on lines 25 26 and 31 in my compiler.

25 [Error] 'class std::initializer_list<std::reverse_iterator<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> > > >' has no member named 'base'

line col
26 11 [Error] no 'operator++(int)' declared for postfix '++' [-fpermissive]

line col
26 21 [Error] no 'operator++(int)' declared for postfix '++' [-fpermissive]

31 2 [Error] 'Edit' does not name a type
> I think i will work with c++ strings.

With std::string_view, we can work seamlessly with both C++ strings and C-style strings.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <string_view>
#include <algorithm>

// note: non-alpha characters and capitalization are *not* ignored
// https://en.cppreference.com/w/cpp/string/basic_string_view
bool is_palindrome( std::string_view str ) noexcept
{
     // https://en.cppreference.com/w/cpp/algorithm/equal
     return std::equal( str.begin(), str.begin() + str.size() / 2, str.rbegin() ) ;
}

int main()
{
    std::cout << std::boolalpha << is_palindrome( "LEVEL" ) << '\n' // true
                                << is_palindrome( "Level" ) << '\n' ; // false

    const std::string str = "RACECAR" ;
    std::cout << std::boolalpha << is_palindrome(str) << '\n' ; // true

    const char cstr[] = "PEEP" ;
    std::cout << std::boolalpha << is_palindrome(cstr) << '\n' ; // true
}

http://coliru.stacked-crooked.com/a/2886d3488868a824
but im am still getting some errors from your code on lines 25 26 and 31 in my compiler.


That code compiles OK with VS2022 as C++20. What compiler and what version of C++ are you using?
Last edited on
im using dev c++ version 5.11

update: i think i got it figured out now. I updated my complier. Thanks for the help guys
Last edited on
Topic archived. No new replies allowed.