Saturday 11 February 2017

find a string is colindrome or not?


Colindrome is a string which satisfy this condition
"Colidrome is a word that has 3 alphabets followed by the reverse of the 3 alphabets and so on."


/*
 * This Function will return 0 if not colindrome and return 1 if colindrome.
 */
int find_colindrome(const char *str){
        size_t len = strlen(str);
        if( (len % 6) != 0){
                return 0;
        }
        for(int itr1=0; itr1 < len; itr1+=6){
                for( int itr2=0; itr2 < 3 ; itr2++){
                        if(str[itr1+itr2] != str[itr1+5-itr2]){
                                //printf("%d:%d-%c:%c\n", itr1+itr2, itr1+5-itr2, str[itr1+itr2], str[itr1+5-itr2]);
                                return 0;
                        }
                }
        }
        return 1;
}

Full Source Code


#include <stdio.h>
#include <string.h>

int find_colindrome(const char *str){
        size_t len = strlen(str);
        if( (len % 6) != 0){
                return 0;
        }
        for(int itr1=0; itr1 < len; itr1+=6){
                for( int itr2=0; itr2 < 3 ; itr2++){
                        if(str[itr1+itr2] != str[itr1+5-itr2]){
                                //printf("%d:%d-%c:%c\n", itr1+itr2, itr1-itr2+5, str[itr2+itr1], str[itr1-itr2+5]);
                                return 0;
                        }
                }
        }
        return 1;
}


int main(int argc, char *argv[]){
        printf("%s is %s\n", argv[1], find_colindrome(argv[1])? "Colindrome": "Not a Colindrome");
        return 0;
}

Compile and Output


rajesh@ideapad:~/Rajesh/Blog/colindrome$ gcc colindrome.c 
rajesh@ideapad:~/Rajesh/Blog/colindrome$ ./a.out cappac
cappac is Colindrome
rajesh@ideapad:~/Rajesh/Blog/colindrome$ ./a.out abcdef
abcdef is Not a Colindrome
rajesh@ideapad:~/Rajesh/Blog/colindrome$ ./a.out abccba
abccba is Colindrome
rajesh@ideapad:~/Rajesh/Blog/colindrome$ ./a.out abccba123321
abccba123321 is Colindrome
rajesh@ideapad:~/Rajesh/Blog/colindrome$ ./a.out abccba123456
abccba123456 is Not a Colindrome

No comments:

Post a Comment