Пример #1
0
char *myStrstr(char * first, char *second){

    /*Compute the lengths of the strings*/
    int lengthF = myStrlen(first);
    int lengthS = myStrlen(second);

    /*Compute the pattern vector for the second string*/
    int *p = pattern(second);

    /*f_index is an iterator for the first string
      s_index is an iterator for the second string*/
    int f_index = 0, s_index = 0;

    /*Iterate the first string*/
    while(f_index < lengthF){

        /*If the current characters match,
          advance to the next characters*/
        if(second[s_index] == first[f_index])
        {
            f_index++;
            s_index++;
        }

        /*A match of the string was found*/
        if(s_index == lengthS){
            free(p);    /*Free the memory used for the pattern vector*/

            /*Return a pointer to the match*/
            return first + f_index - s_index;
        }
        /*If characters do not match anymore*/
        else if(f_index < lengthF && second[s_index] != first[f_index]){

            /*If we may still have a pattern*/
            if(s_index)
                /*Try a shorter prefix suffix*/
                s_index = p[s_index - 1];
            else
                /*Go to the next character in the first string*/
                f_index++;
        }
    }

    /*Deallocate the memory for the pattern vector*/
    free(p);

    /*No pattern found means returning NULL*/
    return NULL;

}
Пример #2
0
char* myStrmove( char * const dest, const char * const src )
{
	if ( dest == src )
	{
		return dest;
	}

	if ( dest < src )
	{
		size_t index = 0;
		while ( (dest[index] = src[index]) != '\0' )
		{
			++index;
		}
	} 
	else
	{
		for ( int i=(int)myStrlen(src); i>-1; --i )
		{
			dest[i] = src[i];
		}
	}

	return dest;
}
Пример #3
0
char* myStrmove( char * const dest, const char * const src )
{
	if( dest == src )
	{
		return dest;
	}
	else
	{
		size_t index = 0;

		if( dest > src )
		{
			size_t srcLen = myStrlen( src );
			for ( int i=(int)srcLen; i>-1; --i )
			{
				dest[i] = src[i];
			}
		}
		else
		{
			while ( (dest[index] = src[index]) != '\0' )
			{
				++index;
			}
		}
	}

	return dest;
}
Пример #4
0
int main(){
	char* source = "abc";
	char* destination=malloc((strlen(source)+1)*sizeof(char));
	destination=mystrcpy(destination,source);

	printf("%d \n",myStrlen(destination));
	printf("%s \n",myStrdup(source));
}
Пример #5
0
int main()
{
    char temp[256];
    printf("Please enter a sentence: ");
    gets(temp);
    printf("The sentence entered is %u characters long.\n",myStrlen(temp));
    return 0;
}
Пример #6
0
void myStrlwr(char *str){

    int stringSize = myStrlen(str);

    for(int index = 0; index < stringSize; index++){
        str[index] = myTolower( str[index] );
    }
}
Пример #7
0
char *reverse(char *str)
{
	int end = (int)myStrlen(str);
	char *ret = malloc((unsigned long)end);
	for (int i=0; str[i]; ++i)
	{
		ret[end-i-1] = str[i]; // Subtract 1 because ret[end] returns 0
	}
	return ret;
}
Пример #8
0
char* myStrcat( char * const dest, const char * const src )
{
	size_t destLen = myStrlen( dest );
	size_t index = 0;
	while ( (dest[destLen+index] = src[index]) != '\0' )
	{
		++index;
	}

	return dest;
}
Пример #9
0
char *switchCases(char *str)
{
	char *ret = malloc(myStrlen(str));
	for (int i=0; str[i]; ++i)
	{ // Excuse the clump below. I saw no reason in using curly braces when all I'm doing is one thing each.
		if (islower(str[i])) ret[i]=(char)toupper(str[i]);
		else if (isupper(str[i])) ret[i]=(char)tolower(str[i]);
		else ret[i] = str[i];
	}
	return ret;
}
Пример #10
0
char *extracttoend(char *str, int start) // Used for myStrchr and myStrstr
{
    unsigned long lenstr = myStrlen(str);
    char *extract = (char *) malloc((lenstr-(unsigned long)start+1));
	// Dynamically allocate memory that wouldn't be erased. Size of string should just fit.
    int beg = start;
    for (int i=0; i<((int)lenstr-start+1); ++i)
    {
        extract[i] = str[beg];
        ++beg;
    }
    return extract;
}
Пример #11
0
/**
 * @brief               Generates the message for the the ID of the system.
 *
 * @param[out] Cbuff    Pointer to the circular buffer to put the data in.
 * @return              HAL_FAILED if the message didn't fit or HAL_SUCCESS
 *                      if it did fit.
 */
