Question:
how can i compare two strings without using string.h?
linn
2011-03-04 20:03:14 UTC
I'm trying to write this program that determines whether a string is a palindrome. I was able to take the user input and reverse it (like, if the user inputted hello the reversal of that would be olleh). I thought that I'd be able to use an if statement, saying

if (array1[i] == array2[i])

It would compare if the two were equal. If they were equal, then I'd be able to say that the user input was a palindrome. However, it completely ignores the else statement (that it's NOT a palindrome) and prints that everything is a palindrome. What am I doing wrong?

Note: The line "if (strcmp(input, array) == 0)" is just there so that the program will work, but I'm trying to compile it WITHOUT using the string.h library

Here's my code:

#include
#include
#define STRLEN 50

int
main(void)
{
char input[STRLEN];
char array[STRLEN];
int i;
int total;

printf("Please enter a string > ");
scanf("%s", input);

total = 0;
for (i = 0; input[i] != '\0'; ++i) {
total = total + 1;
}

for (i = 0; i < total; i++) {
array[i] = input[total - 1 - i];
}

array[i] = '\0';

if (strcmp(input, array) == 0)
printf("Given string \"%s\" is a palindrome\n", input);

else
printf("Given string \"%s\" is not a palindrome\n", input);

return (0);
}
Four answers:
vinodh t
2011-03-05 01:13:03 UTC
include

#define STRLEN 50

int main(void)

{

char input[STRLEN];

char array[STRLEN];

int i;

int total,d=0;

printf("Please enter a string > ");

scanf("%s", input);



total = 0;

for (i = 0; input[i] != '\0'; ++i) {

total = total + 1;

}



for (i = 0; i < total; i++) {

array[i] = input[total - 1 - i];

}



array[i] = '\0';

for(i=0;i
{

if (array[i]==input[i])

d++;

}

if (d==total)

printf("Given string \"%s\" is a palindrome\n", input);



else

printf("Given string \"%s\" is not a palindrome\n", input);



return (0);

}





now it works i add new variable called d and slight modification into program.it works without string.h

for feedback and further queries please contact vinodhec@gmail.com
Ratchetr
2011-03-04 20:32:32 UTC
You don't need to make a (reverse) copy of a string to know if the string is a palindrome. Spend some time thinking about the problem, and and algorithm to solve the problem, rather than the details of your code.



Suppose you have pointers to the first and last character in a string.



If the chars they point to aren't the same, then it isn't a palindrome, is it?



If they do match, then it's only a palindrome if the string between the first and last characters is a palindrome. RIght? Think it through.....



So you start at both ends of the string. As long as the 2 characters at each end are the same, you have a potential palindrome. Bump the left had pointer. Decrement the right hand pointer. Repeat until you find a mismatch, or the 2 pointers cross each other.



No need for string.h to do that.
?
2016-10-27 15:49:48 UTC
First, you desire a respectable IDE (integrated progression ecosystem) that you need to step by ability of code to ensure what that's doing. 2d, your code as offered the following is amazingly complicated to study. No tabbing. with somewhat of luck that's only a effect of your decrease and paste the following. on your search for recurring, you've that for loop. The IF statement contained in the for loop returns no be counted what. It in no way exams the 2d value because it lower back. What you want to do is go back on condition that you got here around the call. Then in case you fall out of the for loop, you probably did not locate it. In different words, get rid of the else difficulty and placed the go back -a million after the for. also, look at the scope of your variables. the position does Number_entrys get set? It would not depart the function you placed it in.
MT
2011-03-04 20:18:45 UTC
I guess you can loop through the string and compare them character by character? You can loop until you find a mismatch or a '\0'.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...