How can I run this program?

closed account (2vkE6Up4)
Hi! I'm trying to write a program to check whether a sentence ending with ‘.’ is a palindrome or not using isPalindrome function, and this is what i did but the program is not running

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

using namespace std;

int reversal (char sent[]);
bool isPalindrome (char sent[]);
int main()
{
    char sent[100], i;
    //Enter a sentence;
    cin.get(sent,100);

    isPalindrome(sent);

    char ch='.';

    while(sent[i] != ch)
    {
        if(isPalindrome(sent))
        {
            cout<<"It's a palindrome!"<<endl;
        }
        else
        {
            cout<<"Not a Palindrome"<<endl;
        }
        cout<<endl;
    }
    sent[++i]='\0';

    return 0;
}
int reversed (char sent[])
{
    int i, length;
    for(i = 0; i < length  ; i++)
    {
        sent[i]= sent[i];
    }
    return i;
}
bool isPalindrome (char sent[])
{
    int i;

    if(sent[i]== reversed (sent))
    {
        return true;
    }
    else
    {
        return false;
    }
}
Last edited on
Hello ruu,

Welcome to the forum.

Looking at the program I see that you have included the header file "string". On line 10 you define a char array when a "std::string" will work the same and even better for you.

You define "i" as a "char" when it should be an "int" and for what it will be used for I would use an "unsigned int" or "std::size_t" which is another name for "unsigned int".

On line 12 yo can use "std::getline(std::cin, sent);".

Line 18. The while will not wort because you never change the value of "i" until after the loop.

Line 30. will add one to "i" and put a '\0' at the end of the char array if it would work, but yo are doing this to late. And with a "std::string" this would not be necessary.

The function "reversed" you define the variable "length" which has no value, so in the for loop it might look like: for (i = 0; i < -858993460; i++) because has no value only the garbage the is at the memory location that was set aside for "length".

Line 39 You are setting"sent[i] equal to sent[i];. Did you want to compare the two (==), but what is the point. The function is called "reversed" yet you reverse nothing and return an int. I think returning a "std::string" of the reversed original string would make more sense.

Right now I am thinking along the lines of:
1
2
3
4
5
6
7
8
9
10
11
std::string reversed (std::string sent)
{
   std::string reversed;

    for(std::size_t i =  sent.length(); i > 0; i--)
    {
       reversed += sent[i];
    }

    return reversed;
}


In the function "isPalindrome" again you are useing a variable, "i" in this case, that has a garbage value and is never given a value. In the if statement calling the the function "reversed" is not going to give you the result that you want.

To make the function "isPalindrome" work, maybe, "i" would have to be defined in the function definnition as a second parameter and passed from main except the "i" in main never changes value, so you would always be comparing the same position in the strings.

You have a good start that has possibilities, but needs reworked.

Hope that helps,

Andy
closed account (2vkE6Up4)
Thank you for explaining. but can you tell me please why "i" should be an "int" and not a "char" and what is unsigned int because I didn't learn about it yet and is there is any alternative for it . and what do you mean by "Line 18. The while will not work because you never change the value of "i" until after the loop.". sorry for asking so many questions I only know few about c++.
Hello ruu,

"i" should be an "int" because it is used in a subscript. When you have "sent[i]" what is between the []s is called a subscript and is used to access a given element of an array. Even using a "std::string" you can use this to access each individual character of the string. Although a "char" is a type of "int" the number used in a "char" would most likely be larger than the array is.

An "int" can handle both positive (unsigned) and negative (signed) numbers where as an "unsigned int" can only handle the (unsigned) positive numbers. Since a subscript can only use positive numbers, negative are not allowed, making the variable an "unsigned int" to use only positive numbers makes sense.

While on the subject when I started working on the program I found that the for loop in the above code needed changed. It should read this way:
for (int i = static_cast<int>(sent.length()) - 1; i >= 0; i--)

