palindrom words in a sentence

Need help! Why is output always ZERO?
This is should tell you how many palindrom words are in a sentence but it doesn't work.
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>
int palindrom(char rec[500])
{
    char nopu[500];
    int i;
    for (i=strlen(rec)-1;i>=0;i--)
    {
        rec[i]=tolower(rec[i]);
        nopu[i]=rec[i];
    };
    if (rec==nopu) return 0; else return 1;
};
int main()
{
    char rec[500];
    char nopu[500];
    int zbir=0,max,i,a;

    scanf("%s",rec);
    for (i=0;i<=(a=strlen(rec));i++);
    {
    max=0;
    memset(nopu,'\0',500);
    
    while (1==1)
    {
          
    if (isalnum(rec[i])) goto sort;
    if (max > 0) goto pal; else goto poc;
    
    sort:;
    
    nopu[i]=rec[i];
    max++;
    i++;
    };
    
    
    pal:;
    
     
    if(palindrom(nopu)==0) zbir++;
    
    poc:;
    };
   
    printf("%d\n",zbir);  
    getch();  
    return 0;
}
    
    
    
    
    
    
Last edited on
That's not very readable. The gotos make my head hurt.

Are you sure you need to ; in for (i=0;i<=(a=strlen(rec));i++);? I'm not sure what variable a means.

Can you describe your algorithm in words and I can offer some help in coding it?
I removed that thingy ; and variable a but it still doesn't work.
It is suposed to parse all the words and check if they are palindromes.
I didn't use this method because it is too slow
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>
#include <ctype.h>
#include <stdio.h>
using namespace std;
int palindrom (string a)
{
    string b;
    for (int i=a.size()-1;i>=0;i--)
    {
              a[i]=tolower(a[i]);
              b+=a[i];
    }
           if (a==b) return 1; else return 0;
}

int main ()
{
string a,b;
int max=0,zbr=0;
getline (cin,a);
for (int i=0;i<=a.size();i++)
 {
     b.clear();
     zbr=0;
     while (5==5)
     {
                    if ((a[i]>=65 && a[i]<=90)||(a[i]>=48 && a[i]<=57)||(a[i]>=97 && a[i]<=122))
                    {
                          goto abc;
                    };

                    if (zbr>0) goto pal;
                    goto poc;
                    abc:;  
                    b+=a[i];
                    zbr++;          
                    i++;
     };
           
              pal:;
              cout<<b<<endl;
              if (palindrom(b)==1)
              {
               max++;
              };
              poc:;
 };
          printf("%d",max);
          system("pause");
          return 0;
}
Since you already have an 'iostream' declaration try to stick to 'couts/cins', makes it easier to debug. Anyway, I have an old code I used for palindrome testing using stacks to hold the values, hope it helps a little. I've not done this in quite long, so I can't remember most of what to do, sorry I could not be of more help.

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
38
39
40
41
#include <iostream>
#include <stack>

using namespace std;

int main()
{
stack<char>mystack,tempstack,reversestack;
int k;
mystack.push('C');
mystack.push('I');
mystack.push('V');
mystack.push('I');
mystack.push('C');

cout <<"The size is: "<<mystack.size()<<endl;

cout << "The word is: ";
while (!mystack.empty())
{
cout << " " << mystack.top();
k=mystack.top();
mystack.pop();
reversestack.push(k);
tempstack.push(k);
}
cout <<endl<<"The reverse word is: ";
while (!tempstack.empty())
{
cout << " " << tempstack.top();
k=tempstack.top();
tempstack.pop();
mystack.push(k);
}
if(reversestack==mystack)
cout<<endl<<"It is a palindrome!";
else
cout<<endl<<"It is not a palindrome!";
return 0;
}

I've run your code. The palindrom function works as is, there just a lot of unnecessary junk in main.

I'm reposting your code without the noise.

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
#include <iostream>
#include <string>
#include <ctype.h>
#include <stdio.h>
using namespace std;
int palindrom (string a)
{
	string b;
	for (int i=a.size()-1;i>=0;i--)
	{
		a[i]=tolower(a[i]);
		b+=a[i];
	}

	if (a==b)
		return 1;
	else
		return 0;
}

int main ()
{
	string a;
	getline (cin,a);
	printf("%d", palindrom(a));
	system("pause");
	return 0;
}
You didin't understand me well.
Standard input is a sentence Example: "My name on www.cplusplus.com is aarmin."
The program is suposed to parse all the words and check if they are palindromes and write number of palindromes on standard output.
Palindrom in this example sentence is "www".
You can see that i removed all character except numbers and letters so "www" is a palindrom. if (isalnum(rec[i]))
I DON'T WANT to use this code because it can't pass some tests because it is to slow.

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>
#include <ctype.h>
#include <stdio.h>
using namespace std;
int palindrom (string a)
{
    string b;
    for (int i=a.size()-1;i>=0;i--)
    {
              a[i]=tolower(a[i]);
              b+=a[i];
    }
           if (a==b) return 1; else return 0;
}

int main ()
{
string a,b;
int max=0,zbr=0;
getline (cin,a);
for (int i=0;i<=a.size();i++)
 {
     b.clear();
     zbr=0;
     while (5==5)
     {
                    if ((a[i]>=65 && a[i]<=90)||(a[i]>=48 && a[i]<=57)||(a[i]>=97 && a[i]<=122))
                    {
                          goto abc;
                    };

                    if (zbr>0) goto pal;
                    goto poc;
                    abc:;  
                    b+=a[i];
                    zbr++;          
                    i++;
     };
           
              pal:;
              cout<<b<<endl;
              if (palindrom(b)==1)
              {
               max++;
              };
              poc:;
 };
          printf("%d",max);
          system("pause");
          return 0;
}


Now this would pass but the output is always zero.
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>
int palindrom(char rec[500])
{
    char nopu[500];
    int i;
    for (i=strlen(rec)-1;i>=0;i--)
    {
        rec[i]=tolower(rec[i]);
        nopu[i]=rec[i];
    };
    if (rec==nopu) return 0; else return 1;
};
int main()
{
    char rec[500];
    char nopu[500];
    int zbir=0,max,i,a;

    scanf("%s",rec);
    for (i=0;i<=(a=strlen(rec));i++);
    {
    max=0;
    memset(nopu,'\0',500);
    
    while (1==1)
    {
          
    if (isalnum(rec[i])) goto sort;
    if (max > 0) goto pal; else goto poc;
    
    sort:;
    
    nopu[i]=rec[i];
    max++;
    i++;
    };
    
    
    pal:;
    
     
    if(palindrom(nopu)==0) zbir++;
    
    poc:;
    };
   
    printf("%d\n",zbir);  
    getch();  
    return 0;
}
No need for help i solved it!
Topic archived. No new replies allowed.