You are on page 1of 4

<cstring> (string.

h)
Copying:

strcpy - Copy string


strncpy ( char * destination, const char * source);

strncpy - Copy num characters from source

strncpy ( char * destination, const char * source, size_t num );

Concatenation:
strcat - Concatenate string source to destination

strcat ( char * destination, const char * source );

strncat - Append num characters from source to destination


strncat ( char * destination, const char * source, size_t num );

Comparison:

strcmp - Compare two strings.(return 0 if equals, <0 if lesser, >0 if greater)


size_t strcmp ( const char * str1, const char * str2 );

strncmp - Compare num characters of source in destination(return 0 if equals, <0 if lesser, >0 if
greater)
size_t strncmp ( const char * str1, const char * str2, size_t num );

Searching:
strchr - returns pointer of first occurrence of character in string (ex: char *pch=strchr(str,'s')str+1;)
char * strchr ( char * str, int character );

strrchr - - returns pointer of last occurrence of character in string


char * strrchr ( char * str, int character );
strcspn - Returns the last index of str1 before first occurrence of str2
Int strcspn ( const char * str1, const char * str2 );
strspn {1} - Get span amount of character set from str2 occurrences in str1
int strspn ( const char * str1, const char * str2 );
strpbrk {2} - Returns pointer of first occurrence from characters in str2 inside str1
char * strpbrk ( char * str1, const char * str2 );
strstr {3} - Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not
part of str1.
char * strstr ( char * str1, const char * str2 );
strtok {4} - Split string into tokens based on delimiters
char * strtok ( char * str, const char * delimiters );

Other:

memset {5} - Fill block of memory


void * memset ( void * ptr, int value,
size_t num );
strlen - Get string length
size_t strlen ( const char * str );

<cctype> (ctype.h) {6}


Character classification functions

isalnum - Check if character is


alphanumeric
isalpha - Check if character is
alphabetic
isblank - Check if character is blank
iscntrl - Check if character is a control
character
isdigit - Check if character is decimal
digit
isgraph - Check if character has graphical representation
islower - Check if character is lowercase letter
isprint - Check if character is printable
ispunct - Check if character is a punctuation character
isspace - Check if character is a white-space
isupper - Check if character is uppercase letter
isxdigit - Check if character is hexadecimal digit

Character conversion functions

tolower - Convert uppercase letter to lowercase


toupper - Convert lowercase letter to uppercase

Algorithms
1. Finding occurrences of specific character in string
#include <stdio.h>
#include <string.h>
int main (){
int i;
char strtext[] = "129th";
char cset[] = "1234567890";
i = strspn (strtext,cset);
printf ("%d",i);
}output: 3
2. Finding certain characters in string
#include <stdio.h>
#include <string.h>
int main (){

char str[] = "This is a sample string";


char key[] = "aeiou";
char * pch;
pch = strpbrk (str, key);
while (pch != NULL){
printf ("%c " , *pch);
pch = strpbrk (pch+1,key);
}
}output: i i a a e i

3. Find occurrences of substring

#include <stdio.h>
#include <string.h>
int x=0;
main (){
char str[] ="101101101101101101101";
char * pch;
pch=strstr(str,"101101");
while(pch!=NULL){
x++;
pch=strstr(pch+1,"101101");
}
printf("%d",x);
}output:6
4. Split based on delimiters
#include <stdio.h>
#include <string.h>
int main (){
char str[] ="- This, a sample string.";
char *pch = strtok (str," ,.-");
while (pch != NULL){
printf ("%s+",pch);
pch = strtok (NULL, " ,.-");
}
}output: This+a+sample+string+

5. Fill block of memory


#include <stdio.h>
#include <string.h>
int main (){
char str[] = "almost every programmer should know memset!";
memset (str,'-',6);
puts (str);
}output: ------ every programmer should know memset!

6. Change to uppercase
#include <stdio.h>
#include <ctype.h>
int main (){
int i=0;
char str[]="Test String.\n";

while (str[i]){
if (islower(str[i])) str[i]=toupper(str[i]);
putchar (str[i]);
i++;
}
}output: TEST STRING.

7. Quicksort and Binary Search


#include <stdio.h>
#include <stdlib.h>
int compareints (const void * a, const void * b) {return ( *(int*)a - *(int*)b );} //needed to
compare
int values[] = { 50, 20, 60, 40, 10, 30 };
int main (){
int * pItem;
int key = 40; //key to search
qsort (values, 6, sizeof(int), compareints); //needed to be sort before searched
pItem = (int*) bsearch (&key, values, 6, sizeof(int), compareints);
if (pItem!=NULL)printf ("%d is in the array.\n",*pItem);
else printf ("%d is not in the array.\n",key);
} output: 40 is in the array.

You might also like