miércoles, 18 de mayo de 2022

Ejemplos de las funciones de string.h en c

Ejemplos de las funciones de string.h en c

https://www.codesdope.com/blog/article/predefined-string-functions-of-c-in-stringh-librar/


https://www.tutorialspoint.com/c_standard_library/string_h.htm

Library Functions

Following are the functions defined in the header string.h −

Sr.No.Function & Description
1void *memchr(const void *str, int c, size_t n)

Searches for the first occurrence of the character c (an unsigned char) in the first n bytes of the string pointed to, by the argument str.

2int memcmp(const void *str1, const void *str2, size_t n)

Compares the first n bytes of str1 and str2.

3void *memcpy(void *dest, const void *src, size_t n)

Copies n characters from src to dest.

4void *memmove(void *dest, const void *src, size_t n)

Another function to copy n characters from str2 to str1.

5void *memset(void *str, int c, size_t n)

Copies the character c (an unsigned char) to the first n characters of the string pointed to, by the argument str.

6char *strcat(char *dest, const char *src)

Appends the string pointed to, by src to the end of the string pointed to by dest.

7char *strncat(char *dest, const char *src, size_t n)

Appends the string pointed to, by src to the end of the string pointed to, by dest up to n characters long.

8char *strchr(const char *str, int c)

Searches for the first occurrence of the character c (an unsigned char) in the string pointed to, by the argument str.

9int strcmp(const char *str1, const char *str2)

Compares the string pointed to, by str1 to the string pointed to by str2.

10int strncmp(const char *str1, const char *str2, size_t n)

Compares at most the first n bytes of str1 and str2.

11int strcoll(const char *str1, const char *str2)

Compares string str1 to str2. The result is dependent on the LC_COLLATE setting of the location.

12char *strcpy(char *dest, const char *src)

Copies the string pointed to, by src to dest.

13char *strncpy(char *dest, const char *src, size_t n)

Copies up to n characters from the string pointed to, by src to dest.

14size_t strcspn(const char *str1, const char *str2)

Calculates the length of the initial segment of str1 which consists entirely of characters not in str2.

15char *strerror(int errnum)

Searches an internal array for the error number errnum and returns a pointer to an error message string.

16size_t strlen(const char *str)

Computes the length of the string str up to but not including the terminating null character.

17char *strpbrk(const char *str1, const char *str2)

Finds the first character in the string str1 that matches any character specified in str2.

18char *strrchr(const char *str, int c)

Searches for the last occurrence of the character c (an unsigned char) in the string pointed to by the argument str.

19size_t strspn(const char *str1, const char *str2)

Calculates the length of the initial segment of str1 which consists entirely of characters in str2.

20char *strstr(const char *haystack, const char *needle)

Finds the first occurrence of the entire string needle (not including the terminating null character) which appears in the string haystack.

21char *strtok(char *str, const char *delim)

Breaks string str into a series of tokens separated by delim.

22size_t strxfrm(char *dest, const char *src, size_t n)

Transforms the first n characters of the string src into current locale and places them in the string dest.


Examples


strlen

strlen(s1) calculates the length of string s1.

#include <stdio.h>
#include <string.h>
int main()
{
  char name[ ]= "Hello"; 
  int len1, len2;
  len1 = strlen(name); 
  len2 = strlen("Hello World");
  printf("length of %s = %d\n", name, len1); 
  printf("length of %s = %d\n", "Hello World", len2);
  return 0;
}

Output

length of Hello = 5 
length of Hello World = 11

strlen doesn't count '\0' while calculating the length of a string.

strcat

strcat(s1, s2) concatenates(joins) the second string s2 to the first string s1.

#include <stdio.h>
#include <string.h>
int main()
{
  char s2[ ]= "World"; 
  char s1[20]= "Hello"; 
  strcat(s1, s2);
  printf("Source string = %s\n", s2); 
  printf("Target string = %s\n", s1); 
  return 0;
}

Output

Source string = World 
Target string = HelloWorld

strncat

strncat(s1, s2, n) concatenates(joins) the first ‘n’ characters of the second string s2 to the first string s1.

#include <stdio.h>
#include <string.h>
int main()
{
  char s2[ ]= "World"; 
  char s1[20]= "Hello"; 
  strncat(s1, s2, 2);
  printf("Source string = %s\n", s2); 
  printf("Target string = %s\n", s1); 
  return 0;
}

Source string = World
Target string = HelloWo

strcpy

strcpy(s1, s2) copies the second string s2 to the first string s1.

