Check if Two Strings are rotation of each other.

Code Snippets 4 U
#include < stdio.h > 
#include < stdlib.h > 
#include < string.h >

  int main() {
    char * str = "Hi how are you?";
    char * str2 = "?uoy era woh iH";
    int strl = strlen(str);
    int str2l = strlen(str2);

    int ifRotation = 1;
    // first simple check
    if (strl != str2l) {
      printf("Not A rotation");
      return 0;
    }
    for (int i = 0; i < strl; i++) {
      if ( * (str + i) != * (str2 + (str2l - i - 1))) {
        ifRotation = 0;
        break;
      }
    }

    if (ifRotation == 0) {
      printf("Not A rotation");
    } else {
      printf("Yes A rotation");
    }

    return 0;
  }

Leave a Reply

Your email address will not be published. Required fields are marked *

six + 4 =