need help regarding palindrome using pointers

code is not working :(
#include<iostream.h>
#include<process.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void palindrome(char *str1,int l);

void main()
{
clrscr();
int l;
char *str1;
cout<<"enter 1st string";
gets(str1);
l=strlen(str1);
cout<<"length ";
cout<<l;
palindrome(str1,l);

getch();
}
void palindrome(char *str1,int l)
{
int y;
for(int i=0;i<l;i++)
{
for(int j=l;j>0;j--)
{
if(*(str1+i)!=*(str1+j))
{
cout<<"not palindrome";
goto y;
}
}
}

y:



}
Last edited on
closed account (E3h7X9L8)
why so many libraries ? you only need iostream
anyway i should tell you that I dont reccomend using goto ,their purpose can be done with structured programming, pointers must be initialized, I dont get what you've tried to do with that double for's, you basically take every element from the string and compare it with ALL the other elements in the string...here is a efficient version i coded

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

using namespace std;

bool palindrome(char *str1, int l); // using bool function that return true and false

void main()
{
	int l;
	char string[20];
	cout << "Enter 1st string \n";
	gets(string);
	char *str1 = &string[0]; // pointers must be initialized
	l = strlen(str1);
	cout << "Length " << l;
	
	if (palindrome(str1, l) == true)
		cout << "\nIt is palindrome";
	else
		cout << "\nIt is not palindrome";
        cin.get(); // get some input so the console won't close


}
bool palindrome(char *str1, int l)
{
	int a = 0;
	int b = l - 1;
	while (a < b)
	{
		if (str1[a] != str1[b])
			return false;
		a++;
		b--;
	}
	return true;
}
Topic archived. No new replies allowed.