String Functions in C

In this lesson, you will learn about the main string built-in function in C, their usage, with examples to better understand the topic.

String Input/Output functions in C

The commonly used functions in C are the read and write function. There are four read and write functions in C that beginners in C must know: scanf(), gets(), printf(), puts()

scanf() string function

Reads formatted data from stdin and stores it in the additional arguments’ locations.

int a;
scanf("Please enter a value %d", &a);

The code above will read the data from the stdin and store it in the integer variable a.

gets() string function

Reads a line of data from the stdin and store it in a variable. It will terminate its function once it reads a newline character.

char location[30];
printf ("\n Enter your location: \n");
gets (location);

printf() string function

Prints formatted output to stdout.

printf("Welcome to CodingPanel.com! );

puts() string function

Similar to print(), puts() will print the data to the output; however, any null character will be eliminated by the compiler. The puts() moves the cursor to the next line, meaning it will add a newline character to the output after printing out the data.

char location[30];
printf ("Please enter your location\n");
gets (location);
printf ("\nThe location is - ");
puts (location);

Example of Read & write Strings in C using Printf() and Scanf()

#include <stdio.h>
#include <string.h>
int main ()
{

  char location[30];

  printf ("Enter your location: ");

  scanf ("%s", location);

  printf ("Your location is %s", location);

  return 0;
}

Output

Enter your location: USA
Your location is USA

More string built-in functions in C

strlen string function in C

Returns the length of a string, without the ending null character

#include <stdio.h>
#include <string.h>
int main ()
{
  char strName[10] = "Fred";
  printf ("The length of the string is: %d ", strlen (strName));
  return 0;
}

Output

The length of the string is: 4

strcpy string function in C

Copies a string value from string variable A into string variable B, including the end character ‘0\’

#include <stdio.h>
#include <string.h>
int main ()
{

  char stringA[] = "string A";

  char stringB[] = "string B";

  strcpy (stringB, stringA);

  printf ("StringB new value is: %s", stringB);

  return 0;

}

output

StringB new value is: string A

strcat string function in C

Concatenates two strings into one string.

#include <stdio.h>
#include <string.h>
int main ()
{
  char string1[10] = "Coding";
  char string2[10] = "Panel";
  strcat (string1, string2);
  printf ("%s", string1);
  return 0;
}

Output

CodingPanel

strchr string function in C

Searches a string for a character ch.

#include <stdio.h>
#include <string.h>
int main ()
{

  char str[15] = "CodingPanel";

  char *output;

  int findMe = 'i';

  output = strchr (str, findMe);

  printf ("The first occurrence of %c in '%s' is '%s'\n", findMe, str,
      output);

}

Output

The first occurrence of i in ‘CodingPanel’ is ‘ingPanel’

Strrchr string function in C

This function is similar to strchr. The difference is that it searches the string for a char in a reserver order, starting the string’s last char.

#include <stdio.h>
#include <string.h>
int main ()
{

  char str[30] = "Welcome to CodingPanel.com";
  char *output;
  char findMe = 'o';
  output = strchr (str, findMe);
  printf ("The first occurrence of %c in '%s' is '%s'\n", findMe, str,
      output);
  return 0;
}

Output

The first occurrence of o in ‘Welcome to CodingPanel.com’ is ‘ome to CodingPanel.com’

strcmp String function in C

Compares two strings and returns an integer ‘0’ if they are equal. Otherwise, it will return a negative or positive value based on the comparison.

#include <stdio.h>
#include <string.h>
int main ()
{
  char string1[20] = "Coding";
  char string2[20] = "Panel";
  if (strcmp (string1, string2) == 0)
    {
      printf ("string1 and string2 are the same");
    }
  else
    {
      printf ("string 1 and string 2 are not the same");
    }
  return 0;
}

Output

string 1 and string 2 are not the same

strncat string function in C

Concatenates n characters of string2 to string1. The compiler will always add the char (‘\0’) at the end of the concatenated string.

#include <stdio.h>
#include <string.h>
int main ()
{
  char string1[] = "Coding";
  char string2[] = "Panel.commm";
  strncat (string1, string2, 9);
  printf ("%s", string1);
  return 0;
}

Output

CodingPanel.com

strncmp string function in C

Compare the first n characters for two strings. Return the integer ‘0’ if equal.

#include <stdio.h>
#include <string.h>
int main ()
{
  char string1[20] = "CodingPanel";
  char string2[20] = "CodingPanel.com";

  if (strncmp (string1, string2, 7)
    == 0)
    {
      printf ("The two strings are equal");
    }
  else
    {
      printf ("The two strings are not equal");
    }
  return 0;
}

Output

The two strings are equal

Summary

  • C built-in functions are to read, write, compare, and manipulate string variables.
  • The commonly used built-in string functions in C are: scanf(), printf(), gets(), strcat(), strrchr(), strcmp(), strcpy(), strlen(), strncat(), strncmp(), strncpy(), strrchr()
Back to: Learn C Programming > Strings in C

Leave a Reply

Your email address will not be published. Required fields are marked *