void testSubstring()
{
    assert(isSubstring("hello", "hello there"));
    assert(isSubstring("", "hello there"));
    assert(isSubstring("there", "hello there"));
    assert(isSubstring("water", "waterfall"));
    assert(!isSubstring("wafer", "waterfall"));
}
Exemplo n.º 2
0
/********************************************************************************************
*                                                                                           *
*                                             main                                          *
*                                                                                           *
*********************************************************************************************/
int main(void)
{
	char input1[10],input2[10];
	int i=0, j=0, flag=0;
	time_t t0,t;

	t0=time(NULL); // tic

	printf("input1 >>\n");
	scanf("%s",input1);
		while(1){
		if(input1[i]=='\0'){
			break;}
			i++;
		}
	printf("input2 >>\n");
	scanf("%s",input2);
		while(1){
		if(input2[j]=='\0'){
			break;}
			j++;
		}

	isSubstring(input1, input2, i,j);

	t=time(NULL)-t0; // toc
	PrintTime(t);

	return(0);
}
Exemplo n.º 3
0
int main()
{
	const char* string = (char *)malloc(50);
	const char* sub = (char *)malloc(19);
	int a = 8; float b = 9.4444; int c = 2;
	int d;
	printf("Before swapping: a = %d c = %d\n", a , c);
	a = a + c;
	c = a - c;
	a = a - c;
	printf("After swapping: a = %d c = %d\n", a , c);
	string = "This is the company namded apple inc";
	sub = "is";
	printf("Substring: %d\n", isSubstring(string, sub));
	printf("Position of the MSB is: %d\n", msbPos(3));
	printf("Is bit set: %d\n", isBitset(8,4));
	printf("%x to little endian %x\n", a, BigToLittle(a));
	printf("%d is a power of 2: %d\n", a ,isPowerof2(a));
	printf("%s after reversed: %s\n", string, reverse(string));
	printf("%s is palindrome: %d\n", string, isPalindrome(string));
	printf("%d is a prime number: %d\n",a, isPrimeNumber(a));
	primeNumber(20);
	printf("\nFibonacci Series: ");
	fibonacci(15);
	printf("\n");
	printf("Factorial of %d: %d\n", a, factorial(a));
	printf("%s after reversing: %s\n", string, reverseString(string));
	printf("Nearest int of %f is: %d\n", b, nearestInt(b));
	printf("%d", (-1%8));
	return 0;
}
Exemplo n.º 4
0
int isRotation(char *s1, char *s2)
{
  int length = strlen(s1);

  char buffer[MAX_STRING_LENGTH * 2];
  char *doubledString = malloc(sizeof(buffer) + 1);

  strncpy(doubledString, s1, length);
  strncat(doubledString, s1, length);

  // Our lone call to isSubstring
  //
  // Note: this function treats the string as a rotation
  // of itself, e.g. "waterbottle" will return zero and
  // print "waterbottle is a rotation of waterbottle"
  if (isSubstring(doubledString, s2))
  {
    printf("%s is a rotation of %s.\n", s2, s1);
    return 0;
  }
  else
  {
    printf("%s is not a rotation of %s.\n", s2, s1);
    return 1;
  }
}
Exemplo n.º 5
0
bool isRotate(std::string const &str1, std::string const &str2){
    size_t s = str1.size();
    if (s != 0 && str1.size() == str2.size()) {
        return isSubstring(str1+str1, str2);
    }
    return false;
}
Exemplo n.º 6
0
bool isRotation(string s1, string s2) {
	if (s1.length() == s2.length() && s1.length() > 0) {
		string s1s1 = s1 + s1;
		return isSubstring(s1s1, s2);
	}
	return false;
}
Exemplo n.º 7
0
bool isRotation(const std::string& s1, const std::string& s2) {
	int length = s1.length();

	if ((length == s2.length()) && length > 0) {
		std::string newString = s1 + s1;
		return isSubstring(newString, s2);
	}
}
Exemplo n.º 8
0
void JSRopeString::visitFibers(SlotVisitor& visitor)
{
    if (isSubstring()) {
        visitor.append(substringBase());
        return;
    }
    for (size_t i = 0; i < s_maxInternalRopeLength && fiber(i); ++i)
        visitor.append(fiber(i));
}
//若s2由s1旋转而来,则s2一定是s1s1的子串
//s1=xy; s2=yx; s1s1 = xyxy
bool IsRotation(string s1,string s2)
{
	if(s1.size()==s2.size() && s1.size()>0)
	{
		string s1s1 = s1+s1;
		return isSubstring(s1s1,s2);
	}
	return false;
}
Exemplo n.º 10
0
bool isRotation(string s1, string s2){
	if(s1.length() != s2.length()){
		return false;
	}
	// stupid error!!!
	// s1: abc, s2: cab, s1+s1: abcabc
	string s = s1 + s1;
	return isSubstring(s, s2);
}
Exemplo n.º 11
0
void JSRopeString::resolveRopeInternal16(UChar* buffer) const
{
    if (isSubstring()) {
        StringImpl::copyChars(
            buffer, substringBase()->m_value.characters16() + substringOffset(), length());
        return;
    }
    
    resolveRopeInternal16NoSubstring(buffer);
}
Exemplo n.º 12
0
bool isStringRotation(string origString, string rotatedString)
{
	if (origString.length() != rotatedString.length())
		return false;
	if (origString == "")
		return true;

	string newString = origString + origString;
	bool res = isSubstring(newString, rotatedString);
	return res;
}
Exemplo n.º 13
0
int str_check_rotation(const char* s1, const char* s2){
  int len = strlen(s1);
  if(len != strlen(s2)) return FALSE;
  
  char *tmp = (char*)malloc(sizeof(char) * len * 2);
  
  strcpy(tmp, s1);
  strcat(tmp, s1);
  
  return isSubstring(tmp, s2);
}
Exemplo n.º 14
0
bool8 isRotation(char *str1, char *str2)
{
  char str3[NO_OF_CHARS] = "";
  int len = strlen(str1);
  
  if(len == strlen(str2) && len > 0) {
    strcat(str3, str2);
    strcat(str3, str2);
    printf("str3 %s\n", str3);
    
    return isSubstring(str3, str1);
  }
  
  return FALSE;
}
Exemplo n.º 15
0
// Check rotation by using isSubstring(a+a,b)
int isRotation(char a[], char b[]){

    int i,size=0;

    // Get size
    while(*(a+size)) size++;

    // Create a+a string
    char c[2*size+1];
    for(i=0;i<size*2;i++)
        c[i]=a[i%size];
    c[2*size]='\0';
    
    return isSubstring(c,b);
}
Exemplo n.º 16
0
int isRotation(char *str1, char *str2){

	int len1 = strlen(str1);
	int len2 = strlen(str2);
		printf("%s\n", str2);
	
	if(len1 == len2 && len1 > 0){
		char *strtmp = (char*)malloc(sizeof(char) * len1 * 2);
		strcpy(strtmp, str1);
		strcat(strtmp, str1);
		printf("%s\n", str2);
		printf("%s\n", strtmp);
		return isSubstring(strtmp, str2);
	}
	return 0;
}
Exemplo n.º 17
0
bool isRotated(string str1, string str2)
{
	int len1 = str1.length();
	int len2 = str2.length();
	
	string str3 = str1 + str1;
	
	if(len1 != len2)
	{
		return false;
	}
	
	if(isSubstring(str2, str3))
	{
		return true;
	}
	else
	{
		return false;
	}
}
Exemplo n.º 18
0
void JSRopeString::resolveRope(ExecState* exec) const
{
    ASSERT(isRope());
    
    if (isSubstring()) {
        ASSERT(!substringBase()->isRope());
        m_value = substringBase()->m_value.substringSharingImpl(substringOffset(), length());
        substringBase().clear();
        return;
    }
    
    if (is8Bit()) {
        LChar* buffer;
        if (auto newImpl = StringImpl::tryCreateUninitialized(length(), buffer)) {
            Heap::heap(this)->reportExtraMemoryAllocated(newImpl->cost());
            m_value = WTFMove(newImpl);
        } else {
            outOfMemory(exec);
            return;
        }
        resolveRopeInternal8NoSubstring(buffer);
        clearFibers();
        ASSERT(!isRope());
        return;
    }

    UChar* buffer;
    if (auto newImpl = StringImpl::tryCreateUninitialized(length(), buffer)) {
        Heap::heap(this)->reportExtraMemoryAllocated(newImpl->cost());
        m_value = WTFMove(newImpl);
    } else {
        outOfMemory(exec);
        return;
    }

    resolveRopeInternal16NoSubstring(buffer);
    clearFibers();
    ASSERT(!isRope());
}
Exemplo n.º 19
0
bool isRotation(char *str1, char *str2) {

    if (strlen(str1) != strlen(str2)) {
        return false;
    }

    char *p1 = str1;
    char *p2 = str2;

    while (*p2) {
        if (*p2 == *p1) {
            p1++;
            p2++;
        }
        else {
            if (p1 == str1) {
                p2++;
            }
            p1 = str1;
        }
    }

    return isSubstring(str2, p1);
}
bool IsRotation::check(std::string const &lhs, std::string const &rhs){
  return (isSubstring(std::string(lhs+lhs), rhs));
}
Exemplo n.º 21
0
bool isRotation(string s1, string s2) {
    if (s1.length() <= 0 || s1.length() != s2.length()) return false;
    string s3 = s1 + s1;
    return isSubstring(s3, s2);
}