The Set of Functions in string.h C Language

Keywords: less

String Copy 1
@ Function name: strdup
Function prototype: char *strdup(const char *s)
Function function: String copy, the destination space is allocated by the function
Function return: a string pointer to a copy
Parametric description: src - source string to be copied
Subordinate documents:

#include <stdio.h>
#include <string.h>
#include <alloc.h>
int main()
{
  char *dup_str, *string="abcde";
  dup_str=strdup(string);
  printf("%s", dup_str);
  free(dup_str);
  return 0;
}

String Copy 2
@ Function name: strcpy
Function prototype: char* strcpy(char* str1,char* str2);
Function: Copy the string pointed to by str2 to str1
Function return: return str1, the pointer to STR1
Description of parameters:
Subordinate documents:

#include <stdio.h>
#include <string.h>
int main()
{
  char string[10];
  char *str1="abcdefghi";
  strcpy(string,str1);
  printf("the string is:%s/n",string);
  return 0;
}

String Copy 3
@ Function name: strncpy
Function prototype: char *strncpy(char *dest, const char *src, int count)
Function: Copy count characters from the string src to the string dest
Function return: a pointer to dest
Parametric description: dest - destination string, src - source string, count - number of characters copied
Subordinate documents:

#include <stdio.h>
#include <string.h>
int main()
{
  char string[10];
  char *str1="abcdefghi";
  strncpy(string,str1,3);
  string[3]='/0';
  printf("%s",string);
  return 0;
}

String connection 1
@ Function name: strcat
Function prototype: char* strcat(char * str1,char * str2);
Function function: connect str2 to str1, and the last'/0'of STR1 is cancelled
Function return: str1
Description of parameters:
Subordinate documents:

#include <stdio.h>
#include <string.h>

int main()
{
  char buffer[80];

  strcpy(buffer,"Hello ");
  strcat(buffer,"world");
  printf("%s/n",buffer);
  return 0;
}

String connection 2
@ Function name: strncat
Function prototype: char *strncat(char *dest, const char *src, size_t maxlen)
Function Function: Connect the first maxlen characters in the string src to dest
Function returns:
Description of parameters:
Subordinate documents:

#include <stdio.h>
#include <string.h>

char buffer[80];

int main()
{
  strcpy(buffer,"Hello ");
  strncat(buffer,"world",8);
  printf("%s/n",buffer);
  strncat(buffer,"*************",4);
  printf("%s/n",buffer);
  return 0;
}

String comparison 1
@ Function name: strcmp
Function prototype: int strcmp(char * str1,char * str2);
Function: Compare two strings str1,str2.
Function return: str1

#include <string.h>
#include <stdio.h>
int main()
{
  char *buf1="aaa", *buf2="bbb", *buf3="ccc";
  int ptr;
  ptr=strcmp(buf2, buf1);
  if(ptr>0)
    printf("buffer 2 is greater than buffer 1/n");
  else
    printf("buffer 2 is less than buffer 1/n");
  ptr=strcmp(buf2, buf3);
  if(ptr>0)
    printf("buffer 2 is greater than buffer 3/n");
  else
    printf("buffer 2 is less than buffer 3/n");
  return 0;
}

String comparison 2
@ Function name: strncmp
Function prototype: int strncmp(char *str1,char *str2,int count)
Function: Compare the first count characters in str1 and str2 in dictionary order
Function return: less than 0:str1

#include <string.h>
#include <stdio.h>
int main()
{
  int ptr;
  char *buf1="aaabbb",*buf2="bbbccc",*buf3="ccc";
  ptr=strncmp(buf2,buf1,3);
  if (ptr>0)
    printf("buffer 2 is greater than buffer 1");
  else
    printf("buffer 2 is less than buffer 1");
    ptr=strncmp(buf2,buf3,3);
  if (ptr>0)
    printf("buffer 2 is greater than buffer 3");
  else
    printf("buffer 2 is less than buffer 3");
  return(0);
}

String lookup 1
@ Function name: strpbrk
Function prototype: char *strpbrk(const char *s1, const char *s2)
Function function: Search for the position of the first character matching any character in the string s1, excluding the empty character NULL.
Function return: Returns a pointer to the first matching character in s1 and a null pointer NULL if no matching character exists.
Description of parameters:
Subordinate documents:

#include <stdio.h>
#include <string.h>
int main()
{
char *p="Find all vowels";

while(p)
{
  printf("%s/n",p);
  p=strpbrk(p+1,"aeiouAEIOU");
}
return 0;
}

String lookup 2
@ Function name: strcspn
Function prototype: int strcspn(const char *s1, const char *s2)
Function function: counting the length of s1 from scratch to the first "character from s2"
Function return: length
Description of parameters:
Subordinate documents:

#include <stdio.h>
#include <string.h>

int main()
{
  printf("%d/n",strcspn("abcbcadef","cba"));
  printf("%d/n",strcspn("xxxbcadef","cba"));
  printf("%d/n",strcspn("123456789","cba"));
  return 0;
}