I do not use the "string.length()" in the first part of a for loop very often. so I did not think "- 1" part at first. "string.length()" returns the number of characters in a string, but the array starts at zero not 1, so the last character is at 3 not 4 that is why the "- 1". The ".length" function returns a "size_t" type variable. This is just another name for "unsigned int". The "static_cast<int>" is the newer C++ way to type cast one variable type into another variable type. I did this because of going backwards with the for loop I found that "i" would eventyally become "-1" before the loop ended and this was a problem when "i" was an "unsigned int".

That said the "reverse" function works.

I was thinking the program would benefit by changing any upper case letters to lower case letters before you reverse the string and most definitely before you check for palindrome. Include the header file <cctype> and you will have access to the functions "tolower(ch)" and "toupper(ch)" where "ch" refers to a single character. There are also other functions that are available. See: http://www.cplusplus.com/reference/cctype/
And for the "std::string" see: http://www.cplusplus.com/reference/string/string/
Two good links to bookmark for the future.

You may also consider a function that will remove any spaces and punctuation in the string.

You wrote:
to check whether a sentence ending with ‘.’ is a palindrome or not
Unless you remove the '.' nothing you enter will be a palindrome because there will never be a match for the '.'. I suggest you leave the '.' out when prompting the user for input. Using a "std""string" to store the input in there is no need for a '.' to mark the end. But if you use the '.' be sure to remove it before any other processing takes place.

I did change some things around like making "char sent[100]; to a "std::string sent;" "sent" is a poor choice of the variable name. It gives the idea that it is being used for output not the input that you intend. For a variable like this I most often use "line" or "input" for the name. Using "line" with the function "std::getline(...)" makes sense because you are reading an entire up to a delimiter or "\n" newline. It may be my personal preference, but it is better to use a variable name describes what it does and it does make the code easier to read.

In main I removed the while loop because it is working against you also making the program flow more difficult. The while loop is trying to check individual letters in the "char array", but you need to check the whole string at one time.

Even with the "reversed" function I showed you the "isPalindrome" function will not work the way it is. "i" needs to be initialized before it is used. Even then the function will be checking the same character every time. In the if statement you are comparing a "char" to the returned value of "reversed" which is an "int". This is not likely to ever match because the value of the "char" is different than the value of the "int" that is returned.

I know that is a lot to think about and understand for now, so I will let you work on that. When you have a question let me know.

Hope that helps,

Andy
Hello ruu,

Two more things to keep in mind when you are working:

Try to avoid using using namespace std; in your programs it may seem easy now, but WILL get you in trouble some day.

It is better to learn to qualify what is in the standard name space with "std::" and then to learn what is in the standard name space now while it is easy. And not all at once to try to keep a job.

What you are most likely to use for now is "std::cout", "std::cin" and "std::endl". About a week or so of typing this and you will not even notice that you are doing it.

It is ALWAYS a good practice and programming to initialize your variables. If your compiler is using the C++11 standards or after the easiest way to initialize variables is with empty {}s, e.g., int num{};. This will initialize the variable to 0 (zero) or 0.0 for a double. A "char" will be initialized to "\0". "std::string"s are empty to start with and do not need to be initialized. Should you need to you can put a number between the {}s.You can also Initialize an array, e.g., int aNumbers[10]{};. This will initialize all elements of the array to 0 (zero). A use of
int aNumbers[10]{ 1 }; will initial the first element to "1" and the rest of the array to "0".. Following the "1" with ", 2 ..." will initialize the array with the numbers that you have used up to the entire array.


Andy
closed account (2vkE6Up4)
Thank you so much for the explanation and for your time. I will try my best to do it. about using namespace std; I cannot avoid using it because it's the only way they taught us to do, programming is selective course in Uni I've never learned about it before and it's the only course using computer, so I'm pretty slow in understanding it but I will do my best thank you.
Topic archived. No new replies allowed.