Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. /*
  4. converts char to integer position. then integer value is index for the storage of char. this way no need to go through nested loops.
  5. can achieve better if array length is 27....
  6.  
  7. */
  8. void main(){
  9.   char str[255];
  10.   int strCount[255];
  11.   for(int p=0;p<=255;p++){
  12.     str[p] = '^';
  13.     strCount[p]=0;
  14.   }
  15.   char testString[]="civic";
  16.  
  17.   for(int j=0;j<strlen(testString);j++){
  18.     int p = testString[j];
  19.     if(strCount[p]>2){
  20.       str[p]='^';
  21.     }
  22.     if(str[p] == '^'){
  23.       str[p]=testString[j];
  24.       strCount[p]++;
  25.     }else if(str[p] != '^'){
  26.       strCount[p]++;
  27.     }
  28.   }
  29. int counter=0;
  30.   for(int l=0;l<=200;l++){
  31.     if(str[l] != '^'){
  32.       counter++;
  33.     }
  34.   }
  35.  
  36.   int krakoen = strlen(testString)%2==1?(strlen(testString)/2)+1:(strlen(testString)/2);
  37.   if(krakoen == counter ){
  38.     printf("word is palindrome \n");
  39.   }
  40.  
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement