i watch youtube videos on arrays and people are not very precise.
what does s.size() do?
lets say the user input is in string "GFRTRFG" we want to know if this is a palindrome
does s.size() mean that there is 7 letters in the string?
also for array for this string
what does a array do
i initalize count to 0 and a array s[]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void input(string&palimdrome)
{
cout << "Enter a word and this will check if it is a palimdrome or not: ";
cin >> palimdrome;
}
string isPalindrome(string s)
{
int count = 0;
int S[];
x=s.size()-1; //whats this
for(int ix; ix < s.size(); ix++)
{
if (S[count] == s[x]) // because count is 0 we start from 0?
return -1; // then how do i increase count by 1?
cout << "This is a palindrome: " << s;//instead of ix
if () //should i do count; count < size.();count++
}
s being a string, s.size() calls a member function of s which returns the size of s.
does s.size() mean that there is 7 letters in the string?
Yes s.size() returns the size of s which is 7 in this case.
This code should do what you want it to do:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
void input(string &palindrome)
{
cout << "Enter a word and this will check if it is a palindrome or not: ";
cin >> palindrome;
}
bool isPalindrome(const string &s)
{
string::size_type x = s.size() - 1;
string::size_type mid = s.size() / 2 + 1;
for(string::size_type ix = 0; ix != mid; ++ix, --x)
{
if (s[ix] != s[x]) { returnfalse; }
}
returntrue;
}
void input(string &palindrome)
{
cout << "Enter a word and this will check if it is a palindrome or not: ";
cin >> palindrome;
}
bool isPalindrome(const string &s)
{
for(string::size_type ix = 0; ix < s.size() / 2; ++ix)
{
if (s[ix] != s[s.size() - ix - 1]) { returnfalse; }
}
returntrue;
}
That's actually less efficient. You are doing more arithmetic operations than needed. Every time your loop repeats s.size() is called and divided by 2. And every time your loop repeats s.size() is called again in the if condition with 2 more aithmetic operations.
With my way it is more efficient. My function calls the size() function once and defines the middle of the array with one computation.
void input(string&s)
{
cout << "Enter a word and this will check if it is a palimdrome or not: ";
cin >> s;
}
string isPalindrome(string s)
{
double pal;
int count = 0,x;
int S[x];
x=s.size()-1;
for(count; count < s.size(); count++)
{
if (s[count] != s[s.size() - count - 1])
pal = -1;
if (S[count] != s[x])
pal = 1;
}
return pal; // invalid conversion from int ot const char
}
void print(string s, string z)
{
if (z == -1)
cout << "The string " << s << " is a palindrome.";
}
@boost lexical cast
If the code efficiency is concerned, you can do this :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
void input(string &palindrome)
{
cout << "Enter a word and this will check if it is a palindrome or not: ";
cin >> palindrome;
}
bool isPalindrome(const string &s)
{
string::size_type mid = s.size() / 2;
for(string::size_type ix = 0; ix < mid; ++ix)
{
if (s[ix] != s[s.size() - ix - 1]) { returnfalse; }
}
returntrue;
}
the output is going to be in a different function, im suppose to store the result i get here into a new variable in main so i dont lose it then use that variable to go into the output function right?
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
usingnamespace std;
//-------------------------------------------------------------------------------------------------
void input(string&s);
string isPalindrome(string s);
void print(string s, string z);
//-------------------------------------------------------------------------------------------------
int main(){
char repeat;
do
{
string x,store;
input(x);
store=isPalindrome(x);
print(x,store);
cout << endl << "Do you wish to continue? Enter w or y.";
cin >> repeat;
}while(repeat == 'w' || repeat == 'y');
return 0;
}
//-------------------------------------------------------------------------------------------------
void input(string&s)
{
cout << "Enter a word and this will check if it is a palimdrome or not: ";
cin >> s;
}
string isPalindrome(string s)
{
string pal;
int count = 0,x;
int S[x];
x=s.size()-1;
for(count; count < s.size(); count++)
{
if (S[count] != S[x]) //is palindrome
pal = -1;
if (S[count] != S[x]) //not palindrome
pal = 1;
}
return pal; // invalid conversion from int ot const char
}
void print(string s, string z)
{
if (z == -1)
cout << "The string " << s << " is a palindrome.";
else
cout << "The string momx is not a palindrome."; //we want to the program to look at the first element and the last element at the same time, how do i do that?
cout << "The character in position 0 is " << (first letter of palindrome)
cout << "The character in position 3 is " << (where the letter was mismatched) // what goes in here? do i need to declare another variable to get the 2 mismatched position
}
In your string isPalindrome(string s) function you are trying to return an int while you declared the function so it is supposed to return a string aka const char*.
I strongly recommend you use my isPalindrome() function.
You're isPalindrome function has a lot of compile errors and logic errors. I fixed that in my function.
Note: I changed the function to return int since you want it to.
1 2 3 4 5 6 7 8 9 10 11 12
int isPalindrome(const string &s)
{
string::size_type x = s.size() - 1; // marks end of string s
string::size_type mid = s.size() / 2 + 1; // marks middle of string s
for(string::size_type ix = 0; ix != mid; ++ix, --x)
{
if (s[ix] != s[x]) { return -1; } // not palindrome
}
return 0; // palindrome
}
As for the for loop, this is how it works:
Say you have a string "123454321". It is a palindrome. The for loop checks it by checking if the first position of the string and the last position is equal. And then the first position + 1 and the last position -1. And so on until they meet. If any of these "checks" were not equal then it is not a palindrome.
can i change
if (s[ix] != s[x]) { return -1;
to
if (s[ix] == s[x]) { return -1; //is palindrome
return 0; // not palindrome?
-----------------------------------------------------------------------------------------------------
for the declaration
string::size_type x = s.size() - 1; // marks end of string s
string::size_type mid = s.size() / 2 + 1; // marks middle of string s
for(string::size_type ix = 0; ix != mid; ++ix, --x)
i can use the libraries
#include <string>
using namespace std; and it will eliminate the string::size_type
int isPalindrome(const string &s)
{
int x,mid;
x = s.size() - 1; // marks end of string s
mid = s.size() / 2 + 1; // marks middle of string s
for(int count = 0; count == mid; ++count, --x)
{
if (s[count] == s[x]) { return -1; } // palindrome
}
return 0; // not palindrome
}
No. If you change != to == then the moment the operands on either sides are equal, the function will say it is a palindrome. It isn't guaranteed every character in the string will be checked.
using namespace std; is bad practice. It checks all the identifiers used in the std namespace too. The reason the standard library is in the std namespace is so you HAVE to explicitly specifiy the namespace like std::cout. This way everything in the std namespace won't clash with other identifiers outside of it.
The reason I decared x and mid with the type size_type is because they are specifically for using the subscript operator on strings. Safer than int, since size_type is unsigned, too.
That code a lot of logic errors in the function print(string s, int z) and logic errors based on the way you're passing arguments into that function. You're also declaring array S strangely.
Also, when you run it, you'll find some error messages will appear. Like std::logic_error().
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
usingnamespace std;
//-------------------------------------------------------------------------------------------------
void input(string&s);
int isPalindrome(const string &s);
void print(string s, int z, string S[]);
//-------------------------------------------------------------------------------------------------
int main(){
char repeat;
do
{
string x;
string store;
input(x);
store=isPalindrome(x);
print(x,store);
cout << endl << "Do you wish to continue? Enter w or y.";
cin >> repeat;
}while(repeat == 'w' || repeat == 'y');
return 0;
}
//-------------------------------------------------------------------------------------------------
void input(string&s)
{
cout << "Enter a word and this will check if it is a palimdrome or not: ";
cin >> s;
}
int isPalindrome(const string &s)
{
int x,mid;
x = s.size() - 1; // marks end of string s
mid = s.size() / 2 + 1; // marks middle of string s
for(int count = 0; count != mid; ++count, --x)
{
if (s[count] != s[x]) { return -1; } // palindrome
}
return 0; // not palindrome
}
void print(string s, int z, string S[])
{
int count;
if (z == -1)
cout << "The string " << s << " is a palindrome." << endl;
else
cout << "The string " << s << " is not a palindrome." << endl; //we want to the program to look at the first element and the last element at the same time, how do i do that?
cout << "The character in position " << S[s.size()] <<" is " << S[count] << endl;
cout << "The character in position " << S[s.size()-1] << " is " << S[s.size()] << endl;// what goes in here? do i need to declare another variable to get the 2 mismatched position
}