You are on page 1of 8

Character Array - String

String is sequence of characters that is treated as a single data item and terminated
by null character ‘\0’.Remember that C language does not support string as a data
type. A string is actually one dimensional array of characters in C language.
Declaring and Initializing string variables
The general form of declaration of a string variable
char stringname[size];
The size determines the number of characters in the stringname.
Example
char name[20];
char city[15];
Character array may be initialized when they are declared. There are different ways
to initialize a character array variable.
char name[ ]=“Jisha John”;
char name[10]={‘L’,’e’,’s’,’s’,’o’,’n’,’s’,’\0’};
Read string from terminal
1. scanf() function to read a string
The scanf() function reads a sequence of characters until it encounters a
white space.
Example
char name[20];
scanf(“%s”,name);
printf(“ Name : %s”,name);
2. gets() function to read a line of string and puts() function to display string.
Example
char name[20];
gets(name);
printf(“ Name : );
puts(name);
String handling functions
1. strlen()
strlen() function is used to find length of string. Length of string
means number of characters in the string.
Syntax
length = strlen(string);
Example
int len;
char array[20] = “MG University”;
len = strlen(array);

In this example, len = 13


2. strcpy()
strcpy() function is used to copy one string into another string. strcpy()
function accepts two strings (source and destination) as arguments and
copy source string into destination string. It does not return any value.
Syntax
strcpy(Destinationstring, Sourcestring);
Example
char source[] = “ C Programming”;
char target[20];
strcpy(target,source);

In this example, target is C Programming


3. strcat()
strcat() function is used to append (join) one string at the end of another
string.
strcat() function accepts two strings (string1, string2) as arguments and
append string2 at the end of string1 and result is stored in string1. It does not
return any value.
Syntax
strcat(string1,string2);
Example
char source[]= “ Kottayam”;
char target[] = “MG University”
strcat(target, source);

In this example, source is Kottayam


target is MG University Kottayam
strcmp()
The strcmp() compares two strings character by character. If the first character of two
strings are equal, next character of two strings are compared. This continues until the
corresponding characters of two strings are different or a null character ‘\0’ is reached.
Syntax
int strcmp(str1,str2);
str1 - first string
str2 - second string
Return value from strcmp()
0 - if both strings are equal (identical)
Negative - if the ASCII value of first unmatched character is less than
second
Positive integer - if the ASCII value of first unmatched character is greater
than second
If you want to use string functions, the header file string.h should be included.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char str1[15], str2[15];
int m;
clrscr();
printf("Enter first string ");
scanf("%s”,str1);
printf(“\nEnter second string ");
scanf("%s”,str2);
m = strcmp(str1,str2);
if ( m < 0 )
{
printf(“str1 is less than str2”);
}
else if ( m > 0 )
{
printf(“str1 is greater than str2”);
}
else
{
printf(“str1 is equal to str2”);
}
getch();
}
Output
Enter first string Good
Enter second string Good
str1 is equal to str2

You might also like