static bool GenerateGetDeviceInfo(circular_buffer_t *Cbuff)
{
    const uint32_t size = 6;
    uint8_t header[2];
    uint8_t *ptr_list[size];
    uint32_t len_list[size];
    uint32_t data_count, i;
    uint16_t crc16;

    /* The strings are at known locations. */
    ptr_list[0] = header;
    ptr_list[1] = (uint8_t *)ptrGetUniqueID();
    ptr_list[2] = (uint8_t *)ptrGetBootloaderVersion();
    ptr_list[3] = (uint8_t *)ptrGetFirmwareVersion();
    ptr_list[4] = ptrGetUserIDString();
    ptr_list[5] = (uint8_t *)&crc16;

    /* Find the length of the strings, + 1 for the null byte. */
    len_list[0] = 2;
    len_list[1] = UNIQUE_ID_SIZE;
    len_list[2] = myStrlen(ptr_list[2], VERSION_MAX_SIZE) + 1;
    len_list[3] = myStrlen(ptr_list[3], VERSION_MAX_SIZE) + 1;
    len_list[4] = myStrlen(ptr_list[4], USER_ID_MAX_SIZE) + 1;
    len_list[5] = 2;

    /* Calculate data size. */
    data_count = UNIQUE_ID_SIZE + len_list[2] + len_list[3] + len_list[4] ;

    /* Fill header and build the CRC. */
    header[0] = Cmd_GetDeviceInfo;
    header[1] = (uint8_t) data_count;

    crc16 = CRC16_START_VALUE;

    for (i = 0; i < size - 1; i++)
        crc16 = CRC16_chunk(ptr_list[i], len_list[i], crc16);

    return GenerateSLIP_MultiChunk(ptr_list, len_list, size, Cbuff);
}
Пример #12
0
int _tmain(int argc, _TCHAR* argv[])
{
	int c[10] = { 1,2,3,4,5,6,7,8,9,10 };
	int d[10];

	myMemcpy( d, c, sizeof( int )*10 );
	outputArray( c, 10 );
	outputArray( d, 10 );

	printf( "%d\n", myMemcmp( c, d, sizeof( int )*10 ) );

	myMemmove( c, &c[2], sizeof( int )*8 );
	outputArray( c, 10 );

	myMemmove( &c[2], c, sizeof( int )*8 );
	outputArray( c, 10 );

	myMemset( d, 0, sizeof( int )*10 );
	outputArray( d, 10 );

	char a[100] = "123456789";
	char b[100] = "abcdefghijklmn";

	printf( "%d\n", myStrlen( a ) );
	printf( "%d\n", myStrlen( b ) );

	printf( "%s\n", myStrcat( b, a ) );
	//printf( "%s\n", myStrcat( a, &a[2] ) );
	//printf( "%s\n", myStrcat( &a[2], a ) );
	printf( "%s\n", a );
	printf( "%d\n", myStrcmp( b, a ) );
	printf( "%s\n", myStrcpy( b, a ) );
	printf( "%s\n", myStrmove( a, &a[3] ) );
	printf( "%s\n", myStrmove( &a[4], a ) );
	printf( "%s\n", a );
	
	return 0;
}
Пример #13
0
char *replaceDigits(char *str, int c)
{
	char *ret = malloc(myStrlen(str));
	for (int i=0; str[i]; ++i)
	{
		if (isdigit(str[i]))
		{
			ret[i] = (char)c;
			continue;
		}
		ret[i] = str[i];
	}
	return ret;
}
Пример #14
0
char *removeSpaces(char *str)
{
	char *ret = malloc(myStrlen(str)); // This will likely not completely fill up
	int spaces = 0; // Keep track of spaces so chars placed in ret would be in the right position
	for (int i=0; str[i]; ++i)
	{
		if (isspace(str[i]))
		{
			++spaces;
			continue;
		}
		ret[i-spaces] = str[i];
	}
	return ret;
}
Пример #15
0
char *removeVowels(char *str)
{
	int count = 0; // Same concept used in removeSpaces
	char *ret = malloc(myStrlen(str));
	for (int i=0; str[i]; ++i)
	{
		if (isvowel(str[i]))
		{
			++count;
			continue;
		}
		ret[i-count] = str[i];
	}
	return ret;
}
Пример #16
0
void tests()
{
	char a[] = {"String."};
	printf("%d\n", myStrlen(a));

	char b[20] = {0};
	myStrcpy(b, a);
	printf("%s\n", b);

	char c[20] = {0};
	myStrcpy(c, a);
	myStrcat(c, b);
	printf("%s\n", c);

	printf("%d\n", myStrcmp(a, b));
	printf("%d\n", myStrcmp(b, c));
}
Пример #17
0
void myStrrev(char *str){

    char *strEnd = str + myStrlen(str) - 1;
    char auxCharacter;


    while(str < strEnd){

        auxCharacter = *str;
        *str = *strEnd;
        *strEnd = auxCharacter;

        str++;
        strEnd--;
    }

}
Пример #18
0
/*Creates the pattern of a string
  The pattern will be used only by the myStrstr() function
  As a result, the user of the library
  will not have access to this function*/
