Strings

Working with strings in C. String manipulation functions from the `string.h` library.


Searching within Strings in C: strchr and strstr

C provides powerful functions for searching within strings. Two commonly used functions for this purpose are strchr and strstr. They allow you to find specific characters or substrings within a larger string.

strchr: Finding a Character

The strchr function searches for the first occurrence of a specific character within a string. Its prototype is:

char *strchr(const char *str, int c);
  • str: A pointer to the string to be searched.
  • c: The character you are looking for (passed as an int, but typically represents a char).

strchr returns a pointer to the location of the first occurrence of the character c in the string str. If the character is not found, it returns NULL.

Example:

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

int main() {
  const char *myString = "Hello, world!";
  char *location = strchr(myString, 'o');

  if (location != NULL) {
    printf("The character 'o' was found at position: %ld\n", location - myString);
    printf("The rest of the string starting from 'o' is: %s\n", location);
  } else {
    printf("The character 'o' was not found in the string.\n");
  }

  location = strchr(myString, 'z'); // Search for a non-existent character
  if (location != NULL) {
    printf("The character 'z' was found.\n");
  } else {
    printf("The character 'z' was not found in the string.\n");
  }

  return 0;
} 

Explanation of the example:

  • The code first searches for the character 'o' in the string "Hello, world!".
  • Since 'o' is found, the code prints its position (calculated by subtracting the starting address of the string from the address returned by strchr) and the rest of the string starting from that 'o'.
  • Then, it searches for 'z', which is not present. The strchr function returns NULL, and the code prints a message indicating that 'z' was not found.

strstr: Finding a Substring

The strstr function searches for the first occurrence of a substring within a string. Its prototype is:

char *strstr(const char *haystack, const char *needle);
  • haystack: A pointer to the string to be searched (the larger string).
  • needle: A pointer to the substring you are looking for.

strstr returns a pointer to the beginning of the first occurrence of the substring needle within the string haystack. If the substring is not found, it returns NULL.

Example:

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

int main() {
  const char *myString = "This is a simple example string.";
  const char *subString = "simple";
  char *location = strstr(myString, subString);

  if (location != NULL) {
    printf("The substring '%s' was found at position: %ld\n", subString, location - myString);
    printf("The rest of the string starting from '%s' is: %s\n", subString, location);
  } else {
    printf("The substring '%s' was not found in the string.\n", subString);
  }

  subString = "complex"; // Search for a non-existent substring
  location = strstr(myString, subString);
  if (location != NULL) {
    printf("The substring '%s' was found.\n", subString);
  } else {
    printf("The substring '%s' was not found in the string.\n", subString);
  }

  return 0;
} 

Explanation of the example:

  • The code first searches for the substring "simple" in the string "This is a simple example string.".
  • Since "simple" is found, the code prints its position and the rest of the string starting from that substring.
  • Then, it searches for "complex", which is not present. The strstr function returns NULL, and the code prints a message indicating that "complex" was not found.

Important Considerations

  • Both strchr and strstr are case-sensitive. If you need a case-insensitive search, you'll need to implement your own custom function (often involving converting both strings to lowercase or uppercase before searching).
  • These functions operate on null-terminated strings (C-style strings). They rely on the null terminator (\0) to know when to stop searching.
  • Be mindful of potential buffer overflows if you are manipulating the returned pointers. Always ensure you stay within the bounds of the original string.