Easy example of strstr(),strspn(),strrchr(),strchr(),strbrk(),memcpy(),memset(),memcmp() - #include

I have written quick example of few function such as strspn(),strrchr(),strchr(),strbrk(),memcpy() etc of C language.These function we often use for dealing with string. These code are very easy to read and write. I am just pasting the code here, Please read the comments and if you have any questions , please post comment!


strstr():


/*
* use and example of strstr() function
*/

/*
* The strstr() function finds the first occurrence of the substring needle in the string haystack.
* The terminating null bytes ('\0') are
* not compared.
*/


#include
#include
int main(){
char *str="what the hell! system got hacked!!!";
char *str2="what";
char *str3="system";
printf("\n%s\n\n",strstr(str,str3));
printf("%s\n\n",strstr(str,str2));
return 0;
}

/* gcc strstr1.c -o strstr1
* ./strstr1
* system got hacked!!!
*
* what the hell! system got hacked!!!
*
*/





strspn():



/* The use and example of strspn() */
/*search a string for a set of bytes. The strspn()
* function calculates the length (in bytes) of the initial segment of s which consists entirely of bytes in accept. */


#include
#include

int main(){
char *str="C is a greate system language 1337";
char *str2="1234567890";
printf("Lets see %s\n",strspn(str,str2));
return 0;

}




strrchr():


/*use and example of strrchr()*/
/*
* The strrchr() function returns a pointer to the last occurrence of the character c in the string s.
* This will search the char from last. For example if we search 'a' then it will point you "ammer"
* from the "programmer"

*/


#include
#include

int main(){
char *str="You are the programmer";
int str1;
printf("Enter a char:");
scanf("%c",&str1);
//int search=strrchr(str,str1);
printf("\'%c\' found in \'%s\'\n",str1,strrchr(str,str1));
return 0;
}

/*
* pusheax@programming:~/codes/linux1blog$ gcc strrchar.c -o strrchar
* pusheax@programming:~/codes/linux1blog$ ./strrchar
* Enter a char:a
'a' found in 'ammer'
pusheax@programming:~/codes/linux1blog$ ./strrchar
Enter a char:u
'u' found in 'u are the programmer'
pusheax@programming:~/codes/linux1blog$ ./strrchar
Enter a char:o
'o' found in 'ogrammer'
pusheax@programming:~/codes/linux1blog$ ./strrchar
Enter a char:y
'y' found in '(null)'
pusheax@programming:~/codes/linux1blog$ ./strrchar
Enter a char:Y
'Y' found in 'You are the programmer'
pusheax@programming:~/codes/linux1blog$
*/



strchr():




/* The use and example of strchr() */

#include
#include /*include string.h for all the string related function*/

int main(){
char *strng="Mr. Stupid!"; //We will search the char in this string
char secstr='S'; //Char should be closed in single quote
int search=(strchr(strng,secstr) != NULL); //The strchr() is the search function

if (!search) //Compare if search variable is not true
printf("The char is not found!\n");
else //Otherwise it is true
printf("Wow the char \'%c\' found in strng \"%s\"\n",secstr,strng);

return 0;
}


strbrk():




/* use and example of strpbrk() */
/* strpbrk - search a string for any of a set of bytes */

#include
#include

int main(){
char *str="Programming is another best way to learn hacking";
char *str2="b";
int search=*strpbrk(str,str2); //Is "b" in str2 in str?
if (!search)
printf("Nothing!\n");
else
printf("Found \'%c\' in \"%s\"\n",search,str); //Yes it is, well print that what char it is. strpbrk is pointer to the char!
return 0;
}




memcpy():



/*Use and example memcpy() and memmove()*/

/*memcpy - copy memory area*/

//memcpy() does not check the boundary. Be careful!

#include
#include


int main(){
char str[10];
char str1[]="Hello all hackers!";
memcpy(str,str1,sizeof(str1));
printf("%s\n",str);
return 0;
}



memset():



//Use and example of memset()
//memset - fill memory with a constant byte

#include
#include

int main(){
char str[]="Life is boring!";
int str1='A';
printf("First string:%s\n",str);
printf("Now it is:%s\n",memset(str,str1,sizeof(str1)));
return 0;
}

memcmp():


#include 
#include

int main(){
char str[]="ABa";
char str1[]="AbA";
int what=memcmp(str,str1,sizeof(str));
if(what)
printf("Return:%d not matched\n",what);
else
printf("Return:%d mean equal\n",what);

printf("Lets print something different!\n");
printf("Confused for:%d ?\n",memcmp(str,str1,2));
printf("Another confusion for :%d ?\n",memcmp(str,str1,1));
return 0;
}