Advertisement
Sierra_ONE

StringFunctions

Feb 15th, 2024
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.08 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main() {
  5.     // strcpy: Copies a string
  6.     char source[] = "Hello";
  7.     char destination[20];
  8.     strcpy(destination, source);
  9.     printf("strcpy: Copied string: %s\n", destination);
  10.     printf("parameters: (destination, source);\n");
  11.  
  12.     // strncpy: Copies a certain number of characters from one string to another
  13.     char source2[] = "Hello, World!";
  14.     char destination2[20];
  15.     strncpy(destination2, source2, 5);
  16.     destination2[5] = '\0'; // Null-terminate the string
  17.     printf("\nstrncpy: Copied string: %s\n", destination2);
  18.     printf("parameters: (destination, source, num);\n");
  19.  
  20.     // strcat: Concatenates two strings
  21.     char str1[20] = "Hello ";
  22.     char str2[] = "World!";
  23.     strcat(str1, str2);
  24.     printf("\nstrcat: Concatenated string: %s\n", str1);
  25.     printf("parameters: (destination, source);\n");
  26.  
  27.     // strncat: Concatenates a certain number of characters from one string to another
  28.     char str3[20] = "Hello ";
  29.     char str4[] = "World!";
  30.     strncat(str3, str4, 3);
  31.     printf("\nstrncat: Concatenated string: %s\n", str3);
  32.     printf("parameters: (destination, source, num);\n");
  33.  
  34.     // strcmp: Compares two strings
  35.     char str5[] = "hello";
  36.     char str6[] = "Hello";
  37.     int result = strcmp(str5, str6);
  38.     printf("\nstrcmp: Comparison result: %d\n", result);
  39.     printf("parameters: (string1, string2);\n");
  40.  
  41.     // strncmp: Compares a certain number of characters of two strings
  42.     char str7[] = "hello";
  43.     char str8[] = "Hello";
  44.     int result2 = strncmp(str7, str8, 3);
  45.     printf("\nstrncmp: Comparison result: %d\n", result2);
  46.     printf("parameters: (string1, string2, num);\n");
  47.  
  48.     // strlen: Calculates the length of a string
  49.     char str9[] = "Hello";
  50.     printf("\nstrlen: Length of string '%s' is %zu\n", str9, strlen(str9));
  51.     printf("parameters: (string);\n");
  52.  
  53.     // strchr: Finds the first occurrence of a character in a string
  54.     char str10[] = "Hello";
  55.     char *ptr = strchr(str10, 'l');
  56.     printf("\nstrchr: First occurrence of 'l' in '%s' at position: %ld\n", str10, ptr - str10);
  57.     printf("parameters: (string, character);\n");
  58.  
  59.     // strrchr: Finds the last occurrence of a character in a string
  60.     char str11[] = "Hello";
  61.     ptr = strrchr(str11, 'l');
  62.     printf("\nstrrchr: Last occurrence of 'l' in '%s' at position: %ld\n", str11, ptr - str11);
  63.     printf("parameters: (string, character);\n");
  64.  
  65.     // strstr: Finds the first occurrence of a substring in a string
  66.     char str12[] = "Hello, World!";
  67.     char *substr = strstr(str12, "World");
  68.     printf("\nstrstr: Substring 'World' found in '%s' at position: %ld\n", str12, substr - str12);
  69.     printf("parameters: (string, substring);\n");
  70.  
  71.     // strtok: Splits a string into tokens
  72.     char str13[] = "Hello,World,Bye,I'm,Back,Okay,Bye";
  73.     char *token = strtok(str13, ",");
  74.     while (token != NULL) {
  75.         printf("\nstrtok: Token: %s\n", token);
  76.         printf("parameters: (string, delimiter);\n");
  77.         token = strtok(NULL, ",");
  78.     }
  79.  
  80.     // memset: Fills a block of memory with a particular value
  81.     char str14[20];
  82.     memset(str14, 'A', 5);
  83.     printf("\nmemset: After memset: %s\n", str14);
  84.     printf("parameters: (block, value, size);\n");
  85.  
  86.     // memcpy: Copies a block of memory
  87.     char source15[] = "Hello";
  88.     char destination15[20];
  89.     memcpy(destination15, source15, strlen(source15) + 1);
  90.     printf("\nmemcpy: Copied string: %s\n", destination15);
  91.     printf("parameters: (destination, source, size);\n");
  92.  
  93.     // memmove: Moves a block of memory to another location
  94.     char str16[] = "oldstring";
  95.     memmove(str16 + 3, str16, strlen(str16) + 1);
  96.     printf("\nmemmove: After moving: %s\n", str16);
  97.     printf("parameters: (destination, source, size);\n");
  98.  
  99.     // memcmp: Compares two blocks of memory
  100.     char str17[] = "abc";
  101.     char str18[] = "abd";
  102.     int result3 = memcmp(str17, str18, strlen(str17));
  103.     printf("\nmemcmp: Comparison result: %d\n", result3);
  104.     printf("parameters: (block1, block2, size);\n");
  105.  
  106.     return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement