Here's a code snippet (a pretty long one) to perform several string operations, like trimming, find next occurence of a substring, get next word, take a substring, remove all occurences of a substring, etc,etc this one is for C++ by the way :
#ifndef STRINGOPERATIONS_H
#define STRINGOPERATIONS_H
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
class StringOperations
{
public:
StringOperations();
~StringOperations();
public:
/** This method finds next occurence of 'substring' in
* 'string' starting at position 'pos'. Returns the position
* where this new occurence starts.
*/
int nextOccurence(char *string, char* substring, int pos);
/** This method is simetric to the previous one.
*/
int previousOccurence(char *string, char *substring, int pos);
/** Copies a part of 'origin' into 'dest'. Starts copying in
* 'start' and finishes in 'end'.
*/
void copyFragment(char *origin, char *dest, int start, int end);
/** Removes the segment [start --- end] from given
* string. This method does not deallocate memory.
*/
void removeSubstring(char *string,int start, int end);
/** Adapted from a function created by David Fillion (
[email protected])
* Returns a substring.
* This method allocates memory.
*/
char *substring (char *buffer, unsigned int start, unsigned int length);
/** This method starts at 'pos' and skips all 'isspace()' characters.
* Places at 'pos' the position of the first non-whitespace and
* returns 0 if corrrect and 1 if there have been any problems.
*/
int skipWhitespaces(char *string, unsigned int *pos);
/** This method returns a string with next word
* from 'string' starting at '*pos'.
* If (string[*pos]) is a whitespace, then returns NULL.
* Sets in '*pos' the position of the first character that
* is not included in returned word.
* This function allocates memory.
*/
char *getNextWord(char *string, unsigned int *pos);
/** This method returns a new string that is a copy
* of the given one until finding 'token'.
* If 'token' is not found, returned sting is a
* copy of the given one.
* This method allocates memory
*/
char *substringUntil(char *string, char *token);
/** This method returns a new string that is
* a substring of 'string' starting at the first
* occurence of 'token' until the end of 'string'.
* If token is not found, it returns NULL.
* This method allocates memory.
*/
char *substringFrom(char *string, char *token);
/** Converts given string to uppercase.
*/
void uppercase(char* string);
/** Removes all whitespaces from string
*/
void trim(char*string);
/** Removes character 'which' from 'string'.
* I don't deallocates memory, simply moves
* character positions.
*/
void removeCharacter(char *string, unsigned int which);
/** This method creates a new string which is a copy
* of the given one except by it's first and last characters
* if they are quotes (simple or double).
* This method allocates memory.
*/
char *removeLeadingQuotes(char *origin);
/** This method encodes the string passed. I transforms
* the '\n' character in '\\n'
* the '\' character in '\\'
* the '"' character in '\"'
* This method allocates memory.
*/
char *encodeText(char *origin);
/** The simetric method to the one above.
* This method allocates memory.
*/
char *decodeText(char *origin);
/** This method returns a new string equal to 'origin' but
* whitout any occurence of the 'toRemove' character.
* This method allocates memory.
*/
char *removeAllOccurences(char *origin, char toRemove);
/** This method returns the number of occurences of character
* 'c' in the given string.
*/
unsigned int howManyTimesAppear(char *string, char c);
};
#endif
/
#include "stringoperations.h"
StringOperations::StringOperations()
{
}
StringOperations::~StringOperations()
{
}
/*************************************************************************/
/** This function copies a part of origin into dest.
* it starts copying in 'start' and ends in 'end'.
*/
void StringOperations::copyFragment(char *origin, char *dest, int start, int end)
{
int i;
for (i=start;i<=end;i++)
{
dest[i-start]=origin
;
}
dest[end-start+1]='\0';
}
/*************************************************************************/
int StringOperations::nextOccurence(char *string, char* substring, int pos)
{
/** Searches the next occurence of "substring" in "string" starting
* at 'pos'
* If it doesn't exist, returns -1
*/
int i;
int j;
int k;
int found;
int longitud;
int longitudSub;
found = 0;
i = pos;
longitud = strlen(string);
longitudSub=strlen(substring);
j = 0;
while ((!found) && (i<longitud))
{
if (string==substring[j])
{
k = i+1;
j++;
found = 1;
while ((i<longitud) && (j<longitudSub) && (found))
{
if (string[k] == substring[j])
{
k++;
j++;
}
else
{
found = 0;
}
}
if (j<longitudSub)
{
found = 0;
}
if (!found)
{
i++;
j=0;
}
}
else
{
i++;
}
}
return(found?i:-1);
}
/*************************************************************************/
int StringOperations:
reviousOccurence(char *string, char *substring, int pos)
{
/** Searches the previous occurence of "substring" in "string" starting
* at 'pos'
* If it doesn't exist, returns -1
*/
int i;
int j;
int k;
int found;
int longitud;
int longitudSub;
found = 0;
i = pos;
longitud = strlen(string);
longitudSub=strlen(substring);
j = longitudSub-1;
while ((!found) && (i>=0))
{
if (string==substring[j])
{
k = i-1;
j--;
found = 1;
while ((i>=0) && (j>=0) && (found))
{
if (string[k] == substring[j])
{
k--;
j--;
}
else
{
found = 0;
}
}
if (j>=0)
{
found = 0;
}
if (!found)
{
i--;
j=longitudSub-1;
}
}
else
{
i--;
}
}
return(found?i:-1);
}
/*************************************************************************/
void StringOperations::removeSubstring(char *string,int start, int end)
{
int i;
int longitud;
int howMany;
howMany=end-start;
longitud = strlen(string);
for (i=start;i<longitud-howMany;i++)
{
string = string[i+howMany+1];
}
}
/*************************************************************************/
/* Function created by David Fillion ([email protected])
Return a substring
Note:
If LENGTH is greater then the length of BUFFER, then a copy of BUFFER
is returned.
If LENGTH < 1, a NULL is returned.
If START < 0, a NULL is returned.
The starting position begins at 1.
*/
char *StringOperations::substring (char *buffer, unsigned int start, unsigned int length)
{
static char *sub;
if ((start > strlen (buffer)))
return NULL;
if (strlen (buffer) < length)
{
sub = (char*) strdup (buffer);
return buffer;
}
sub = (char*)malloc (sizeof (char) * (length + 1));
if (sub == NULL)
{
fprintf (stderr, "substring(): insufficient memory, exiting\n");
exit (1);
}
memset (sub, '\0', length + 1);
/* Copy substring */
buffer += start;
memcpy (sub, buffer, length);
return sub;
}
/*************************************************************************/
int StringOperations::skipWhitespaces(char *string, unsigned int *pos)
{
unsigned int len;
int found;
found = 0;
len = strlen(string);
while ((!found) && (*pos<len))
{
found = !isspace(string[*pos]);
if (!found)
{
(*pos)++;
}
}
return(!found);
}
/*************************************************************************/
char *StringOperations::getNextWord(char *string, unsigned int *pos)
{
unsigned int len;
int found;
unsigned int start;
char *word;
word = NULL;
start = *pos;
found = 0;
len = strlen(string);
while ((!found) && (*pos<len))
{
found = isspace(string[*pos]);
if (!found)
{
(*pos)++;
}
}
// if (found)
// {
(*pos)--;
if (*pos-start>0)
{
word = substring(string,start,(*pos-start+1));
}
(*pos)++;
// }
return(word);
}
/*************************************************************************/
char *StringOperations::substringUntil(char *string, char *token)
{
int until;
until = nextOccurence(string,token,0);
if (until<0)
{
until = strlen(string)+2;
}
return(substring(string,0,until));
}
/*************************************************************************/
char *StringOperations::substringFrom(char *string, char *token)
{
int until;
unsigned int start;
unsigned int end;
char *value;
until = nextOccurence(string,token,0);
if (until<0)
{
value = NULL;
}
else
{
start = until+strlen(token);
end = strlen(string)-start;
value = substring(string,start,end);
}
return(value);
}
/*************************************************************************/
void StringOperations::uppercase(char* string)
{
unsigned int i;
unsigned int len;
len = strlen(string);
for (i=0; i<len;i++)
{
string = toupper(string);
}
}
/*************************************************************************/
void StringOperations::trim(char*string)
{
unsigned int i;
unsigned int len;
len = strlen(string);
i=0;
while (i<len)
{
if (isspace(string))
{
removeCharacter(string,i);
len = strlen(string);
}
else
{
i++;
}
}
}
/*************************************************************************/
void StringOperations::removeCharacter(char *string, unsigned int which)
{
unsigned int i;
unsigned int len;
len = strlen(string);
for (i=which;i<len;i++)
{
string = string[i+1];
}
}
/*************************************************************************/
char *StringOperations::removeLeadingQuotes(char *origin)
{
unsigned int i;
unsigned int len;
unsigned int newLen;
unsigned int begin;
unsigned int end;
char *value;
len = strlen(origin);
newLen = len;
if ((origin[0]=='\'') || (origin[0]=='\"'))
{
newLen--;
begin=1;
}
else
{
begin=0;
}
if ((origin[len-1]=='\'') || (origin[len-1]=='\"'))
{
newLen--;
end = len-1;
}
else
{
end = len;
}
value = (char*)malloc(sizeof(char)*(newLen+1));
for (i=begin; i<end;i++)
{
value[i-begin] = origin;
}
value[i-1] = '\0';
return(value);
}
/*************************************************************************/
char *StringOperations::encodeText(char *origin)
{
unsigned int len;
unsigned int i;
unsigned int newLen;
unsigned int j;
char *newString;
len = strlen(origin);
i=0;
newLen = 0;
while (i<len)
{
switch(origin)
{
case '\n':
newLen +=2;
break;
case '\"':
newLen +=2;
break;
case '\\':
newLen += 2;
break;
default:
newLen++;
break;
}
i++;
}
newString = (char*)malloc(sizeof(char)*(newLen+1));
j = 0;
for (i=0;i<len;i++)
{
switch(origin)
{
case '\n':
newString[j] = '\\';
j++;
newString[j]='n';
j++;
break;
case '\"':
newString[j] = '\\';
j++;
newString[j]='"';
j++;
break;
case '\\':
newString[j] = '\\';
j++;
newString[j]='\\';
j++;
break;
default:
newString[j] = origin;
j++;
break;
}
}
newString[j] = '\0';
return(newString);
}
/*************************************************************************/
char *StringOperations::decodeText(char *origin)
{
char *newString;
unsigned int len;
unsigned int newLen;
unsigned int i;
unsigned int j;
len = strlen(origin);
newLen = 0;
i = 0;
while (i<len)
{
if (i<len-1)
{
if (origin=='\\')
{
switch(origin[i+1])
{
case 'n':
case '\"':
case '\\':
newLen++;
i+=2;
break;
default:
fprintf(stderr,"ERROR\n");
break;
}
}
else
{
newLen++;
i++;
}
}
else
{
newLen++;
i++;
}
}
newString = (char*)malloc(sizeof(char)*(newLen+1));
i=0;
j=0;
while (i<len)
{
if (i<len-1)
{
if (origin=='\\')
{
switch(origin[i+1])
{
case 'n':
newString[j]='\n';
break;
case '\"':
newString[j]='\"';
break;
case '\\':
newString[j]='\\';
break;
default:
fprintf(stderr,"ERROR\n");
break;
}
i++;
}
else
{
newString[j] = origin;
}
}
else
{
newString[j] = origin;
}
i++;
j++;
}
newString[j]='\0';
return(newString);
}
/*************************************************************************/
char *StringOperations::removeAllOccurences(char *origin, char toRemove)
{
char *newString;
unsigned int i;
unsigned int len;
unsigned int j;
len = strlen(origin);
i = howManyTimesAppear(origin,toRemove);
newString = (char*)malloc(sizeof(char)*(len-i+2));
j = 0;
for (i=0;i<len;i++)
{
if (origin!=toRemove)
{
newString[j] = origin;
j++;
}
}
newString[j] = '\0';
return(newString);
}
/*************************************************************************/
unsigned int StringOperations::howManyTimesAppear(char *string, char c)
{
unsigned int value;
unsigned int len;
unsigned int i;
value = 0;
len = strlen(string);
for (i=0;i<len;i++)
{
if (string==c)
{
value++;
}
}
return(value);
}
Sorry if it's al little long but you asked for it.