![]() Creating strrev() functionĪs said before, on Linux there is no strrev() function in string.h (because it is not part of the standard library), so we have to implement it ourselves to make the first example work. Take a look at the next example on how to implement your own strrev() function on you own. Note: this example will not work on Linux, because strrev() function doesn’t exist. If so, the input string is a palindrome string. We then have two strings that we can compare with strcmp() to check if the input string is the same as the reversed string. The strrev() function is then used to reverse this second string. The strcpy() functions to copy the input string to a second string. Printf("The reverse of the string is %s.\n", two) Īs you can see the string.h functions strcpy(), strrev() and strcmp() are used. Printf("The entered string %s is not a palindrome.\n", one) If the last digit is equal to the first digit then the number is palindrome, otherwise not.Printf("The entered string %s is a palindrome.\n", one) And the variable c holds the first digit of the number. The variable b holds the middle digit of the three digit number. The statement a=a/10 removes the last digit. Here, variable x holds the last digit of the number. ![]() The statement num=num/10 again removes the last digit and the loop continues as long as n>0. Then the value currently being stored at res is multiplied by 10 and c is added to res. On the second loop, c again stores the last digit of the number. Then the statement num=num / 10 removes the last digit of the digit as for example, 129/10 equals to 12. The variable res is initially 0 so res*10 = 0 c which is equal to res=c. When num>0 the c variable stores the last digit of the number. The loop begins and continues to loop as along as num>0. Another variable chk is assigned the same value as num to check the result at the end of the program. The entered number is assigned to variable num. While loop in C is a pre-test loop where the expression is evaluated then only. Palindrome program in C using while loop. 123 321 So, 123 is not a palindrome number. Example:-5225 5225 So, 5225 is a palindrome number. Here the user is asked to enter a number. Palindrome number:-If the Reverse of a number is equal to the same number then the number is called a palindrome number. ![]() Printf ("n The entered number is NOT PALINDROME.") ![]() ![]() Printf("n The entered number is PALINDROME.") Example 1: Check Palindrome Using while Loop and if…else Statement C program to check whether the number is palindrome or not is shown below. Some examples of palindrome number are: 121, 12321, 515, etc. In the above program, reverse of the number entered by the user is calculated inside while loop which iterates till the value of temp is equal to zero. A palindrome number is a number whose reverse is the original number. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |