array help, i dont understand anything

Pages: 12
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++
	}
what does s.size() do?
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]) { return false; }
	}
	
	return true;
}
Last edited on
closed account (18REwA7f)
@boost lexical cast
This can be also written as :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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]) { return false; }
	}
	
	return true;
}
@Tantum

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.
Last edited on
i want to write a value returning function, if its a palindrome return -1;

instead of return -1 cant i declare a variable like pal = -1; does it act like return -1?
after that what do i do?

can you explain what each line does after you code it. i really dont get arrays

my current 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
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.";
}


Last edited on
@seungyeon

Well, normally if you want to create a function to check if something is true, then you want it to return a boolean value (true or false).

But, you can create the function so it returns an int too.

1
2
3
4
5
6
7
8
9
int isPalindrome(const string &s)
{
	for(string::size_type ix = 0; ix < s.size() / 2; ++ix)
	{
		if (s[ix] != s[s.size() - ix - 1]) { return -1; } // not palindrome
	}
	
	return 0; // palindrome
}


And yes, if you declare a variable of type int named pal and give it a value of -1, then return pal; is equal to return -1;.

To use these functions you can do this:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>

int main()
{
        std::string str;
        input(str);
        std::cout << (isPalindrome(str) ? "yes" : "no") << '\n';

        return 0;
}

If you're using the int version of the function then
1
2
3
if (isPalindrome(str) == 0) { std::cout << "yes\n"; }

else { std::cout << "no\n"; }
Last edited on
closed account (18REwA7f)
@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]) { return false; }
	}
	
	return true;
}
@Tantum

That is still less efficient.

If you define x first and use x then you only have to decrement x every time the loop repeats.
Last edited on
check out my code

i cant return pal even if i assigned it to -1

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?

why cant i return pal?

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

using namespace 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 

}
Last edited on
@seungyeon

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*.
@seungyeon

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.
Last edited on
@seungyeon

I don't mean any offense but, the code you last posted above doesn't make a lot of sense.

Tons of compile errors and logic errors. I can't list them all. Undefined variables, wrong integral types, etc.

I don't really see how you're trying to check for palindromes.
Last edited on
@boost lexical cast

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
}
@seungyeon

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.
Last edited on
can someone give me a simple array example
instead of this int A[4] ={1,2,3,4}

as 4 use a variable to give out 4 elements the user inputed
Last edited on
@seungyeon

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().
Last edited on
how do you declare an array of strings?

i hate arrays
closed account (18REwA7f)
how do you declare an array of strings?

i hate arrays

And what does your title say?
array help, i dont understand anything
^^

@tantum
please help me guide me through this
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
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>

using namespace 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 
}
Pages: 12