The summary of string operation in c
C String Function
You guys can access this website [https://beginnersbook.com/2014/01/c-strings-string-functions/] to view some c string functions. Because it not shows all c string function in detail, so I will add the rest c string function in the following article.
strcspn
size_t strcspn(const char *str1, const char *str2)
It searches the str1 for character in str2. It will return the first position.
Example of strcspn:
#include <stdio.h> #include <string.h> int main () { const char str1[] = "ABCDEF4960910"; const char str2[] = "ABC1DEF4960910"; const char str3[] = "013"; int len = strcspn(str1, str3); int len1 = strcspn(str2, str3); printf("The first matching char is in %d\n", len + 1); printf("The first matching char is in %d\n", len1 + 1); return(0); }
Output:
The first matching char is in 10
The first matching char is in 4
strdup
extern char *strdup(char *s);
This function will use malloc method to allocate memory for new char * variables and copy s to it.
Note: The strdup isn’t part of standard C.
Example of strdup:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main () { char *ss = "Golden Global View"; char *s = strdup(ss); if (s) { printf("%s\n", s); free(s); } return(0); }
Output:
Golden Global View
strerror
char *strerror(int errnum)
Errnum is the error number, generally it is errno(It is a global variable in errno.h). The function will return a string which explain the error.
Example of strerror:
#include <stdio.h> #include <string.h> #include <errno.h> int main () { FILE *fp; fp = fopen("file.txt","r"); if( fp == NULL ) { printf("Error: %s\n", strerror(errno)); } return(0); }
Output:
Error: No such file or directory
strpbrk
char *strpbrk(const char *str1, const char *str2)
It searches the str1 for character in str2. It will return the string when it finds the char in str2 in first time .
Example of strpbrk:
#include <stdio.h> #include <string.h> int main () { const char str1[] = "abcde2fghi3jk4l"; const char str2[] = "kd"; char *ret; ret = strpbrk(str1, str2); if(ret) { printf("The first match char is : %c\n", *ret); } else { printf("Not found"); } return(0); }
Output:
The first match char is : d
strspn
int strspn(char *str1, char *str2);
Find the first difference char between str1 and str2, and return the position.
Example of strspn:
#include<stdio.h> #include<string.h> #include<malloc.h> int main(void) { char string1[15]="1234567890"; char string2[15]="123DC8"; int length; length = strspn(string1,string2); printf("Character where strings differ is at position = %d\n",length); return 0; }
Output:
Character where strings differ is at position = 3
strtod
double strtod(const char *str, char **endptr)
This function will get the double value and the rest string separately.
Example of strtod:
#include <stdio.h> #include <stdlib.h> int main() { char str[30] = "23.3333 This is test"; char *ptr; double ret; ret = strtod(str, &ptr); printf("Number (double) is = %lf\n", ret); printf("The string part is |%s|\n", ptr); return(0); }
Output:
Number (double) is = 23.3333
The string part is | This is test|
strtok
char *strtok(char *str, const char *delim)
The str will be split by delim. Note: the str will be destroyed, so if you need to use this str later, you should deep copy this string.
Example of strtok:
#include <string.h> #include <stdio.h> int main () { char str[80] = "Hi Hello World OK!"; const char s[2] = " "; char *token; // Get the first substring. token = strtok(str, s); // Continue to get substring while( token != NULL ) { printf( "%s\n", token ); token = strtok(NULL, s); } return 0; }
Output:
Hi
Hello
World
OK!
strtol
long int strtol(const char *str, char **endptr, int base)
This is similar to strtod, it will get the long value and the rest string separately. You can set the base to 10, we assume the long value is a dec number. if you set the base to 16, we assume the long value is a hex number. The base range is from 2 to 36(included). The value 0 is a particular value, it can on behalf of 10.
Example strtol:
#include <stdio.h> #include <stdlib.h> int main() { char str[30] = "100 This is test"; char *ptr; long ret; ret = strtol(str, &ptr, 36); printf("Number(unsigned long)is %ld\n", ret); printf("The string part is |%s|\n", ptr); ret = strtol(str, &ptr, 2); printf("Number(unsigned long)is %ld\n", ret); ret = strtol(str, &ptr, 0); printf("Number(unsigned long)is %ld\n", ret); ret = strtol(str, &ptr, 10); printf("Number(unsigned long)is %ld\n", ret); return(0); }
Output:
Number(unsigned long)is 1296
The string part is | This is test|
Number(unsigned long)is 4
Number(unsigned long)is 100
Number(unsigned long)is 100
memchr
void *memchr(const void *str, int c, size_t n)
The function will check the str to find the char “c” as unsigned char in n bytes and return the rest string.
Example memchr:
#include <stdio.h> #include <string.h> int main () { const char str[] = "http://settleques.com"; const char ch = '.'; char *ret; ret = (char*)memchr(str, ch, strlen(str)); printf("After %c and then the string is (%s)\n", ch, ret); return(0); }
Output:
After . and then the string is (.com)
memcmp
int memcmp(const void *str1, const void *str2, size_t n)
str1 is the pointer to memory, str2 is the pointer to memory. There are n bytes need to be compared. If str1 is less than str2, it will return negative value. If str1 is greater than str2, it will return positive value. If str1 is equal to str2, it will return 0.
Example of memcmp:
#include <stdio.h> #include <string.h> int main() { char str1[15]; char str2[15]; int ret; memcpy(str1, "abcdef", 6); memcpy(str2, "ABCDEF", 6); ret = memcmp(str1, str2, 5); if(ret > 0) { printf("str2 is less than str1\n"); } else if(ret < 0) { printf("str1 is greater than str2\n"); } else { printf("str1 is equal to str2\n"); } return(0); }
Output:
str2 is less than str1
memcpy
void *memcpy(void *str1, const void *str2, size_t n)
Copy n bytes str2 to str1.
Example of memcpy:
#include <stdio.h> #include <string.h> int main () { const char src[50] = "http://settleques.com"; char dest[50]; memcpy(dest, src, strlen(src)+1); printf("dest = %s\n", dest); return(0); }
Output:
dest = http://settleques.com
memmove
void *memmove(void *str1, const void *str2, size_t n)
The memmove is similar with memcpy, but it can solve the overlap memory in str1 and str2.
memset
void *memset(void *str, int c, size_t n)
Copy the character c (an unsigned character) to the first n characters of the string to which the parameter str refers.
Example of memset
#include <stdio.h> #include <string.h> int main () { char str[50]; strcpy(str,"http://settleques.com"); printf("%s\n", str); memset(str,'0',7); printf("%s\n", str); return(0); }
Output:
http://settleques.com
0000000settleques.com
Have fun.
If you want to know the transition between char *, string, CString, char [], int, double. You can visit this link[ http://settleques.com/index.php/2019/07/04/a-summary-of-the-transition-between-cstring-char-char-string-int-double/ ].