Пример #1
0
int main(int argc, char **argv) {

    char *str1 = "Waterbottle";
    char *str2 = "erbottleWat";

    assert(isRotation(str1, str2));

    str1 = "Waterbottle";
    str2 = "Waterbottle";

    assert(isRotation(str1, str2));

    str1 = "erbottleWat";
    str2 = "Waterbottle";

    assert(isRotation(str1, str2));

    str1 = "erbottleWate";
    str2 = "Waterbottle";

    assert(!isRotation(str1, str2));

    str1 = "erboterbottle";
    str2 = "erbottleerbot";

    assert(isRotation(str1, str2));

    return 0;
}
Пример #2
0
Файл: 1-8.c Проект: kgraves/ctci
int main() {
  // tests
  char str1[] = "This is a test string ";
  char str2[] = "a test string This is ";
  int actual = isRotation(str1, str2);
  assert(actual == 1);

  char str3[] = "Test";
  char str4[] = "estT";
  actual = isRotation(str3, str4);
  assert(actual == 1);

  char str5[] = "Test";
  char str6[] = "esz";
  actual = isRotation(str5, str6);
  assert(actual == 0);

  return 0;
}
Пример #3
0
int main(int argc, char **argv) {
  FILE *f;
  char line[LINE_SIZE], *p;

  // open file passed as argument
  if (argc < 2 || !(f = fopen(argv[1], "r"))) {
    fprintf(stderr, "Unable to open file argument\n");
    return 1;
  }

  // read lines from file
  while (fgets(line, LINE_SIZE, f)) {
    // possibly remove the trailing '\n'
    if ((p = strchr(line, '\n'))) { *p = '\0'; }

    // skip empty lines
    if (line[0] == '\0') { continue; }

    /*********************/
    /*** DO LINE ***/
    /*********************/

    // parse words from line
    char *delim = ",";
    char *word = strtok(line, delim);
    char *rot = strtok(NULL, delim);

    // find start of rotation
    bool isRot = false;
    for (int i = 0; i <= strlen(rot); i++) {
      if (isRotation(word, rot, i)) {
        isRot = true;
        break;
      }
    }
    if (isRot) {
      printf("True\n");
    } else {
      printf("False\n");
    }
    /**************************/
    /*** /END DO LINE ***/
    /**************************/

  }


  if (ferror(f)) {
    perror("I/O Error");
  }

  return 0;
}
Пример #4
0
int main()
{
	char str1[] = "abcdefg";
	char str2[] = "efgabcd";

	if(isRotation(str1, str2)){
		printf("YES\n");
	}else{
		printf("NO\n");
	}

	return 0;
}
Пример #5
0
int main()
{
  char str1[] = "apple";
  char str2[] = "pleap";
  
  bool8 isRot;
  
  isRot = isRotation(str1, str2);
  
  printf("isRot : %d\n", isRot);
  
  return 0;
}
Пример #6
0
int main(){

	char str1[] = "helloworld";
	char str2[] = "oworldhell";

	int isRot = isRotation(str1, str2);
//	int isSub = isSubstring(str1, str2);

	printf("%d\n", isRot);

//	printf("%d\n", isSub);

	return 0;
}
Пример #7
0
int main(void)
{
  char buffer[MAX_STRING_LENGTH];
  char *stringOneWithNewline = malloc(sizeof(buffer) + 1);
  char *stringTwoWithNewline = malloc(sizeof(buffer) + 1);

  printf("String one (50 characters max): ");
  fgets(stringOneWithNewline, sizeof(buffer), stdin);

  // If the input comprises only the newline character
  if (strlen(stringOneWithNewline) == 1)
  {
    printf("String must have a non-zero length.\n");
    exit(-1);
  }

  // This looks weird, but all it does is tokenize the string
  // on newlines, thereby removing the final "\n" from input
  //
  // I'm reasonably sure strtok isn't thread safe, which is
  // okay for this implementation. For a reentrant (thread
  // safe) version, see strtok_r (under `man strtok`)
  char *stringOne = strtok(stringOneWithNewline, "\n");

  printf("String two (50 characters max): ");
  fgets(stringTwoWithNewline, sizeof(buffer), stdin);

  if (strlen(stringTwoWithNewline) == 1)
  {
    printf("Strings must have a non-zero length.\n");
    exit(-1);
  }

  char *stringTwo = strtok(stringTwoWithNewline, "\n");

  isRotation(stringOne, stringTwo);

  return 0;
}
Пример #8
0
#define CATCH_CONFIG_MAIN
#include "../lib/catch.hpp"
#include "../CtCI5/1_8_string_rotation.cpp"

TEST_CASE( "Test string rotation detection" ) 
{
	SECTION( "Empty string" ) {
		std::string s1 = "";
		std::string s2 = "";

		REQUIRE( isRotation(s1,s2) == true );
		REQUIRE( isRotationV2(s1,s2) == true );

	}

	SECTION( "Strings with inequal length" ) {
		std::string s1 = "abc";
		std::string s2 = "cbaa";

		REQUIRE( isRotation(s1,s2) == false );
		REQUIRE( isRotationV2(s1,s2) == false );
	}

	SECTION( "Rotated String" ) {
		std::string s1 = "waterbottle";
		std::string s2 = "erbottlewat";

		REQUIRE( isRotation(s1, s2) == true );
		REQUIRE( isRotationV2(s1, s2) == true );
	}
Пример #9
0
int main(){
    char a[]="aabbcc";
    char b[]="caabbc";
    printf("%d\n",isRotation(a,b));
    return 0;
}