Please navigate to the bottom of the page for Table of Contents

Saturday, May 21, 2011

String functions galore

In this post we will go over a bunch of string functions. I have added code comments to all of them so that you can follow them without much of an explanation.

Find the first occurrence of a character

// find the first occurrence of a character
int IndexOf(char *source, char searchChar)
{
int currentPos = 0;

// loop through the enite source
while (*source)
{
// if the current character matches, return that position
if (*source == searchChar)
return currentPos;
// no match. move to next position
source++;
currentPos++;
}

// no match was found
return -1;
}


 


Check if two strings are equal


// check if two strings are equal
int IsEqual(char* s1, char* s2)
{
// continue looping till either one string exists
while (*s1 || *s2)
{
// if the chars do not match, return false
if (*s1 != *s2)
return 0;

// chars are equal, move to next char
s1++;
s2++;
}
// we have a match, the strings are equal
return 1;
}


 


Check if the first string is greater than the second


// check if the first string is greater than the second
int IsGreater(char *s1, char* s2)
{
// loop through the entire first string
while (*s1)
{
// if the characters do not match
if (*s1 != *s2)
{
// if the char in first string is greater,
// return success
if (*s1 > *s2)
return 1;
else
return 0;
}
// the characters in both strings were same
// move forward
s1++;
s2++;
}
// first string ended
return 0;
}


 


Check if the first string is smaller than the second


// check if the first string is smaller than the second
int IsSmaller(char *s1, char* s2)
{
// loop through the entire second string
while (*s2)
{
// if the characters do not match
if (*s1 != *s2)
{
// if the char in first string is lesser,
// return success
if (*s1 < *s2)
return 1;
else
return 0;
}
// the characters in both strings were same
// move forward
s1++;
s2++;
}
// second string ended
return 0;
}


 


Find the substring


// return the substring starting at position startPos
// and of length len
char* Substring(char* source, int startPos, int len)
{
// move to the desired start position
char *s = source + startPos;
// initialize the return vale
char *substr = new char[len + 1];

int currentCount = 0;
// loop for the desired length
while (currentCount < len)
{
substr[currentCount] = *s;
s++;
currentCount++;
}
// terminate the return string
substr[currentCount] = '\0';

// return the substring
return substr;
}


 


Extract leftmost n characters from the string


// extract leftmost n characters from the string
char* LeftSubstring(char* source, int len)
{
// initialize the return value
char *substr = new char[len + 1];

int currentCount = 0;
// loop for the desired length
while (currentCount < len)
{
substr[currentCount] = *source;
source++;
currentCount++;
}
// terminate the return string
substr[currentCount] = '\0';

// return the substring
return substr;
}


 


Extract rightmost n characters from the string


// extract rightmost n characters from the string
char* RightSubstring(char* source, int len)
{
// initialize the return value
char *substr = new char[len + 1];
int strLen = strlen(source);

// move to the desired position in the string
char *s = source + (strLen - len);

int currentCount = 0;
// loop for the desired length
while (currentCount < len)
{
substr[currentCount] = *s;
s++;
currentCount++;
}
// terminate the return string
substr[currentCount] = '\0';

// return the substring
return substr;
}


 


Convert string to uppercase


// convert string to uppercase
void ToUpper(char *source)
{
while (*source)
{
// if the char is upper in ASCII, make lower
if (*source >= 97 && *source <= 123)
*source -= 32;
source++;
}
}


 


Convert string to lowercase


// convert string to lowercase
void ToLower(char *source)
{
while (*source)
{
// if the char is lower in ASCII, make upper
if (*source >= 65 && *source <= 91)
*source += 32;
source++;
}
}


 


Reverse a string


// reverse a string 
void Reverse(char *source)
{
int strLen = strlen(source);
char temp; // temp char for swapping
// last char in the string
char *revStr = source + (strLen - 1);

int currPos = 0;
// loop through half the string
while (currPos < strLen / 2)
{
// swap the currPos char with
// the corresponding char position
// from the end
temp = *source;
*source = *revStr;
*revStr = temp;

// increment/decrement the counters
source++;
revStr--;
currPos++;
}
}


 


Replace the first occurrence of a character with a new char


// Replace the first occurrence of a character with a new char 
int Replace(char *source, char oldChar, char newChar)
{
// loop through the entire string
while (*source)
{
// if we have a match?
if (*source == oldChar)
{
// replace with new char
*source = newChar;
return 1;
}
// match not found, move to next position
source++;
}
// no matches found
return 0;
}

2 comments: