loading...

String functions in C

mikkel250 profile image mikkel250 Updated on ・6 min read

String functions in the C programming language

Since a character string is actually a char array terminated with a null character \0, strings are not a variable/data type, so you can't use the same operators on them that can be used with other data types. In the standard and string libraries, however, there are many functions that are designed to work with strings.
Some of the most commonly used operators are listed below.

  • strlen() gets the length of the string.
    • returned as size_t

For example:

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

int main() {
  char myString[] = "my string";

  printf("The length of my string is %d", strlen(myString));

  return 0;
}

Copying strings: strncpy()

While there is an alternative strcpy() function, the easier and safer method to use is strncpy() -- note the "n".
The syntax is:

strncpy(destination, source, maximumNumberOfCharactersToCopy);

The size of the last argument should correspond to the size of the destination array, minus 1 to account for the null terminator at the end to prevent buffer overflows.

Concatenation: strncat()

The strncat() function takes two strings as arguments and the second argument is tacked on to the end of the first. The combined version becomes the new first string, and the original second string is not altered. It returns the value of its first argument -- the address of the first character of the string to which the second string is appended. The third argument is how many character to copy.

The syntax is:

strncat(destination, source, numberOfCharacterToCopy);

Like the copy function above, there is also a strcat() function, but will result in a buffer overflow if the second string does not fit into the first array's size.

Comparing strings: strncmp()

Compares the contents of the two strings, and returns the following values:

stncmp(string1, string2, numberOfCharactersToCompare);

/*
results in:
0 if the strings are equal
< 0 if string1 is less than string2
> 0 if string1 is greater than string2
*/

char firstString[] = "astronomy";
char secondString[] = "astro";
char thirdString[] = "astounding";

// a common use case for the compare function:
if (stncmp(firstString, secondString, 5) == 0) {
  printf("Found: "astromony");
}

if (strncmp(thirdString, secondString, 5) == 0) {
  printf("Found: "astounding");
}

Note that lowercase characters are greater than uppercase characters because of the way that the different letters are handled by the UTF standard, which can be researched, but suffice it to say that that a lowercase letter will always result in returning a lower value than any capital letter ('a' will be less than 'Z', despite their relative positions in the alphabet). If you want a strict comparison, the strings must both be the same case.

String parsing and manipulation

One thing to note is that many of the following functions return pointers. If you are not familiar with pointers, then I'd recommend reading the installment in the series on pointers.

Searching a string: strchr() and strstr()

Including the <string.h> header file will give you access to some string searching functions. These searches are case sensitive.
strchr() searches a given string for a specified character. The first argument is the string to be searched (which will be the address of a char array), and the second argument is the character you are looking for.
The function will search the string starting at the beginning and return a pointer to the first position in the string where the character is found. This return value is not the character itself, but a pointer to the position in memory where the character is stored. The address is essentially the index in the array, and is a special type char*, described as the "pointer to char."
To store the value that's returned, create a variable that can store the address of a character.
If the character is not found, the function returns NULL, meaning the pointer does not point to anything.

char str[] = "A quality example";   // string to be searched
char ch = 'q';                     // the character to search for
char * pFoundChar = NULL;         //  pointer to hold the address of the character, if found, initialized to NULL
pFoundChar = strchr(str, ch);    // stores address where ch is found

In the example above pFoundChar will point to "quality example", because it will start at 'q' but will go until it finds a null terminator (end of string).
To display the character, use printf("%s", pFoundChar);

Searching for a substring with strstr() is much the same as searching for a single character in a string, and will return the address plus the rest of the string until it encounters a null terminator:

char str[] = "A quality example";
char word = 'quality';
char * pFoundWord = NULL;
pFoundWord = strstr(str, word);
Tokenizing a string

A token is a sequence of characters in a string that are bounded by a delimiter (commonly a space, comma, period, etc.), and breaking sentences into words is called tokenizing. Use the strtok() function to split a string into separate words.
strtok() takes two arguments: the string to be tokenized, and a string containing all possible delimiter characters.
This operator is great for parsing (breaking a long string into shorter bits). One handy way to to do this is with a while loop:

int main() {
  char str[80] = "Hello, my name is Mikkel. It's nice to meet you. I'm learning C, how about you?";
  const char s[2] = ".";
  char *token;

  // get the first token
  token = strtok(str, s);

  // walk through the other tokens
  while (token != NULL) {
    printf("%s\n", token);

    token = strtok(NULL, s);
  }

  return 0;

}

Analyzing strings: booleans

The following is a list of other string functions that return a boolean (true/false) value, and do what you would expect based on the the descriptions.
The arguments to each of these functions is the letter to be tested.
A common use for these is to use a loop to test a string for the value in question.

Function Tests for
Islower() lowercase letter
isupper() UpperCase Letter
isalpha() any Letter
isalnum() UpperCase or lowercase Letter or a digit
iscntrl() control character
isprint() any printing character including a space
isgraph() any printing character except a space
isdigit() decimal digit (0-9)
isxdigit() hexadecimal digit (0-9, A-F, a-f)
isblank() standard blank characters (space, tab ['\t'])
isspace() any whitespace character (space, '\n', '\t', '\v', '\r')
ispunct() printing character for which isspace() and isalnum() return false
toupper() converts the character to uppercase (use a loop to convert string)
tolower() converts the character to lowercase (use a loop to convert string)

You can use the toupper() or tolower() along with the strstr() function to search an entire string and ignore case.
The example below illustrates how one could use some of the methods above to test a user's password for strength.

int main() {
  char buf[100];    // input buffer
  int nLetters = 0; // number of letters in input
  int nDigits = 0;  // number of digits in input
  int nPunct = 0;   // number of punctuation characters

  printf("Enter password of more than 8 and less than 100 characters, with at least one digit, and one punctuation mark: ");
  scanf("%s", buf);

  int i = 0;
  while(buf[i])
  {
    if (isalpha(buf[i]))
    {
      ++nLetters;
    }
    else if (isdigit(buf[i]))
    {
      ++nDigits;
    }
    else if (ispunct(buf[i]))
    {
      ++nPunct;
    }
    ++i;
  }

  if (nLetters < 8 || nLetters > 100 || nDigits < 1 || nPunct < 1)
  {
    printf("Sorry, password too weak! Try again.\n");
  }
  else
  }
  printf("Password saved!");
  }

  return 0;
}

Converting strings to numbers

The stdlib.h header file declares functions that you can use to convert a string to a numerical value.
Note that all functions below, leading whitespace is ignored.

Function Returns
atof() A value of type double that is produced from the string argument. Infinity as a double value is recognized from the string's INF or INFINITY where any character can be uppercase or lowercase and not a number is recognized from the string NAN in uppercase or lowercase.
atoi() A value of type int that is produced from the string argument
atol() A value of type long that is produced from the string argument
atoll() A value of type long long that is produced from the string argument

These functions are used to convert numbers that are found in strings to actual numbers, e.g.

char tempString[] = "98.6";
float tempFloat = atof(tempString);
// tempFloat will have the float value of 98.6

That covers the basics of string functions!

Posted on by:

mikkel250 profile

mikkel250

@mikkel250

I've been tinkering with computers since I was a teen. I'm currently pivoting from my current role as Tech Support manager to Full Stack Web Developer. I'm actively seeking employment in the field.

Discussion

pic
Editor guide