String lookup 3
@ Function name: strspn
Function prototype: int strspn(const char *s1, const char *s2)
Function function: counting the length of s1 from scratch to the first "character not from s2"
Function return: position pointer
Description of parameters:
Subordinate documents:

#include <stdio.h>
#include <string.h>
#include <alloc.h>
int main()
{
  printf("%d/n",strspn("out to lunch","aeiou"));
  printf("%d/n",strspn("out to lunch","xyz"));
  return 0;
}

String lookup 4
@ Function name: strchr
Function prototype: char* strchr(char* str,char ch);
Function: Find out where the first character ch appears in the str-pointed string
Function Return: Returns a pointer to that location, and if it cannot be found, returns a null pointer.
Parametric description: str - string to be searched, ch - character to be searched
Subordinate documents:

#include <string.h>
#include <stdio.h>
int main()
{
  char string[15];
  char *ptr, c='r';
  strcpy(string, "This is a string");
  ptr=strchr(string, c);
  if (ptr)
    printf("The character %c is at position: %d/n",c,ptr-string);
  else
    printf("The character was not found/n");
  return 0;
}

String lookup 5
@ Function name: strrchr
Function prototype: char *strrchr(const char *s, int c)
Function function: Get the last position pointer containing c character in string s
Function return: position pointer
Description of parameters:
Subordinate documents:

#include <string.h>
#include <stdio.h>
int main()
{
  char string[15];
  char *ptr,c='r';
  strcpy(string,"This is a string");
  ptr=strrchr(string,c);
  if (ptr)
    printf("The character %c is at position:%d",c,ptr-string);
  else
    printf("The character was not found");
  return 0;
}

String lookup 6
@ Function name: strstr
Function prototype: char* strstr(char* str1,char* str2);
Function Function: Find out where str2 strings first appear in str1 strings (excluding str2 string terminators)
Function return: A pointer to that location, if not found, returns a null pointer
Description of parameters:
Subordinate documents:

#include <stdio.h>
#include <string.h>
int main()
{
  char *str1="Open Watcom C/C++",*str2="Watcom",*ptr;
  ptr=strstr(str1,str2);
  printf("The substring is:%s/n",ptr);
  return 0;
}

String inversion
@ Function name: strrev
Function prototype: char *strrev(char *s)
Function: Reverse the order of all characters in a string
Function return: pointer to s
Description of parameters:
Subordinate documents:

#include <string.h>
#include <stdio.h>
int main()
{
  char *forward="string";
  printf("Before strrev():%s",forward);
  strrev(forward);
  printf("After strrev(): %s",forward);
  return 0;
}

String padding 1
@ Function name: strnset
Function prototype: char *strnset(char *s, int ch, size_t n)
Function function: Set the first n characters in string s to the value of ch
Function return: pointer to s
Description of parameters:
Subordinate documents:

#include <stdio.h>
#include <string.h>
int main()
{
  char *string="abcdefghijklmnopqrstuvwxyz";
  char letter='x';
  printf("string before strnset: %s",string);
  strnset(string,letter,13);
  printf("string after strnset: %s",string);
  return 0;
}

String padding 2
@ Function name: strset
Function prototype: char *strset(char *s, int ch)
Function function: Set all characters in string s to the value of ch
Function return: pointer to s
Description of parameters:
Subordinate documents:

include

include

#include <string.h>
#include <stdio.h>
int main()
{
  char *p;
  char *buffer;
  char *delims={ " .," };

  buffer=strdup("Find words, all of them.");
  printf("%s/n",buffer);
  p=strtok(buffer,delims);
  while(p!=NULL){
    printf("word: %s/n",p);
    p=strtok(NULL,delims);
  }
  printf("%s/n",buffer);
  return 0;
}

String capitalization can also be done with toupper(int) in ctype.
@ Function name: strupr
Function prototype: char *strupr(char *s)
Function function: Change the character in string s to uppercase
Function returns:
Description of parameters:
Subordinate documents:

#include <stdio.h>
#include <string.h>
int main()
{
  char *string="abcdefghijklmnopqrstuvwxyz",*ptr;
  ptr=strupr(string);
  printf("%s",ptr);
  return 0;
}

String lowercase conversion tolower(int)
@ Function name: strlwr
Function prototype: char *strlwr(char *s)
Function: Change characters in strings to lowercase characters
Function return: pointer to s
Description of parameters:
Subordinate documents:

#include<string.h>
int main()
{
  char str[]="HOW TO SAY?";
  printf("%s",strlwr(str));
  return 0;
}

String length
@ Function name: strlen
Function prototype: unsigned int strlen(char * str);
Function function: counting the number of characters in str string (excluding terminator'/0')
Function return: Returns the length of the string.
Description of parameters:
Subordinate documents:

#include <stdio.h>
#include<string.h>
int main()
{
  char str[]="how are you!";
  printf("the lence is:%d/n",strlen(str));
  return 0;
}

@ Function name: strerror
Function prototype: char *strerror(int errnum)
Function function: Get the content information of the error message
Function return: error message string pointer
Parametric description: errnum - error number
Subordinate documents:

#include <stdio.h>
#include <errno.h>
int main()
{
  char *buffer;
  buffer=strerror(errno);
  printf("Error: %s",buffer);
  return 0;
}

@ Function name: memcpy
Function prototype: void *memcpy(void *dest, const void *src, size_t n)
Function function: string copy
Function return: a pointer to dest
Parametric description: src - source string, maximum length of n - copy
Subordinate documents:

#include <stdio.h>
#include <string.h>
int main()
{
  char src[]="******************************";
  char dest[]="abcdefghijlkmnopqrstuvwxyz0123456709";
  char *ptr;
  printf("destination before memcpy:%s/n",dest);
  ptr=memcpy(dest,src,strlen(src));
  if (ptr)
    printf("destination after memcpy:%s/n",dest);
  else
    printf("memcpy failed");
  return 0;
}

@ Function name: memccpy
Function prototype: void *memccpy(void *dest, const void *src, int c, size_t n)
Function function: Copy string, stop copying when specified length or encounter specified character
Function returns:
Description of parameters: src-source string pointer, c-stop copy check character, n-length, dest-copy background destination string pointer
Subordinate documents:

#include <string.h>
#include <stdio.h>
int main()
{
  char *src="This is the source string";
  char dest[50];
  char *ptr;
  ptr=memccpy(dest,src,'c',strlen(src));
  if (ptr)
  {
    *ptr='/0';
    printf("The character was found:%s",dest);
  }
  else
    printf("The character wasn't found");
  return 0;
}

@ Function name: memchr
Function prototype: void *memchr(const void *s, int c, size_t n)
Function function: find the position of a character c in the first n characters of a string
Function Return: Returns the position pointer of c, returning NULL to indicate that it was not found
Parametric description: s - string to be searched, c - character to be searched, n - specified length
Subordinate documents:

#include <string.h>
#include <stdio.h>
int main()
{
  char str[17];
  char *ptr;
  strcpy(str,"This is a string");
  ptr=memchr(str,'r',strlen(str));
  if (ptr)
  printf("The character 'r' is at position: %d",ptr-str);
  else
  printf("The character was not found");
  return 0;
}

@ Function name: memcmp
Function prototype: int memcmp(const void *s1, const void *s2,size_t n)
Function: Compare the first n bytes of two strings s1 and s2 in dictionary order
The function returns: <0,= 0,> 0 for s1<,=,> s2, respectively.
Parametric description: s1,s2 - string to compare, length of n - Comparison
Subordinate documents:

#include <stdio.h>
#include <string.h>
int main() 
{ 
  char *buf1="ABCDE123"; 
  char *buf2="abcde456"; 
  int stat; 
  stat=memcmp(buf1,buf2,5); 
  printf("The strings to position 5 are "); 
  if(stat) printf("not "); 
  printf("the same/n"); 
  return 0; 
} 

@ Function name: memicmp
Function prototype: int memicmp(const void *s1, const void *s2, size_t n)
Function: Comparing the first n characters of string S1 and S2 in dictionary order without considering letter case
The function returns: <0,= 0,> 0 for s1<,=,> s2, respectively.
Parametric description: s1,s2 - string to compare, length of n - Comparison
Subordinate documents:

#include <stdio.h>
#include <string.h>
int main()
{
  char *buf1="ABCDE123";
  char *buf2="abcde456";
  int stat;
  stat=memicmp(buf1,buf2,5);
  printf("The strings to position 5 are ");
  if(stat) printf("not");
  printf("the same");
  return 0;
}

@ Function name: memmove
Function prototype: void *memmove(void *dest, const void *src, size_t n)
Function function: string copy
Function return: a pointer to dest
Parametric description: src - source string, maximum length of n - copy
Subordinate documents:

#include <string.h>
#include <stdio.h>
int main()
{
  char dest[40]="abcdefghijklmnopqrstuvwxyz0123456789";
  printf("destination prior to memmove:%s/n",dest);
  memmove(dest+1,dest,35);
  printf("destination after memmove:%s",dest);
  return 0;
}

@ Function name: memset
Function prototype: void *memset(void *s, int c, size_t n)
Function function: n bytes in the string are set to c
Function returns:
Description of parameters: s-string to be set, c-setting content, n-length
Subordinate documents:

#include <string.h>
#include <stdio.h>
#include <mem.h>
int main()
{
  char buffer[]="Hello world";
  printf("Buffer before memset:%s/n",buffer);
  memset(buffer,'*',strlen(buffer)-1);
  printf("Buffer after memset:%s",buffer);
  return 0;
}

Posted by dar-k on Tue, 16 Jul 2019 16:19:58 -0700