C Programming Language Tutorial

Variables and Data Types

Input/Output

Looping and Selection Structures

Array

Functions

Preprocessing Command

Pointer

Structure

File Operations

Important Knowledge

String Processing Functions in C Programming Language

In this tutorial, we'll explore some of the string processing functions in the C programming language, focusing on functions available in the string.h header. We'll cover the basic syntax and provide examples to demonstrate their usage.

Including the string.h Header

Before using the string processing functions, you should include the string.h header at the beginning of your C program:

#include <string.h>

Common String Processing Functions

Here are some of the most commonly used string processing functions available in the string.h header:

  • strlen(): Returns the length of a string.
size_t strlen(const char *str);
  • strcpy(): Copies a string from a source to a destination.
char *strcpy(char *dest, const char *src);
  • strncpy(): Copies a specified number of characters from a source string to a destination.
char *strncpy(char *dest, const char *src, size_t n);
  • strcat(): Appends a source string to a destination string.
char *strcat(char *dest, const char *src);
  • strncat(): Appends a specified number of characters from a source string to a destination string.
char *strncat(char *dest, const char *src, size_t n);
  • strcmp(): Compares two strings lexicographically and returns an integer indicating their relationship.
int strcmp(const char *str1, const char *str2);
  • strncmp(): Compares a specified number of characters of two strings lexicographically and returns an integer indicating their relationship.
int strncmp(const char *str1, const char *str2, size_t n);

Examples

Here are some examples demonstrating the use of these string processing functions in C:

  • Calculate the length of a string using strlen():
#include <stdio.h>
#include <string.h>

int main() {
    char str[] = "Hello, World!";
    size_t length = strlen(str);
    printf("Length of the string: %zu\n", length);
    return 0;
}
  • Copy a string using strcpy():
#include <stdio.h>
#include <string.h>

int main() {
    char src[] = "Hello, World!";
    char dest[50];
    strcpy(dest, src);
    printf("Destination string: %s\n", dest);
    return 0;
}
  • Append a string using strcat():
#include <stdio.h>
#include <string.h>

int main() {
    char dest[50] = "Hello, ";
    char src[] = "World!";
    strcat(dest, src);
    printf("Resulting string: %s\n", dest);
    return 0;
}
  • Compare two strings using strcmp():
#include <stdio.h>
#include <string.h>

int main() {
    char str1[] = "apple";
    char str2[] = "banana";

    int result = strcmp(str1, str2);

    if (result < 0) {
        printf("%s comes before %s\n", str1, str2);
    } else if (result > 0) {
        printf("%s comes after %s\n", str1, str2);
    } else {
        printf("%s and %s are equal\n", str1, str2);
    }

    return 0;
}
  1. Standard string functions in the C library:

    • Description: The C library provides standard functions for string manipulation, including strlen, strcpy, strcat, and strcmp.
    • Example:
      #include <stdio.h>
      #include <string.h>
      
      int main() {
          char str1[] = "Hello";
          char str2[] = "World";
      
          // Using standard string functions
          printf("Length: %zu\n", strlen(str1));
          strcpy(str1, str2);
          strcat(str1, "!");
          printf("Concatenated: %s\n", str1);
      
          return 0;
      }
      
  2. Manipulating and modifying strings in C language:

    • Description: Manipulating strings involves operations like copying, concatenating, and modifying characters.
    • Example:
      #include <stdio.h>
      #include <string.h>
      
      int main() {
          char source[] = "Hello";
          char destination[20];
      
          // Copying strings
          strcpy(destination, source);
          printf("Copied: %s\n", destination);
      
          // Modifying strings
          strcat(destination, " World!");
          printf("Modified: %s\n", destination);
      
          return 0;
      }
      
  3. Commonly used string handling functions in C:

    • Description: Common functions include strlen, strcpy, strcmp, strcat, strchr, and strstr.
    • Example:
      #include <stdio.h>
      #include <string.h>
      
      int main() {
          char str[] = "Hello, World";
      
          // Using commonly used string functions
          printf("Length: %zu\n", strlen(str));
          printf("First occurrence of 'o': %s\n", strchr(str, 'o'));
          printf("Substring 'World': %s\n", strstr(str, "World"));
      
          return 0;
      }
      
  4. String input/output functions in C programming:

    • Description: Use fgets for string input and puts or printf for output.
    • Example:
      #include <stdio.h>
      
      int main() {
          char buffer[50];
      
          // String input
          printf("Enter a string: ");
          fgets(buffer, sizeof(buffer), stdin);
      
          // String output
          printf("You entered: %s\n", buffer);
      
          return 0;
      }
      
  5. Searching and comparing strings using C functions:

    • Description: Use strcmp for string comparison and strchr for searching characters.
    • Example:
      #include <stdio.h>
      #include <string.h>
      
      int main() {
          char str1[] = "Hello";
          char str2[] = "World";
      
          // Comparing strings
          if (strcmp(str1, str2) == 0) {
              printf("Strings are equal.\n");
          } else {
              printf("Strings are not equal.\n");
          }
      
          // Searching for a character
          if (strchr(str1, 'o') != NULL) {
              printf("'o' found in the string.\n");
          } else {
              printf("'o' not found in the string.\n");
          }
      
          return 0;
      }
      
  6. Concatenating and splitting strings in C language:

    • Description: Use strcat for concatenation and various methods for splitting, such as strtok.
    • Example:
      #include <stdio.h>
      #include <string.h>
      
      int main() {
          char str1[20] = "Hello";
          char str2[] = "World";
      
          // Concatenating strings
          strcat(str1, " ");
          strcat(str1, str2);
          printf("Concatenated: %s\n", str1);
      
          // Splitting strings
          char *token = strtok(str1, " ");
          while (token != NULL) {
              printf("Token: %s\n", token);
              token = strtok(NULL, " ");
          }
      
          return 0;
      }
      
  7. String manipulation with pointers in C programming:

    • Description: Pointers can be used for efficient string manipulation, including traversing characters and modifying elements.
    • Example:
      #include <stdio.h>
      
      int main() {
          char str[] = "Hello";
          char *ptr = str;
      
          // Using pointers for string manipulation
          while (*ptr != '\0') {
              printf("%c ", *ptr);
              ++ptr;
          }
      
          return 0;
      }
      
  8. C code examples demonstrating string processing functions:

    • Example:
      #include <stdio.h>
      #include <string.h>
      
      int main() {
          char str[] = "Programming in C";
      
          // Using string functions for processing
          printf("Length: %zu\n", strlen(str));
          printf("Substring 'in': %s\n", strstr(str, "in"));
      
          return 0;
      }