Thursday, April 4, 2013

Some Simple C Language String Functions

Here are some C Language functions that I created to use in one of my University Assignments This was a C language assignment and i needed some String functions that i use in java and C#. Ther first one is the IndexOf Function which return the index of a given Character in a string
//Method to find the index of a given char in a string
int IndexOf(char *string,char find)
{
 char *i=strchr(string,find);
 int index=(int)(i-string);
 if(index>=0)
 {
  return index;
 }
 else
 {
  return -1;
 }
}
The second one is the Substring which returns the subsequence of a string
//Method to get the SubString from a given string
char* SubString(int startIndex, int length, char *ch) {
 char *output=malloc(length);
 strncpy(output,&ch[startIndex],length);
 return output;
}
The third one is a function to validate a string using regular expressions You'll need to include the regex.h file to use this
int CheckMatch(char *input,char *format)
{
 regex_t regex;
 regcomp(&regex,format,0);
 int match=regexec(&regex,input,0,NULL,0);
 if( !match ){
                return 1;
        }
        else
        {
         return 0;
        }
}
Please leave a comment