Exemple #1
0
/*
*	var: char* & const char*
* return: char*
*	Copies the characters from source into destination.
*/
char* new_strcpy(char* destination, const char* source)
{
	int length = (int) new_strlen(source);
	int i;
	for(i = 0 ; i < length + 1 ; i++) //adds each character from source to the destination
		destination[i] = source[i];

	return destination; //returns the new copy
}
Exemple #2
0
/*
*	var: const char* & int
* return: char*
*	Returns a pointer to the first occurrence of character (converted to a char) in string or a NULL pointer if character cannot be found. 
*/
char* new_strchr(const char* string, int character)
{
	int i;
	int length = new_strlen(string);

	for(i = 0 ; i < length ; i++) //goes through the length of the string
		if(string[i] == (char) character)
			return (char*) &string[i];	//if it reaches a character that matches the character provided, it returns the first instance of the character.
	
		return NULL; //otherwise returns null
}
Exemple #3
0
/*
*	var: char* & const char* & size_t
* return: char*
*	Adds the string contained in source to the end of the string contained in destination, but adding a maximum of n characters. 
*/
char* new_strncat(char* destination, const char* source, size_t n)
{
	size_t length = new_strlen(destination);
	size_t i;

	for (i = 0 ; i < n && source[i] != '\0' ; i++) //goes through the source until null character or n number of times
		destination[length + i] = source[i]; //adds the character in source to the end of destination
	destination[length + i] = '\0'; //adds a null character at the very end

	return destination;
}
Exemple #4
0
/*
*	var: const char* & const char*
* return: char*
*	Returns a pointer to the first occurrence of the string contained in needle in haystack or a NULL pointer if needle cannot be found.
*/
char* new_strstr(const char* haystack, const char* needle)
{
	int i;
	for(i = 0 ; i < new_strlen(haystack) ; i++) //goes through the length of the haystack
		{
			int j = 0;
			if(haystack[i] == needle[j]) //if it finds the first character in needle
			{
				while(haystack[i+j] == needle[j]) //loop through the and check to see if the next characters match.
					j++;
				if(needle[j] == '\0') //if the null character in needle is reached, that means we have a match
					return (char*) &haystack[i]; //return the location in haystack where the needle is
			}
		}
	return NULL; //otherwise return null
}
Exemple #5
0
int test_strlen(char* str, int log)
{
	int return1, return2;

	return1 = strlen(str);
	return2 = new_strlen(str);

	if(log)
	{
		printf("strlen:     str: %s  return: %d\n", 
				str, return1);
		printf("new_strlen: str: %s  return: %d\n", 
				str, return2);
	}

	return return1==return2;
}