int *pattern(char *str){
    /*Length of the string*/
    int n = myStrlen(str);

    /*Allocate memory for the pattern vector*/
    int *p = malloc(n * sizeof(int));

    p[0] = 0;

    int index = 1, currentLen = 0;

    /*Iterate from 1 to n-1*/
    while(index < n){

        /*If characters match*/
        if(str[currentLen] == str[index]){
            /*Set the length of the current prefix suffix*/
            currentLen++;
            p[index] = currentLen;
            index++;
        }

        else{
            /*Try to find a suffix of the current prefix with
              a smaller length*/
            if(currentLen != 0){
                currentLen = p[currentLen - 1];
            }
            /*All the suffixes of the current prefix were tried
              and no match found*/
            else{
                p[index] = 0;
                index++;
            }
        }
    }
    /*Return the pattern vector*/
    return p;
}
Пример #19
0
char* myStrtok(char *str, char *delimiters){
    static char *current, *next, *endStr;

    /*Check if this is the first call for a string*/
    if(str != NULL){
        /*If so, compute the pointer to the position of
          the '\0' character (string terminator) -> endPosition*/
        endStr = str + myStrlen(str);

        /*Set the next pointer to be returned and the
          to be the beginning of the string*/
        next = str;

        /*Skip the charactern in the beginning of the string
          which are delimiters*/
        while(next < endStr && myStrchr(delimiters, *next))
            next++;

        /*If the end of the string was reached return NULL*/
        if(next == endStr)
            return NULL;

        current = next;
    }
    else{
        /*Return the NULL pointer if the string cannot be split anymore*/
        if(current == endStr){
            return NULL;
        }

        /*Get the next part of the string*/
        current++;

        /*Save the pointer to it to next*/
        next = current;
    }

    /*While the end of the string hasn't been reached and a delimiter
      hasn't been found keep iterating*/
    while(*current){

        /*If the current substring is valid (starts with a non-delimiter, ends
          right before a delimiter)
          Stop iterating*/
        if(!myStrchr(delimiters, *next) && myStrchr(delimiters, *current))
            break;

        /*If next points to a delimiter,
          skip to the following character*/
        if(myStrchr(delimiters, *next)){
            next = current;
        }
        current++;
    }

    /*Make the character at the current position a string terminator*/
    *current = '\0';

    /*Return the pointer to the next part of the string*/
    /*CORNER CASE: current reached the end of the string, but
      the character to which next is pointing is a delimiter
      CORNER CASE: next reaches the string terminator*/
    return myStrchr(delimiters, *next) || *next == '\0'? NULL : next;
}
Пример #20
0
int main() {
  printf("String length of pie = %d\n", myStrlen("pie"));

  return 0;
}