What is meant by string?

A string is a one-dimensional array of characters that can be terminated using a null character (‘’). The string or character array is used to manipulate the text such as words or sentences. The (termination character) is the most important in the string as it is the only method to identify the end of the string. A string can be defined as a one-dimensional array of characters; whereas, an array of strings is a two-dimensional array of characters.

Example: char s[ ] = “C-style” ;

Declaration of string

String in C is a simple array with char datatype. The size of an array must be defined while declaring a C string variable because it is used to calculate how many characters are going to be stored inside the string variable.

Syntax: char stringvariable_name [arraysize];

In C, a string can be declared and initialized using two different methods: using character array and using a string literal.

Different methods of string initialization

  • Character array
    • Without array size: char str[ ] = “C string”;
    • With array size: char str[10] = “C string”;
  • String literal
    • Without array size: char s1[ ] = {‘C’,’ ‘,’s’,’t’,’r’,’i’,’n’,’g’,’’};
    • With array size: char s1[10] = {‘C’,’ ‘,’s’,’t’,’r’,’i’,’n’,’g’,’’};

Reading a string input from user

The function scanf() is used to read the strings. scanf() can read a sequence of characters till it encounters a newline, space, or tab. The function fgets() is used to read lines of strings and the put() function is used to display the strings.

Example code

#include <stdio.h>

int main(){

    char name[10]; //declaration

    printf("Enter name:");

    scanf("%s",name); //read a string from user

    printf("Your name:%s",name);

    return 0;}

Output

Enter name: Jason king

Your name: Jason

Explanation: Even though Jason king was the input from the user, only Jason is stored in the variable name, as there is a space after Jason. In scanf() “name” is used instead of using “&name” because the data type of the variable name is char and it is a char array. In scanf(), the variable name points to the address of the first element.

C String functions

C string functions are built-in library functions and they are declared in the header file <string.h>. Some of the important string functions are listed below,

strcmp()

To compare two strings strcmp() function is used and the strings are compared using the case-sensitive method. When the function returns 0, then it indicates that both the strings are equal. This function returns three different types of integer values based on the comparison.

Syntax: strcmp(str_1, str_2)

strcasecmp()

The function strcasecmp() performs a character-by-character comparison of the string and ignores the case differences. Both the lower and upper cases are mapped in the same character set. It returns 0 when the string matches. This function returns an integer less than, greater than, or equal to zero if string 1 is found.

Syntax: strcasecmp(str_1, str_2)

strlen()

The function strlen() finds the string length without including the null or terminating character. This function returns the total count of characters present in the given string.

Syntax: strlen(str_name)

strcpy()

The function strcpy() is used to copy the data of the source to the destination string. This function returns a pointer to the copied string(destination string). In the below syntax, destination_string is where the data will be copied, source_string is the string to be copied.

Syntax: strcpy(destination_string, source_string)

strncpy()

The strncpy() function is to copy a specified number of characters from the source to the destination string. This function returns a pointer to the destination_string In the below syntax, n is the number of characters.

Syntax: strncpy(destination_string, source_string,n)

strcat()

The function strcat() is used to concatenate or join two strings as a single string. The concatenated string will be stored in the first string. This function returns a pointer to the concatenated string.

Syntax:  strcat(str_1, str_2)

strncat()

The strncat() function is used to append the specified number of characters from one string to the termination of another string. This function returns a pointer to the joined string. In the below syntax, str_1 and str_2 represent the strings and n refers to the count of characters.

Syntax:  strncat(str_1, str_2,n)

strstr()

The function strstr() is used to search the specified character within the given string. This function returns a pointer to the first occurrence of the string in a given string. In the below syntax, str_1 is the string to be scanned and ch is the search character.

Syntax: char *strstr(const char *str_1, const char ch)

strnstr()

The strnstr() function will traverse for a single string within the first n characters of the second string. When the function finds the string then it will return the pointer to the string’s location. In the below syntax, str_1 and str_2 refer to two strings, and n is the count of character.

Syntax: char *strstr(const char *str_1, const char *str_2, n)

strrev()

The function strrev() is used to reverse the given string. This function returns the reverse of the given string. In the below syntax, str_1 contains the string which should be reversed.

Syntax: char *strrev(char str_1)

strchr()