#include <string.h>
#include <stdio.h>
int main()
{
  char s2[ ]= "Hello"; 
  char s1[10]; 
  strcpy(s1, s2);
  printf("Source string = %s\n", s2); 
  printf("Target string = %s\n", s1); 
  return 0;
}

Output

Source string = Hello
Target string = Hello

strncpy

strncpy(s1, s2, n) copies the first ‘n’ characters of the second string s2 to the first string s1.

#include <string.h>
#include <stdio.h>
int main()
{
  char s2[ ]= "Hello"; 
  char s1[10]; 
  strncpy(s1, s2, 2);
  s1[2] = '\0';   /* null character manually added */
  printf("Source string = %s\n", s2); 
  printf("Target string = %s\n", s1); 
  return 0;
}

Output

Source string = Hello
Target string = He

Please note that we have added the null character (‘\0’) manually.

strcmp

strcmp(s1, s2) compares two strings and finds out whether they are same or different. It compares the two strings character by character till there is a mismatch. If the two strings are identical, it returns a 0. If not, then it returns the difference between the ASCII values of the first non-matching pair of characters.

#include <stdio.h>
#include <string.h>
int main()
{
  char s1[ ]= "Hello"; 
  char s2[ ]= "World"; 
  int i, j;
  i = strcmp(s1, "Hello");
  j = strcmp(s1, s2);
  printf("%d \n %d\n", i, j);
  return 0;
}


-15

The difference between the ASCII values of H(72) and W(87) is -15.

strncmp

strncmp(s1, s2, n) compares the first ‘n’ characters of s1 and s2.

#include <stdio.h>
#include <string.h>
int main()
{
  char s1[ ]= "Hello"; 
  char s2[ ]= "World"; 
  int i, j;
  i = strncmp(s1, "He BlogsDope", 2);
  printf("%d\n", i);
  return 0;
}

Output

0

strchr

strchr(s1, c) returns a pointer to the first occurrence of the character c in the string s1 and returns NULL if the character c is not found in the string s1.

#include <stdio.h>
#include <string.h>
int main()
{
  char s1[ ]= "Hello Blogsdope"; 
  char c = 'B';
  char *p;
  p = strchr(s1, c);
  printf("%s\n", p);
  return 0;
}

Output

Blogsdope

strrchr

strrchr(s1, c) returns a pointer to the last occurrence of the character c in the string s1 and returns NULL if the character c is not found in the string s1.

#include <stdio.h>
#include <string.h>
int main()
{
  char s1[ ]= "Hello Blogsdope"; 
  char c = 'o';
  char *p;
  p = strrchr(s1, c);
  printf("%s\n", p);
  return 0;
}

Output

ope

strstr

strstr(s1, s2) finds the first occurrence of the string s2 in the string s1.

#include <stdio.h>
#include <string.h>
int main()
{
  char s1[ ]= "HelloBlogsdope"; 
  char s2[ ] = "Blog";
  char *p;
  p = strstr(s1, s2);
  printf("%s\n", p);
  return 0;
}

Output

BlogsDope

strcspn

strcspn(s1, s2) returns the length of the initial part of the string s1 not containing any of the characters of the string s2.

#include <stdio.h>
#include <string.h>
int main()
{
  char s1[ ]= "HelloBlogsdope"; 
  char s2[ ] = "lo";
  int i = strcspn(s1, s2);
  printf("The first character is matched at %d\n", i+1);
  return 0;
}

Output

The first character is matched at 3

strspn

strspn(s1, s2) returns the length of the initial part of the string s1 only containing the characters of the string s2.

#include <stdio.h>
#include <string.h>
int main()
{
  char s1[ ]= "123abc"; 
  char s2[ ] = "123456790";
  int i = strspn(s1, s2);
  printf("%d\n", i);
  return 0;
}

Output

3

strpbrk

strpbrk(s1, s2) returns the first occurrence of any of the characters of the string s2 in the string s1.

#include <stdio.h>
#include <string.h>
int main()
{
  char s1[ ]= "Hey 123 Blogsdope"; 
  char s2[ ] = "123456790";
  char *p;
  p = strpbrk(s1, s2);
  printf("%s\n", p);
  return 0;
}

Output

123 Blogsdope

strtok

strtok(s1, s2) finds s2 in s1 and returns a pointer to it and returns NULL if not found.

#include <stdio.h>
#include <string.h>
int main()
{
  char s1[ ]= "Hey,123,Blogsdope";
  char *p;
  p = strtok(s1, ",");

  while(p != NULL)
  {
	printf("%s\n", p);
	p = strtok(NULL, ",");
  }
  return 0;
}

Output

Hey
123
Blogsdope

No hay comentarios:

Publicar un comentario