11 Replies - 331 Views - Last Post: 30 October 2012 - 06:35 PM Rate Topic: -----

#1 agrawalparth08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-October 12

how do i print out all palindrome words in c?

Posted 29 October 2012 - 06:12 PM

i have used this porgram..can anyone help me..
#include<stdio.h>
#include<conio.h>
#include<string.h>
int palindrome(char str[50], int st, int ed);
main()
{
	char str[50],temp[50];
	int pal = 0, len = 0, i, start = 0, end,j=0;
	
	printf("\n------------------------------------------------------------------");
	printf("\n\nPROGRAM TO FIND THE TOTAL NUMBER OF PALINDROME IN A GIVEN STRING");
	printf("\n\n----------------------------------------------------------------");
	printf("\n\n\t ENTER A SENTENCE...: ");
	gets(str);
	while(str[len]!='\0')
		len++;
		
	len--;
	for(i=0;i<=len;i++)
	{
		if((str[i] == ' ' && str[i+1] != ' ') || i == len)
		{
                   
			if(i == len)
				end = i;
			else
				end = i - 1;
			if(palindrome(str,start,end))
				pal++;
				strncpy(temp,str,i);
              if(pal==1)
                printf("\n%s",temp);
			start = end + 2;
		}
	}
	printf("\n\n\t THE TOTAL NUMBER OF PALINDROMES ARE..: %d",pal);
	printf("\n\n---------------------------------------------------------------");
getch();
}
int palindrome(char str[50], int st, int ed)
{
	int i, pal=0;
	for(i=0; i<=(ed-st)/2; i++)
	{
		if(str[st+i] == str[ed-i])
			pal = 1;
		else
		{
			pal = 0;
			break;
		}
	}
	return pal;
}



Is This A Good Question/Topic? 0
  • +

Replies To: how do i print out all palindrome words in c?

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10663
  • View blog
  • Posts: 39,599
  • Joined: 27-December 08

Re: how do i print out all palindrome words in c?

Posted 29 October 2012 - 07:13 PM

What specific problems or errors are you encountering?
Was This Post Helpful? 0
  • +
  • -

#3 agrawalparth08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-October 12

Re: how do i print out all palindrome words in c?

Posted 30 October 2012 - 04:06 PM

well i have to print the palindrome words and also i dont want single letters like "a" or "i" which come in sentence to be treated as palindrome
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10663
  • View blog
  • Posts: 39,599
  • Joined: 27-December 08

Re: how do i print out all palindrome words in c?

Posted 30 October 2012 - 05:34 PM

So what does your program do now? How does that deviate from requirements?

Quote

i dont want single letters like "a" or "i" which come in sentence to be treated as palindrome

Ignore strings of length <= 1.
Was This Post Helpful? 0
  • +
  • -

#5 agrawalparth08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-October 12

Re: how do i print out all palindrome words in c?

Posted 30 October 2012 - 05:43 PM

basically what i want is the if i enter a sentence..
A radar is madam
the i shud be able to print radar and madam..
and not "a".
i have not given conditions for printing....it just counts how many palindromes are there..
i want to print the palindrome words and not consider single letter words
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10663
  • View blog
  • Posts: 39,599
  • Joined: 27-December 08

Re: how do i print out all palindrome words in c?

Posted 30 October 2012 - 05:47 PM

So break the problem down:
-How would you get each individual word?
-How would you test to see if it is a palindrome of length > 1?
-How would you print said word?
Was This Post Helpful? 0
  • +
  • -

#7 agrawalparth08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-October 12

Re: how do i print out all palindrome words in c?

Posted 30 October 2012 - 05:53 PM

View Postmacosxnerd101, on 30 October 2012 - 05:47 PM, said:

So break the problem down:
-How would you get each individual word?
-How would you test to see if it is a palindrome of length > 1?
-How would you print said word?

that is what i m asking...some sort of help as i m beginner..

i m asking u a code for that part..
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10663
  • View blog
  • Posts: 39,599
  • Joined: 27-December 08

Re: how do i print out all palindrome words in c?

Posted 30 October 2012 - 05:55 PM

Quote

it just counts how many palindromes are there

Clearly you can check if a string is a palindrome. So go from there. If it is a palindrome, print it.

Quote

i m asking u a code for that part..

We don't do that here. We aren't a code writing service. However, we are happy to help you with your good faith efforts.
Was This Post Helpful? 0
  • +
  • -

#9 agrawalparth08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-October 12

Re: how do i print out all palindrome words in c?

Posted 30 October 2012 - 06:25 PM

i tried new code...but it is not running.compilation is done..but exe crashes..
#include <stdio.h>
#include<conio.h>
main()
{
    char str[50];
    int len=0,i=0,j,start,end;
    char c;
    printf("Enter a string: ");
    gets(str);
    
    while(str[i++]!='\0')
        len++;
        for(i=0;i<=len;i++)
	{
		if((str[i] == ' ' && str[i+1] != ' ') || i == len)
		{
                
			if(i == len)
				end = i;
			else
				end = i - 1;
palindrome(str,start,end);
				
				
			start = end + 2;
		}
	}
    
getch();
}

int palindrome(char str[50], int st, int ed)
{
    int i,j,flag=1;
    char c;
for(i=0,j=((st-ed)-1);i<(st-ed)/2;i++,j--)
    {
        if(str[j]!=str[i])
        {
            flag=0;
            break;
        }
    }
        
        
    if(flag==1)
       { c=str[i];
        printf("%c",c);}
    else

        printf("String has no  palindrome");
        }


Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10663
  • View blog
  • Posts: 39,599
  • Joined: 27-December 08

Re: how do i print out all palindrome words in c?

Posted 30 October 2012 - 06:27 PM

Have you run it through a debugger to determine where and why it crashes?
Was This Post Helpful? 0
  • +
  • -

#11 agrawalparth08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-October 12

Re: how do i print out all palindrome words in c?

Posted 30 October 2012 - 06:33 PM

an access violation(segementation fault) raised in your program
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10663
  • View blog
  • Posts: 39,599
  • Joined: 27-December 08

Re: how do i print out all palindrome words in c?

Posted 30 October 2012 - 06:35 PM

A segfault means you have gone out of bounds. Look here:
for(i=0;i<=len;i++)
   {
        if((str[i] == ' ' && str[i+1] != ' ') || i == len)



Remember that arrays are indexed from 0 to length-1. When i = length, you go out of bounds. Even before that though, when you access str[i+1] and i = len-1, you have gone out of bounds.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1