C

Strings And String Functions In C, With Examples

A string is a collection of characters. This guide teaches you how to declare strings, work with strings in C programming, and use pre-defined string handling functions.
We’ll look at how to compare two strings, concatenate strings, copy one string to another, and perform other string manipulation tasks. We can carry out such operations by utilizing the pre-defined functions in the “string.h” header file. To use these string functions, include the string.h file in your C program.

Declaration of Strings

Method 1:

char address[]={'T', 'E', 'X', 'A', 'S', '\0'};

Method 2: The preceding string can also be written as –

char address[]="TEXAS";

The NULL character (0) will be automatically inserted at the end of the string in the above declaration.
What exactly is NULL Char “0”?
‘0’ denotes the end of the string. It is also known as a string terminator and a null character.

Strings can be read and written in C using the Printf() and Scanf() functions.

#include <stdio.h>
#include <string.h>
int main()
{
    /* String Declaration*/
    char nickname[20];

    printf("Enter your Nick name:");

    /* I am reading the input string and storing it in nickname
     * Array name alone works as a base address of array so
     * we can use nickname instead of &nickname here
     */
    scanf("%s", nickname);

    /*Displaying String*/
    printf("%s",nickname);

    return 0;
}

Output:

Enter your Nick name:Negan
Negan

In C, use the gets() and puts() functions to read and write strings

#include <stdio.h>
#include <string.h>
int main()
{
    /* String Declaration*/
    char nickname[20];

    /* Console display using puts */
    puts("Enter your Nick name:");

    /*Input using gets*/
    gets(nickname);

    puts(nickname);

    return 0;
}

C String function – strlen

Syntax:

size_t strlen(const char *str)

size t denotes unsigned short.
It returns the length of the string excluding the terminating character (char ‘0’).

Example of strlen:

#include <stdio.h>
#include <string.h>
int main()
{
     char str1[20] = "BeginnersBook";
     printf("Length of string str1: %d", strlen(str1));
     return 0;
}

Output:

Length of string str1: 13

strlen vs sizeof strlen returns the length of the string stored in the array, whereas sizeof returns the array’s total allocated size. So, if I consider the preceding example again, the following statements would return the values shown below.

strlen(str1) the value 13 was returned
sizeof(str1) would return 20 because the array size is 20 (see the first statement in main function).

Strnlen is a string function in C

Syntax:

size_t strnlen(const char *str, size_t maxlen)

size t denotes unsigned short.
It returns the length of the string if it is less than the maxlen (maximum length) value, otherwise it returns the maxlen value.

Example of strnlen:

#include <stdio.h>
#include <string.h>
int main()
{
     char str1[20] = "BeginnersBook";
     printf("Length of string str1 when maxlen is 30: %d", strnlen(str1, 30));
     printf("Length of string str1 when maxlen is 10: %d", strnlen(str1, 10));
     return 0;
}

Output:
String str1 length when maxlen is 30: 13
String str1 length when maxlen is 10: 10

Have you noticed the output of the second printf statement? Despite the fact that the string length was 13, it only returned 10 because the maxlen was 10.

Strcmp is a C string function

int strcmp(const char *str1, const char *str2)

It performs a string comparison and returns an integer value. If both strings are the same (equal), this function will return 0. Otherwise, depending on the comparison, it may return a negative or positive value.

If string1 = string2 OR string1 is a substring of string2, the result is negative. If string1 is greater than string2, it will return a positive value.
When you use this function to compare strings, you will get 0 (zero) if string1 == string2.

Example of strcmp:

#include <stdio.h>
#include <string.h>
int main()
{
     char s1[20] = "BeginnersBook";
     char s2[20] = "BeginnersBook.COM";
     if (strcmp(s1, s2) ==0)
     {
        printf("string 1 and string 2 are equal");
     }else
      {
         printf("string 1 and 2 are different");
      }
     return 0;
}

Output:

string 1 and 2 are different

C String function – strncmp

int strncmp(const char *str1, const char *str2, size_t n)

size t refers to unassigned shorts.
It compares both strings up to n characters, or in other words, the first n characters of both strings.

Example of strncmp:

#include <stdio.h>
#include <string.h>
int main()
{
     char s1[20] = "BeginnersBook";
     char s2[20] = "BeginnersBook.COM";
     /* below it is comparing first 8 characters of s1 and s2*/
     if (strncmp(s1, s2, 8) ==0)
     {
         printf("string 1 and string 2 are equal");
     }else
     {
         printf("string 1 and 2 are different");
     }
     return 0;
}

Output:

string1 and string 2 are equal

C String function – strcat

char *strcat(char *str1, char *str2)

It joins two strings and returns the resulting string.

Example of strcat:

#include <stdio.h>
#include <string.h>
int main()
{
     char s1[10] = "Hello";
     char s2[10] = "World";
     strcat(s1,s2);
     printf("Output string after concatenation: %s", s1);
     return 0;
}

Output:

Output string after concatenation: HelloWorld

Strncat is a C string function

char *strncat(char *str1, char *str2, int n)

It appends n characters from str2 to string str1. At the end of the concatenated string, a terminator char (‘0’) is always appended.

Example of strncat:

#include <stdio.h>
#include <string.h>
int main()
{
     char s1[10] = "Hello";
     char s2[10] = "World";
     strncat(s1,s2, 3);
     printf("Concatenation using strncat: %s", s1);
     return 0;
}

Output:

Concatenation using strncat: HelloWor

C String function – strcpy

char *strcpy( char *str1, char *str2)

It copies str2 into str1, including the last character (terminator char ‘0’).

Example of strcpy:

#include <stdio.h>
#include <string.h>
int main()
{
     char s1[30] = "string 1";
     char s2[30] = "string 2 : I’m gonna copied into s1";
     /* this function has copied s2 into s1*/
     strcpy(s1,s2);
     printf("String s1 is: %s", s1);
     return 0;
}

Output:

String s1 is: string 2: Im gonna copied into s1

C String function – strncpy

size t is an unassigned short and n is a number in char *strncpy(char *str1, char *str2, size t n).
Case 1: If the length of str2 is greater than n, it simply copies the first n characters of str2 into str1.
Case 2: If the length of str2 is greater than n, it copies all of the characters from str2 into str1 and appends several terminator chars (‘0’) to increase the length of str1 to n.

Example of strncpy:

#include <stdio.h>
#include <string.h>
int main()
{
     char first[30] = "string 1";
     char second[30] = "string 2: I’m using strncpy now";
     /* this function has copied first 10 chars of s2 into s1*/
     strncpy(s1,s2, 12);
     printf("String s1 is: %s", s1);
     return 0;
}

Output:

String s1 is: string 2: Im

C String function – strchr

char *strchr(char *str, int ch)

It searches string str for character ch (you may be wondering why I gave the data type of ch as int in the above definition; don’t worry, I didn’t make a mistake; it should only be int). The problem is that when we use strchr, any character is internally converted into an integer for better searching.

Example of strchr:

#include <stdio.h>
#include <string.h>
int main()
{
     char mystr[30] = "I’m an example of function strchr";
     printf ("%s", strchr(mystr, 'f'));
     return 0;
}

Output:

f function strchr

C String function – Strrchr

char *strrchr(char *str, int ch)

It is similar to the function strchr, with the exception that it searches the string in reverse order. You may have guessed why there is an extra r in strrchr, and you are correct.
Consider the following example:

#include <stdio.h>
#include <string.h>
int main()
{
     char mystr[30] = "I’m an example of function strchr";
     printf ("%s", strrchr(mystr, 'f'));
     return 0;
}

Output:

function strchr

Why is output different from strchr? It is because it began searching from the end of the string and found the first ‘f’ in function rather than the first ‘of’.

Strstr is a string function in C.

char *strstr(char *str, char *srch_term)

It is similar to strchr, except that it looks for the string srch term rather than a single character.

Example of strstr:

#include <stdio.h>
#include <string.h>
int main()
{
     char inputstr[70] = "String Function in C at BeginnersBook.COM";
     printf ("Output string is: %s", strstr(inputstr, 'Begi'));
     return 0;
}

Output:

Output string is: BeginnersBook.COM

You can also use this function instead of strchr because you can pass a single character in place of the search term string.

Related Articles

Leave a Reply

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

Back to top button