The function strchr() is used to search the character with the given string. This function will return the pointer to the first occurrence of the character in the given string. In the below syntax str_1 is the string and ch is the search character.

Syntax: char *strrchr(const char str_1, int ch)

The major difference between function strstr() and strchr() is strchr() function searches the target string from the character; whereas, strstr() function searches the target string from the substring.

strrchr()

The function strchr() is used to search the character with the given string in the reverse method. This function will return the pointer to the last occurrence of the character in the given string. The below syntax is the same as strchr() function except for the function name.

Syntax: char *strrchr(const char str_1, int ch)

strlwr()

The function strlwr() is used to return the string in lowercase. In the below syntax, str_name contains the string.

Syntax: strlwr(str_name)

strupr()

The function strupr() is used to return the string in uppercase. In the below syntax str_name contains the string.

Syntax: strupr(str_name)

C string function example program

The below code has some of the string functions

Code:

#include <stdio.h>

#include <string.h>

int main (){

printf("C STRING FUNCTION");
char str_1[20] = "C string"; //initialization
char str_2[20] = "function";
char str_3[20];
const char str_4[20] = "Substring";
const char str_5[20] = "string";
int len =strlen(str_1);
char *ret;
ret = strstr(str_4,str_5);
printf("nstr_1:%s",str_1);
printf("nstr_2:%s",str_2);
printf("nstring copy: %s",strcpy(str_3, str_1)); // string copy
printf("nstring concatenation: %s", strcat(str_1, str_2)); //string concatenation
printf("nlength of str_1: %d", len); //string length
printf("nstr_4:%s",str_4);
printf("nstr_5:%s",str_5);
printf("nsubstring: %s",ret); //sub string

}

Output:

C STRING FUNCTIONS

str_1: C string

str_2: function

string copy: c string

string concatenation: C stringfunction

length of str_1: 8

str_4: substring

str_5: string

substring: string

Explanation: In the above code, the char data type is used to store the strings in the variables str_1 and str_2. The function strcpy() is used to copy the string 1 to string 2, strlen() for finding the length of the string, and strcat() for string concatenation. The function strstr() is used to find the substring.

Context and Applications

This topic is important for postgraduate and undergraduate courses, particularly for,

  • Bachelors in computer science engineering.
  • Masters in computer science.

Practice Problems

Question 1: Which string function is used to compare strings?

  1. strlen
  2. strcmp
  3. strnlen
  4. strxfrm

Answer: Option b is correct.

Explanation: Two strings can be compared using strcmp() function. This function uses a case-sensitive method in comparing the string. When both the string are equal the return value of the function is 0.

Question 2: In a string function, null character is referred by ___.

  1. n
  2. a
  3. None of the above

Answer: Option c is correct.

Explanation: A string is a one-dimensional array of characters that can be terminated using a null character (‘’). The (termination character) is the most important in the string as it is the only method to identify the end of the string.

Question 3: To find the length of the string, the ___  function is used.

  1. strlen
  2. sizeof
  3. strcoll
  4. None of the above

Answer: Option a is correct.

Explanation: The string function in C is a built-in library function and to find the length of the string, strlen() function is used.

Question 4: String can be initialized using ___ and ____.

  1. Character array
  2. String literals
  3. List
  4. Dictionary
  1. 1 & 2
  2. 2 & 4
  3. 1 & 3
  4. 2 & 3

Answer: Option a is correct.

Explanation: A string is initialized in two different methods and they are character array and string literal. The major difference is the character array can be reassigned but string literals cannot be reassigned.

Question 5: What is the purpose of strcat() function?

  1. compare strings
  2. concatenate strings
  3. string reverse
  4. None of the above

Answer: Option b is correct.

Explanation: The string function in C is a built-in library function and the function strcat() is used to concatenate two strings and the result of the string is stored in the first string.

Want more help with your computer science homework?

We've got you covered with step-by-step solutions to millions of textbook problems, subject matter experts on standby 24/7 when you're stumped, and more.
Check out a sample computer science Q&A solution here!

*Response times may vary by subject and question complexity. Median response time is 34 minutes for paid subscribers and may be longer for promotional offers.

Search. Solve. Succeed!

Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.

Tagged in
EngineeringComputer Science

Programming

Character

C-string

Search. Solve. Succeed!

Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.

Tagged in
EngineeringComputer Science

Programming

Character

C-string