Check if a string is palindrome or not is one of most common question we encountered during our basic days.
Let's Solve this problem with a Naive but efficient approach in this case.
Pseudocode :
- i=0, j=n
- while i < j:
- check if i'th element is not equal to j'th
- then return False
- else i++,j--
- loop end
- return True
Source Code in C:
We implemented both versions of palindrome check using for loop and while loop.
- #include<stdio.h>
- int isPalindrome(char arr[],int size){
- //Using for loop
- int i,j;
- for(i=0,j=size-1;i<j;i++,j--){
- if(arr[i] != arr[j]){
- return 0;
- }
- }
- return 1;
- }
- int isPalindrome2(char arr[],int size){
- //using While loop
- int i,j;
- i=0;
- j=size-1;
- while(i<j){
- if(arr[i] != arr[j]){
- return 0;
- }
- else{
- i++;
- j--;
- }
- }
- return 1;
- }
- int main(){
- char arr[4]={"abba"};
- char arr2[5]={"abvba"};
- if(isPalindrome(arr,4)){
- printf("\n%s : is a Palindrome",arr);
- }else{
- printf("\nIt is NOT a Palindrome");
- }
- if(isPalindrome2(arr2,5)){
- printf("\n%s : is a Palindrome",arr2);
- }else{
- printf("\nIt is NOT a Palindrome");
- }
- }
Output:
isPalindrome |
Thank you for this interesting small tip! Such little actions can fasten my working process twice! I really believe that this information is a future of language translation and a lot of people will use it in future. Thank you for making this early review.
ReplyDeleteyang kurang bagus dimana kans kemenangannya akan rendah. membaca paduan dari dasar Poker Walaupun
ReplyDeleteasikqq
dewaqq
sumoqq
interqq
pionpoker
bandar ceme terpercaya
hobiqq
paito warna
http://199.30.55.59/interqq78/
datahk