☰ Topics


× Home Introduction Structure of program Variables and keywords Constants Data Types Operators Statements Functions Storage Classes Array Structure Pointer Union Strings Header Files

String Handling in C

String:

A string is a collection of characters. Strings are always enclosed in double quotes as "String_constant".

String are used in string handling operations such as,

Declaration:

The string can be declared as follow:

Syntax:

  

char string_name[size];

Example:

  

char name[50];

String Structure:

When compiler assigns string to character array then it automatically supplies null character('\0') at the end of string. Thus, size of string = original length of string + 1.

  

char name[7]; name = "TECHNO"

'T'
'E'
'C'
'H'
'N'
'O'
'\0'
1
2
3
4
5
6
7

Read Strings:

To read a string, we can use scanf() function with format specifier %s

  

char name[50]; scanf("%s", name);

The above format allows to accept only string which does not have any blank space, tab, new line, form feed, carrage return.

Write Strings:

To write a string, we can use printf() function with format specifier %s.

  

char name[50]; scanf("%s", name); printf("%s", name);

String handling functions:

'string.h' is a header file which includes the declarations, functions, constants of string handling utilities. These string functions are widely used today by many programmers to deal with string operations.

Some of the standard member functions of string.h header files are,

  

#include< stdio.h > #include< conio.h > #include< string.h > void main() { char str[50]; clrscr(); printf("\n\t Lower case of string: %s", strlwr(str)); printf("\n\t Upper case of string: %s", strupr(str)); printf("\n\t Reverse of string: %s", strrev(str)); printf("\n\t Length of string: %s", strlen(str)); getch(); } Output: Enter your name: Technoexam Lower case of string: technoexam Upper case of string: TECHNOEXAM Reverse of string: MAXEONHCET Length of string: 10

Next Topic ➤