All pastes #588093 Raw Edit

Untitled

public text v1 · immutable
#588093 ·published 2007-06-25 10:15 UTC
rendered paste body
char *StrCopy(char *source, int index, const char *what, int count)
{
	int i;
	for (i = 0; i < count; i++)
		{
			source[index + i] = what[i];
		}
	return source;
}

char *StrDelete(char *source, int index, int count)
{
	int len = strlen(source);
	int i;
	count = (count + index > len) ? len - index : count;
	for (i = index; i + count <= len; i++)
		{
			source[i] = source[i + count];
		}
	return source;
}

char *StrInsert(char *source, int index, const char *what)
{
	int whatLen = strlen(what);
	int sourceLen = strlen(source);
	int i;
	for (i = sourceLen; i >= index; i--)
		{
			source[i + whatLen] = source[i];
		}
	for (i = 0; i < whatLen; i++)
		{
			source[index + i] = what[i];
		}
	return source;
}

char *StrReplace(char *source, const char *what, const char *withWhat)
{
	char *pos;
	int whatLen = strlen(what);
	int withWhatLen = strlen(withWhat);
	int minLen;
	int index;
	
	while ((pos = strstr(source, what)))
		{
			minLen = min(whatLen, withWhatLen);
			StrCopy(source, pos - source, withWhat, minLen); 
			index = pos - source + minLen;
			if (whatLen > withWhatLen)
				{
					StrDelete(source, index, whatLen - withWhatLen);
				}
				else{
					if (whatLen < withWhatLen)
						{
							StrInsert(source, index, withWhat + minLen);
						}
				}
		}
	return source;
}