Calling a function from a function

I am having trouble calling the function isPurePalindrome() from inside the function isPalindrome(). I have checked everything and determined the problem is at the line

ans= isPurePalindrome(dptr);

here is my code, if anyone could please help me identity the error, i would be most grateful.



#include<stdlib.h>
#include<stdio.h>
#include<string.h>

int isPurePalindrome( const char *sentence );

int isPalindrome( const char *sentence );


int main( void )
{
//declare variables

char array[1000];
char *pointer = array;
int size,i,ans, ans1;

//inform user of program purpose

printf("\n\nType in any sentece, phrase, or numerical value and this program"
" will check for palindromes; ordinary palindromes, pure palindromes."

printf("\n\nType in a sentence.\n\n");

//get input from user

gets(array);

pointer= &array[0]; //pointer points to first element of array

ans= isPurePalindrome(pointer);
if( ans == 0 ){
printf("\nSentence entered is not a pure palindrome.\n");
}
else if( ans == 1 ){
printf("\nSentence is a pure palindrome.\n");
}


ans1= isPalindrome(pointer);

if( ans1 == 0 ){
printf("\nSentence entered is not an ordinary palindrome.\n");
}
else if( ans1 == 1 ){
printf("\nSentence is an ordinary palindrome.\n");
}

system("pause");
return 0;
}


//define purepalindrome

int isPurePalindrome( const char *sentence ){
if ( *sentence == '\0' ){
return 0;
}
int size= strlen(sentence);
char *begin, *end;
*begin = *sentence; //points to first character
end= begin + size -1; //points to last character
while ( begin < end ){
if ( toupper(*begin) != toupper(*end) ){
return 0;
}
else if ( toupper(*begin) == toupper(*end) ){
*begin++;
*end--;
}}
return 1;
}

//define ispalindrome
int isPalindrome( const char *sentence ){
if (*sentence== '\0'){
return 0;
}
int i, size= strlen(sentence), size2=0, ans;
char array1[size+1],* sptr, *dptr;
dptr=array1; //destination pointer points to destination array
sptr = sentence; //source pointer points to original string
for(i=0;i<size+1;i++){ //loop to copy only alphbetical characters into new array
if(isalpha(*sptr)){
*dptr= *sptr;
*sptr++;
*dptr++;
}
else if(*sptr == '\0'){
*dptr='\0';
}
else *sptr++;
}
dptr=&array1[0]; //point pointer to beginning of string

ans= isPurePalindrome(dptr);

return ans;
}
Have you even tried to compile this? It won't compile, and does this have to be coded as a C application or can you use C++?
Definitely compiles. The purpose of the isPalindrome function is basically to copy the string from main into a new string using pointers, then pass the new string to the isPurePalindrome function. Everything that i wrote in the function is verified and works up until the line

ans= isPurePalindrome(dptr);

the program just stops running, and yeah it has to be in C.
1
2
char *begin, *end;
*begin = *sentence; //points to first character 


No, it doesn't. *begin is the content at the memory location that begin points to. Seeing as begin hasn't been initialised, you've just written to a totally random memory location.

What you want is begin = sentence; because we're assigning a pointer (begin) to point to the same location that another pointer (sentence) is pointing to.

Jim
Topic archived. No new replies allowed.