Checking pallindrome
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 30

Thread: Checking pallindrome

  1. #1
    Join Date
    May 2004
    Posts
    43

    Checking pallindrome

    I was writing a C program, for practice, that checks for a pallindrome. When I compile with Visual C++ compiler, it gives me an error (16 errors actually) at the line where I prototype the function ispallindrome. It says

    syntax error: identifier 'ispallindrome'
    What could be the reason? I am going batty over this. Here's the code.


    Code:
    #include <stdio.h>
    
    bool ispallindrome(char[]);
    
    
    int main(void)
    {
    
    	char str[100];
    	char cReply;
    
    	do
    	{
    		printf("Enter a word here to see if it is a 
    pallindrome: ");
    		scanf("%s", str);
    		if ispallindrome(str)
    		{
    			printf("\n\n%s is a 
    pallindrome",str);
    		}
    		else
    		{
    			printf("\n\n%s is not a 
    pallindrome",str);
    		}
    
    		printf("\nDo you wish to enter one more 
    word? [Y/N]: ");
    		scanf(%d, cReply);
    		if ((cReply!='Y') && (cReply!='y')) break;
    
    	} while (true);
    
    }
    
    bool ispallindrome(char s[])
    {
    	bool bResult;
    	int i,j, len;
    	float median;
    
    	for (len=0; s[len]!='\0';len++);
    	median=(len+1)/2;
    	if isint(median)
    	{
    		i=j=median;
    	}
    	else
    	{
    		i= mod(median);
    		j=i+1;
    	}
    
    	bResult=true;
    	for(len=0; len<=i; i--, j++)
    	{
    		if (s[i]!=s[j])
    		{
    			bReply=false;
    			break;
    		}
    	}
    
    	return bReply;
    
    }

  2. #2
    Join Date
    Mar 2004
    Location
    Israel
    Posts
    638
    a lot of typos here:

    first, if your printf's strings are cut, then use the character '\' at the end of each cut line.

    if ispallindrome(str)
    should be:
    if (ispallindrome(str))

    scanf(%d, cReply);
    should be
    scanf("%d", cReply); where cReply should be integer, or use "%c" instead, depends what you tryin to do.

    if isint(median) ===> what is 'isint'? again, use parenthesis after 'if'...
    **** **** **** **** **/**

  3. #3
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    Since you are writing a C program, you should know that bool is not part of the C language that Visual Studio supports. That is why you are getting the error at the prototype declaration.

    bool is available in the C99 standard through <stdbool.h>. However, this still is not supported by Visual Studio.

    - Kevin

  4. #4
    Join Date
    May 2004
    Posts
    43
    I dont have unterminated strings in the code. I broke the lines in the CODE tags on purpose because their underlying HTML tag is PRE, so they won't insert line breaks.

  5. #5
    Join Date
    May 2004
    Posts
    43
    Thanks, KevinHall. That was the problem. Now I changed all bools to ints and here is my code. Now it asks me where the functions isint and mod are. I thought they must be in the standard library stdio.h or stdlib.h or ctype.h, but it is still complaining. It compiles with warnings but it won't know what to link in place of those calls. Please help.


    Edited later:

    Oops! I forgot to post the code. Here.

    Code:
    #include <stdio.h>
    
    int ispallindrome(char []);
    
    int main(void)
    {
    
    	char str[100];
    	char cReply;
    
    	do
    	{
    		cReply=' ';
    		printf("Enter a word here to see if it is a pallindrome: ");
    		scanf("%s", str);
    		if (ispallindrome(str))
    		{
    			printf("\n\n%s is a pallindrome",str);
    		}
    		else
    		{
    			printf("\n\n%s is not a pallindrome",str);
    		}
    
    		printf("\nDo you wish to enter one more word? [Y/N]: ");
    		scanf("%c", cReply);
    		if ((cReply!='Y') && (cReply!='y')) break;
    
    	} while (1);
    
    }
    
    int ispallindrome(char s[])
    {
    	int bResult;
    	int i,j, len;
    	float median;
    
    	for (len=0; s[len]!='\0';len++);
    	median=(float)(len+1)/2;
    	if (isint(median))
    	{
    		i=j=(int)median;
    	}
    	else
    	{
    		i= mod(median);
    		j=i+1;
    	}
    
    	bResult=1;
    	for(len=0; len<=i; i--, j++)
    	{
    		if (s[i]!=s[j])
    		{
    			bResult=0;
    			break;
    		}
    	}
    
    	return bResult;
    
    }

  6. #6
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    There is no standard C functions mod or isint. fmod is available in <math.h> -- which is a floating point modulus. (Click on the link and look at the documentation before modifying your code.) And isint will need to be implemented in terms of fmod(). Perhaps something like:

    Code:
    #define isint(x) (fmod((x), 1.0) == 0.0)

  7. #7
    Join Date
    May 2004
    Posts
    43
    Thanks again, Kevin. I now realize I do not need the mod or fmod function, that returns a modulus. I was misguided by a peer whom I asked for help. Let me tell you what I want.

    I want two functions that do the following:

    (1) Tell me if the value in a floating point variable is a whole number or a decimal number.
    (2) Another function that retrieves the integral part out of a floating point variable.

    I'd be very grateful if you helped me out. Thanks very much for you help till now. I am really obliged.

  8. #8
    Join Date
    May 2004
    Posts
    43
    I surmise I could accomplish the second task by simply type-casting a float to an int.

    Code:
    int i;
    float median;
    i = (int)median;
    But I am scared that it might round off the float rather than trimming it's decimal part. Please advise.

  9. #9
    Join Date
    May 2004
    Location
    Norway
    Posts
    655
    Typecasting only trims the decimal part away from the float. 1.9234574 will become 1;

  10. #10
    Join Date
    May 2004
    Posts
    43
    Thanks for the info.

    And for the first part, I guess I figured it out using this:

    Code:
    if (fmod(median,1)>0)
    This would tell me if it has a decimal part or not.

  11. #11
    Join Date
    May 2004
    Posts
    43
    Ok, now my program does not gimme errors, but it has a logical bug. It does not gimme a correct result and only tells me nothing is or was every a pallindrome. Then it crashes. I think the problem is somewhere in the for loop in the ispallindrome function. Here's my code:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int ispallindrome(char []);
    
    int main(void)
    {
    
    	char str[100];
    	char cReply;
    
    	do
    	{
    		cReply=' ';
    		printf("Enter a word here to see if it is a pallindrome: ");
    		scanf("%s", str);
    		if (ispallindrome(str))
    		{
    			printf("\n\n%s is a pallindrome",str);
    		}
    		else
    		{
    			printf("\n\n%s is not a pallindrome",str);
    		}
    
    		printf("\nDo you wish to enter one more word? [Y/N]: ");
    		scanf("%c", cReply);
    		if ((cReply!='Y') && (cReply!='y')) break;
    
    	} while (1);
    
    	return 1;
    }
    
    int ispallindrome(char s[])
    {
    	int bResult;
    	int i,j, len;
    	float median;
    
    	for (len=0; s[len]!='\0';len++);
    	median=(float)(len+1)/2;
    	if (fmod(median,1)>0)
    	{
    		i=j=(int)median;
    	}
    	else
    	{
    		i= (int)median;
    		j=i+1;
    	}
    
    	bResult=1;
    	for(len=0; i>=0; i--, j++)
    	{
    		if (s[i]!=s[j])
    		{
    			bResult=0;
    			break;
    		}
    	}
    
    	return bResult;
    
    }

  12. #12
    Join Date
    May 2004
    Posts
    43
    I figured the problem was with the scanf call. It crashed there may be because of an improper formatting of the char. So I now replaced it with a getchar call. But now it does not evaluate the loop condition and just terminates after 1 round of the do loop.

    Here's the code:

    Code:
    do
    	{
    		cReply='Y';
    		printf("Enter a word here to see if it is a pallindrome: ");
    		scanf("%s", str);
    		if (ispallindrome(str))
    		{
    			printf("\n\n%s is a pallindrome",str);
    		}
    		else
    		{
    			printf("\n\n%s is not a pallindrome",str);
    		}
    		printf("\nDo you wish to enter one more word? [Y/N]: ");
    		cReply=getchar();
    	} while (cReply=='Y' || cReply=='y');

  13. #13
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    Your program is crashing due to a memory access violation. This problem is in your call to scanf(). You need to use:

    Code:
    scanf("%c", &cReply);
    As far as your algorithm for finding palindromes are concerned, I suggest you try debugging it. There are some interesting things you should learn by debugging the program -- one example: what is happening with my float cast and more importantly why?

    Good luck debugging!

    - Kevin

  14. #14
    Join Date
    May 2004
    Location
    Norway
    Posts
    655
    I don't quite see why you feel the need to use floating point at all here. The point is to find the middle indices to the string, right? If the length of the string is an odd number, the middle index (that is, both j and i) will simply be length/2. (integer math) If the length is an even number, the indices (i and j) will be (length/2) - 1 and length/2. There's no need for floating point math anywhere, as this can all be done with a simple if construction.

    I'll leave the "is odd number" test and the coding to you since you say this is for practice.

  15. #15
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    Yeah, there are lots of things he could improve. Another example is using strlen().

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center