How to count number of palindromes in the sentence [Closed]

Report
Posts
7
Registration date
Thursday October 3, 2013
Status
Member
Last seen
April 30, 2014
-
 Blocked Profile -
hello everyone;

can anybody teach me how to make a program using c language that would count the number of palindromes in a sentences and display the palindromes disregarding special characters...

example: Enter your sentence: " Ana and hannah together with her dad went to the beach.

number of palindromes: 3 ana hannah dad

i really need help;
Related:

6 replies


Well, lets break it down.


0. load all variables-IE (PALCOUNT=0; sentance=""; dim wordarray()
1.Trap whole sentence in a variable
2.Count the number of spaces in the sentence
(IE 10-one_two_three_four_five_six_seven_eight_nine_ten_eleven)
3. Make an array with 10+1 slots-populate each element in array as word - redim your array with the count [wordarray(11)].
4. Loop through array and get the word, making another variable for the current word being scoured.
5. Make a logical check with the most right character (last character), with the first.
6. If it passes that test, then check the rest of the characters to see if match working from outter chars into inner.
7. If it passes increment variable of PALCOUNT++;

I am certain that you have this wireframe built but are looking for the actual code. I like to guide and advise, attempting to encourage to do your own research for particulars. I am certain Buno can come up with the C code for this logic.
Posts
7
Registration date
Thursday October 3, 2013
Status
Member
Last seen
April 30, 2014
1
hi void display(good day);

i have here a simple snippets


int main(){


char a[100],b[100];
int i,k;



clrscr();

printf("Type the string: ");
gets(a);

k=strlen(a)-1;

for(i=0; i<strlen(a); i++){
b[i]=a[k];
k--;
}

b[strlen(a)]='\0';

k=strcmp(a,b);

if (k==0){
printf("palindrome");
}
else{

printf("not palindrome");

}

getch();

}


it will print "palindrome" if the input string is palindrome but will print "not palindrome" if I input a two word palindrome string. Example: "eju huje".

the main problem is if I input a string:

it will count and display a 3 or more palindrome words:

Example: "my dad and mom went to Glenelg, Scotland"

output:

number of palindrome words: 3 dad mom Glenelg


thank you ;



Posts
14814
Registration date
Monday July 11, 2005
Status
Moderator
Last seen
October 30, 2020
1,402
Yes, it's because you must take care of space character.
Just have to "delete" them before the comparison
Posts
7
Registration date
Thursday October 3, 2013
Status
Member
Last seen
April 30, 2014
1
yes!

the first thing I need to do is remove all the spaces and punctuation, so
"eju, huje"

gets stored as

"ejuhuje"

So that when I reverse, it will work as expected.

i work a hard time in it and still I can't get
Blocked Profile
if you do that, then in your sentence as posted:

i work a hard time in it and still I can't get

the line up of "aha" and "ini" and "illi" would count, so...

store whole sentence, chop up, array, test against adjoining arrays?
Blocked Profile
Dan went to the park with hannah, anna , and dad.

thesentence="Dan went to the park with hannah, anna, and dad."

Break into array elements and toss the punctiation.
1 2 3 4 5 6 7 8 9 10

1dan danwent 2went wentto 3to tothe 4the thepark 5park parkwith 6with withhannah 7hannah hannahanna 8anna anaand 9and anddad 10dad

would result with: hannah anna dad

danwenttotheparkwithhannahannaanddad

results in:
tot hannah anna annahanna nnahann nahan anna naan dad
Posts
14814
Registration date
Monday July 11, 2005
Status
Moderator
Last seen
October 30, 2020
1,402
Hi,
Here is a code, with pointers, which should take care of space characters

int isPalindrome(char *text)
{
// init pointers
char *c, *d;

// set the 1st pointer to the 1st character
c = d = text;

// set the 2nd pointer to the last character
while(*d)
d++;
d--;

// process the text and check the characters:
// the 1st with the last
// the 2nd with the 2nd to last
// etc.
// until the first difference or the middle
// doesn't take care of space characters
while(c < d) {
while(*d == 32) // 32 is the space character ASCII value
d--; // jump over space character
if(c >= d)
break; // OK
while(*c == 32)
c++; // jump over space character
if(c >= d)
break; // OK
if(*c != *d)
return 0; // if characters are differents, it is not a palindrome

c++;
d--;
}

// yeah! it is a palindrome
return 1;
}

If you want to use the "string methods" (strlen, strcmp,...), you can see the strrev() method (http://www.warpspeed.com.au/cgi-bin/inf2html.cmd?..%5Chtml%5Cbook%5CToolkt40%5CXPG4REF.INF+275 used to reverse a string. And..oh! There is an example of palidrome detection on the above link ;)

Good luck and feel free to ask for advices
Posts
14814
Registration date
Monday July 11, 2005
Status
Moderator
Last seen
October 30, 2020
1,402
I'm here ;)

I have a solution...BUT, I will not do all the stuff! I'm waiting for your contribution =)

To check if a word is a palindrome, you just have to follow the points 5, 6 of the above ac3mark's algorithm. I propose you a code a little more optimized to do this check. I hope you're a friend of pointers ;)

int isPalindrome(char *word)
{
// init pointers
char *c, *d;

// set the 1st pointer to the 1st character
c = d = word;

// set the 2nd pointer to the last character
while(*d)
d++;
d--;

// process the word and check the characters:
// the 1st with the last
// the 2nd with the 2nd to last
// etc.
// until the first difference or the middle
while(c < d) {
if(*c != *d)
return 0; // if characters are differents, it is not a palindrome
c++; d--;
}

// yeah! it is a palindrome
return 1;
}

Now, all you have to do is to cut your sentence into words. Based on the code I've given to you for counting sentences inside a paragraph, it will be easy...

Feel free to ask for advices if you need help. And I said help and not entire code ;)
@+
Buno, Modo CS-CCM
The urgent is done, the impossible is underway. For miracles, envisage a time ...