String in C Programming Language With Example

What is the string?

A sequence of characters is known as a string. Characters are arranged one after another in memory to form a sequence. It can be treated as a one-dimensional array of characters.

Example of string:

The string is enclosed within ” ” symbol known as double-quotes.

String-in-C

Syntax:

char name_of_string[dimension_of_array];

The following program describes how to initialize a string.

#include <stdio.h>
#include <stdlib.h>
int main()
{
char name[20];
printf("Enter any name:\n");
scanf("%s",name);
printf("Hello %s!",name);
return 0;
}
 

Output:

Enter any name:
Ansh Kushwah
Hello Ansh!

The second program to write a single line in a string.

//The second program to write a single line.
 
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch[100];
printf("Enter any string: ");
gets(ch);
printf("You have entered: %s",ch);
return 0;
}

Output:

Enter any string: Learn Simply helps you to learn C language.
You have entered: Learn Simply helps you to learn C language.

Introduction of string.h

As we know, to use some predefined functions, we have just added the appropriate header files. Therefore string.h is a standard library string function related to a string for us.

Way to use string.h is: #include <string.h>

Example of functions defined in string.h

  • strlen()
  • strcpy()
  • strcat()
  • strcmp()
  • strrev()
  • strset() etc.

Read and Write operations on strings

Reading and writing operations on strings can be done using input and output statements.

Reading Strings

Following input functions can be used to read a string

  • scanf()function
  • gets() function
  • getchar() function

Writing Strings

Following output, functions can be used to write a string

  • printf() function
  • puts() function
  • putchar() fumction

Operations that can be performed on a string:

  • To create a string object.
  • Access characters in a string.
  • To obtain the size of a string.
  • Read strings from the standard input device (i.e. keyboard).
  • To display strings on the standard output device(i.e. monitor).
  • Modify a string.
  • To find a substring from a string.
  • To compare two strings.

Standard library functions of string.h

A large set of string handling library functions are provided with every C compiler. The following are the standard library functions with their use are available in C language.

FunctionsUses
strlen ()To find the length of the string
strlwr ()Converts a string to lowercase
strupr ()Converts a string to uppercase
strcat ()Append one sting at the end of the other
strncat ()Appends first n characters of a string at the end of the another
strcpy ()Copies a string into another
strncpy ()Copies first n characters of a string at the end of the another
strcmp ()Compares two strings
strncmp ()Compare first n characters of two strings
strncmpi ()Compare two strings without regard to case (“I” denotes that this function ignores case)
stricmp ()Compare two strings without regard to case
strnicmp ()Compare the first n characters of two strings without regard
strdup ()Duplicate a string
strchr ()Finds the first occurrence of a given character in a string
strrchr ()Finds the last occurrence of a given character in a string
strstr ()Finds the first occurrence of a given character in another string
strset ()Sets all characters of a string to a given character
strnet ()Sets first n characters of a string to a given character
strrev ()Reverse string

The most useful string library functions in C programs are:

1. strrev ()

This function is used to convert any string into a reverse string sequence of characters.

Program:

#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch[ ]="Learn Simply";
printf("String in reverse: %s", strrev(ch));
return 0;
}
 

Output:

String in reverse: ylpmiS nraeL

2. strcpy

This function copies the contents of one string into another.

Program:

#include <stdio.h>
#include <stdlib.h>
int main()
{
char first[]="Learn Simply";
char second[]="";
printf("Content of first string: %s\n",first);
printf("Content of second string: %s\n",second);
strcpy(second,first);
printf("After copy content of second string: %s",second);
return 0;
}
 

Output:

Content of first string: Learn Simply
Content of second string:
After copy content of second string: Learn Simply

3. strcat

This function concatenates the source string at the end of the target string.

Program:

#include <stdio.h>
#include <stdlib.h>
int main()
{
char first[]=" Simply";
char second[]="Learn";
printf("Content of first string: %s\n",first);
printf("Before concatenate contents of second string: %s\n",second);
strcat(second,first);
printf("After concatenate contents are: %s",second);
return 0;
}
 

Output:

Content of first string:  Simply
Before concatenate contents of second string: Learn
After concatenate contents are: Learn Simply

4. strlwr

This function is used to convert string written in uppercase letters into lowercase.

Program:

#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch[]="C PROGRAMMING LANGUAGE";
printf("string in lowercase: %s", strlwr(ch));
return 0;
}
 

Output:

string in lowercase: c programming language

5. strupr

This function is used to convert string written in lowercase letters into uppercase.

Program:

#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch[]="c programming language";
printf("string in uppercase: %s", strupr(ch));
return 0;
}
 

Output:

string in uppercase: C PROGRAMMING LANGUAGE

6. strlen

This function counts the number of characters present in a string.

Program:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int i;
    i=strlen("Learn Simply");
    printf("length of string Learn Simply is: %d",i);
return 0;
}
 

Output:

Length of string Learn Simply is: 12

7. strcmp

This is a function that compares two string to find out whether they or the same or different.

The two string are compared character by character until there is a mismatch or end one of the string is reached, whichever occurs first.

If the two string is identical, strcmp() function returns a value zero.

if they are not identical, it returns the numeric difference between the ASCII values of the non-matching characters.

Program:

#include <stdio.h>
#include <stdlib.h>
int main() 
{
    char a[10]="hello";
    char b[10]="world";
    if(strcmp(a,b) == 0){
        printf("Equal");
    }
    else{
        printf("Not equal");
    }
    return 0;
}
 

Output:

Not equal

If both strings are equal strcmp function returns zero.


Previous Lecture:

Array in C
Loops in C
Operators in C
Data Types in C
Switch Case Statement

Conditional Statements if,if-else
Jumping statements goto, break, etc.

If you have any problem in solving your questions, then join our Telegram group or search @Learnsimply, we will help you.

Please write comments if you find anything incorrect in the String in C lecture.

Leave a Comment

en English
X