This is a fragment of a program for finding palindrome numbers.Every checked number is a string which is reversed...and I'm using array of strings which isn't done right. Any help will be greatly appreciated.

#include <stdio.h>
#include <string.h>
void isPalindrome(char *str1[],int l){
	int str1size = strlen(str1[l]);
	int n;
	int misMatch = 0;
	char *str2[20];
	for(int i = (str1size - 1); i>=0; i--)
	{
		n = (str1size - 1) - i;
		str2[n] = str1[i];
	}
	for (int j=0;j<=(str1size-1);j++){
             if (str1[j]!= str2[j])
		misMatch++;
     }
	if (misMatch)
		printf("\nNot palindrome\n");
	else
		printf("\nA paliindrome\n");
}
int main(){
	char *str1[20];
	str1[0] = "9009";
	str1[1] = "2345";
	for(int l=0; l<2; l++)
	isPalindrome(str1,l);
	return 0;
}

You probably don't need an array of strings. Just make a function that takes two strings as parameters. void isPalindrome(char * str1,char * str2) { } Use the length of one to step backwards through that string while doing forwards through the other one.

Hello george61,
I think the problem is in the for loop which reverses the array.

for(int i = (str1size - 1); i>=0; i--)
	{
		n = (str1size - 1) - i;
		str2[n] = str1[i]; //<-- problem   
	}

I think the whole string("9009" or "2345") gets into str2[n] without getting reversed, because char *str2[20] can hold 20 strings.
str2[n] holds a string and not a character. So every time in the loop the whole string is getting into str2[n] from str[1] and not as each character.

1. for(int i = (str1size - 1); i>=0; i--)
2. {
3. n = (str1size - 1) - i;
4. str2[n] = str1; // ERROR
5. }

In line 4, the address is being copyied instead of value. As, str1 here is a double dereference pointer(**str1), use **pointer notations and copy value itself. dont copy addresses

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.