String Functions
This header file contains functions to: Return the n number of characters from the left/right of a character string, remove trailing and leading spaces and replace a character with a string that is specified. The functions are pretty straight forward.
AI
AI-sammanfattning: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
Källkod
#include <string.h>
#include <math.h>
char * strLeft(char * str,int n)
{
char * tmp;
tmp = strdup(str);
strncpy(tmp + n,"",abs(strlen(tmp) - n));
return tmp;
}
char * strRight(char * str, int n)
{
char * tmp;
tmp = strdup(str);
strrev(tmp);
strncpy(tmp + n,"",strlen(tmp) - n);
strrev(tmp);
return tmp;
}
char * strTrim(char * str)
{
char * tmp;
tmp = strdup(str);
//REMOVE LEADING SPACES
while (strcmp(strLeft(tmp,1)," ") == 0)
{
tmp = strRight(tmp,strlen(tmp) - 1);
}
//REMOVE TRAILING SPACES
while (strcmp(strRight(tmp,1)," ") == 0)
{
tmp = strLeft(tmp,strlen(tmp) - 1);
}
return tmp;
}
char * strReplace(char * str,char FindX, char * ReplaceX)
{
char * tmp, * tmp2, * tmp3;
tmp = strdup(str);
tmp2 = strchr(tmp,FindX);
while (tmp2 != NULL)
{
tmp3 = strLeft(tmp,strlen(tmp)-strlen(tmp2));
tmp2++;
tmp = strdup(tmp3);
strcat(tmp,ReplaceX);
strcat(tmp,tmp2);
tmp2 = strchr(tmp,FindX);
}
return tmp;
}
Upload
Originalkommentarer (3)
Återställd från Wayback Machine