-
palindrome
Can anyone please advise wad's wrong with this code...why do i still get 0 despite typing in radar...
Code:
1 #include <stdio.h>
2 #include <string.h>
3
4 int isPalindrome1(const char *str);
5
6 int main ( )
7 {
8 char str [40];
9 int result;
10 scanf("%s",str);
11
12 result=isPalindrome1(&str);
13 printf("%d",result);
14
15 return 0;
16 }
17
18 int isPalindrome1 (const char *str)
19 {
20 char temp1 [40];
21 char temp2 [40];
22 int i=0;
23 int length=0;
24 int result=0;
25
26 strcpy (temp1,str);
27 length=strlen(temp1);
28 for (i=0; i<length; i++)
29 temp2[i]=temp1[length-i];
30
31 if(strcmp(temp2,str)=='\0')
32 result=1;
33 else
34 result=0;
35
36 return result;
37 }
-
o...i got it :D
Code:
1 #include <stdio.h>
2 #include <string.h>
3
4 int isPalindrome1(const char *str);
5
6 int main ( )
7 {
8 char str [40];
9 int result;
10 scanf("%s",str);
11
12 result=isPalindrome1(&str);
13 printf("%d",result);
14
15 return 0;
16 }
17
18 int isPalindrome1 (const char *str)
19 {
20 char temp1 [40];
21 char temp2 [40];
22 int i=0;
23 int length=0;
24 int result=0;
25
26 strcpy (temp1,str);
27 length=strlen(temp1);
28 for (i=0; i<=length; i++)
29 temp2[i]=temp1[length-i-1];
30 if(strcmp(temp2,str)=='\0')
31 result=1;
32 else
33 result=0;
34
35 return result;
-
Very good, but there is a faster and easier way...
Basically you compare the ends, moving in and exiting on the first difference...
Code:
int IsPalindrome(char *str)
{ int i = 0;
int j = strlen(str) -1;
while (j > i)
{ if ( str[i] != str[j])
return 0; // no it isn't
i++;
j--; }
return 1; } // yes it is!
-
Thanks, m trying a anagram code oso...can u pls advise why this code isnt working....it's printing 0 even for anagrams....
Code:
1 #include <stdio.h>
2 #include <string.h>
3
4 int isAnagram1(char *str1, char *str2);
5 int isAnagram2(const char *str1, char *str2);
6 int isAnagram3(const char *str1, const char *str2);
7 void sort (char *str, int length);
8 void swap (char *a, char*b);
9
10 int main ( )
11 {
12 int result=0;
13 //int check=0;
14 //int triplecheck=0;
15 char str1 [50];
16 char str2 [50];
17
18 scanf("%s %s", str1, str2);
19
20 result=isAnagram1(&str1, &str2);
21 //check=isAnagram2(&str1, &str2);
22 //triplecheck=isAnagram3 (&str1, &str2);
23
24 printf("%d\n", result);
25 //printf("%d\n", check);
26 //printf("%d\n", triplecheck);
27
28 return 0;
29 }
30
31 int isAnagram1(char *str1, char *str2)
32 {
33 int length1=0;
34 int length2=0;
35 int result=0;
36
37 length1=strlen(str1);
38 length2=strlen(str2);
39 sort(&str1, length1);
40 sort(&str2, length2);
41
42 if (strcmp(str1,str2)=='\0')
43 result=1;
44 else
45 result=0;
46
47 return result;
48 }
49
50 void sort (char *str, int length)
51 {
52 int i=0;int j=0;
53
54 for (j=0; j<length; j++)
55 for (i=j+1;i<length; i++)
56 if (str[j]>str[i])
57 swap( &str[i],&str[j]);
58 }
59
60 void swap (char *a, char*b)
61 {
62 char temp;
63 temp=a;
64 a=b;
65 b=temp;
-
-
You could do us all a favour by not posting code with the line numbers included. The board adds it's own line numbers anyway - if you do it properly.
Also, your swap function should be generating warnings - try fixing them.
-
sorry...m not sure how to remove the line numbers...i'll take note...
and i'm unsure how to fix the swap function....do i put a * in front of temp?
-
changed the swap function to this....but...now when i execute the programme...i get segmentation fault(core dumped)
Code:
void swap (char *a, char*b)
{
char temp;
temp=*a;
*a=*b;
*b=temp;
}
-
39 sort(&str1, length1);
40 sort(&str2, length2);
What about the warnings on these lines as well?
-
hmmm....i know there are warnings on lines 20, 39 and 40 oso...but i reali dunno how to fix them....isnt the function sort expecting a string? so wads wrong when i send in the address?
pls advise. tks a million
-
o...the programme runs fine when i remove both the & ....but why? i thot when i see * ...i must put & in the function call???
-
Here's what to do.
1. compile your code
2. fix ALL the warnings and errors.
3. if you don't understand a specific error message, then post your code and exact error messages.
Because all this "well some stuff happens" and general guessing doesn't work.
-
Hi...i wrote another version for anagrams...but i dun understand why does it always return 0...no warnings or errors in the function....can u help me to decipher the code the tell me what's wrong...thanks..
Code:
int isAnagram3(const char *str1, const char *str2)
{
char num = 'a';
int i=0;
int j=0;
int count1=0;
int count2=0;
int length1=0;
int length2=0;
int result=1;
length1=strlen(str1);
length2=strlen(str2);
for (num='a'; num<='z'; num++)
{
for (i=0;i<length1;i++)
if (str1[i]==num)
count1++;
for (j=0; j<length2; j++)
if (str2[j]==num)
count2++;
if (count1!=count2)
{
result=0;break;
}
}
return result;
}
-
not sure why count1=1; count2=0....den they are not equals.....if they are anagrams, why is count1 !=count2?
-
added this on line 117....but no difference....count1=0;count2=0;