Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int len(char text[]){
- int i;
- for( i = 0 ; text[i] ; ++i );
- return i;
- }
- char upper(char c){
- if( c < 97 || c > 122 ) return c;
- return c-32;
- }
- char lower(char c){
- if( c < 65 || c > 90 ) return c;
- return c+32;
- }
- void sUpper(char text[]){
- int i;
- for( i = 0 ; i < len(text) ; ++i ){
- text[i] = upper(text[i]);
- }
- }
- void sLower(char text[]){
- int i;
- for( i = 0 ; i < len(text) ; ++i ){
- text[i] = lower(text[i]);
- }
- }
- void sReverse(char text[]){
- char output[len(text)];
- int i, idx = len(text);
- output[idx--] = '\0';
- for( i = 0 ; text[i] ; ++i )
- output[idx--] = text[i];
- for( i = 0 ; text[i] ; ++i )
- text[i] = output[i];
- }
- void sSort(char text[], int reverse){
- //using counting sort
- char output[len(text)];
- int count[256] = {0};
- int i;
- for( i = 0 ; text[i] ; ++i )
- ++count[text[i]];
- for( i = 1 ; i <= 255 ; ++i )
- count[i] += count[i-1];
- for( i = 0 ; text[i] ; ++i ){
- output[count[text[i]]-1] = text[i];
- --count[text[i]];
- }
- for( i = 0 ; text[i] ; ++i )
- text[i] = output[i];
- if(reverse)
- sReverse(text);
- }
- int isAnagram(char a[], char b[], int insensitive){
- if( len(a) != len(b) ) return 0;
- if( insensitive ){
- sUpper(a);
- sUpper(b);
- }
- sSort(a, 0);
- sSort(b, 0);
- int i;
- for( i = 0 ; a[i] ; ++i )
- if( a[i] != b[i] ) return 0;
- return 1;
- }
- int main(){
- char x[] = "abAB";
- sSort(x, 0);
- printf("%s\n", x);
- return 0;
- }
Add Comment
Please, Sign In to add comment