STRINGS Unit3
STRINGS Unit3
Definition:
A string is defined as the sequence of characters terminated by the special character ‘\0’ (null
character).
Strings are always enclosed in double quotes as “string constant”.
In C, a string variable is any valid C variable name and is always declared as an array of
characters.
The general form of declaration of string variables is:
String-name defines the name of the string variable. It must be a valid ‘C’ identifier.
name [0] name [1] name [2] name [3] name [4]
r a m u \0
Example:
/*Example program to illustrate the strings initialization*/
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20]="college";
char str2[20]={"college"};
char str3[20]={'c','o','l','l', 'e', 'g','e'};
1
clrscr();
printf("%s\n",str1);
printf("%s\n",str2);
printf("%s\n",str3);
getch();
}
Output: college
college
college
scanf ():
The scanf () is an input function which is used to accept a string into a string variable.
It is used to accept a string into str up to a white space character (Blank Space, New Line character,
Tab space). The null character ‘\0’ will be automatically appended to the string. %s is the format
specifier for strings.
printf ():
It displays the string contained in str. %s is the format specifier for strings. str is the string variable
the contents of which are to be displayed.
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
char str[15]; output:
clrscr(); Enter string into str: jkc
printf("Enter string into str:\n"); str=jkc
scanf("%s",str);
2
printf("str=%s\n",str);
getch();
}
getchar():
getchar() is an input function which is used to read a character through standard input device,
keyboard.
syntax: ch = getchar()
Where ch is a variable of char type. As a result of this, a character typed at the keyboard is
assigned to the variable ch.
putchar():
synatx: putchar(ch);
Where ch represents variable of type char. It display the character stored in ch on the screen.
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("Enter any character into ch:\n");
ch=getchar();
printf("The character in ch is:");
putchar(ch);
getch();
}
Output:
Enter any character into ch:
D
The charcater in ch is: D
gets() is an input function which is used to accept a string up to a newline character into a
string variable. It automatically appends the null character ‘\0’ to the end of the string.
syntax: gets(variablename);
Example: gets(str);
It accepts a string up to a new line character into str. If str is “jkc college”, the entire string gets
accepted.
puts():
puts () is an output function which is used to display a string contained in a string variable. It
also adds the new line character ‘\n’ to the string automatically.
syntax: puts(variableaname);
example: puts(str);
Example:
/*To illustrate gets() and puts() functions*/
#include<stdio.h>
#include<conio.h>
void main()
{
char str[30];
clrscr();
printf("Enter any string into str:\n");
gets(str);
printf("The string in str is:\n"); output:
puts(str); Enter any string into str:
getch(); welcome to jkc
} The string in str is:
Welcome to jkc
1) strlen()
2) strcpy()
3) strcat()
4) strcmp()
5) strrev()
6) strlwr()
7) strupr()
All these string handling built-in functions are declared in the header file string.h, this file has to be
included in all the programs which use these functions with the help of preprocessor directive
#include.
1) strlen():
This function is used to counts the number of characters excluding the null character ‘\0’
in the string.
Synatx: strlen(variablename);
Example: strlen(str);
Argument str represents a string. It can be a string constant or a string variable. The function
returns an inetger value, which is the length of the string.
Example:
/* Write a program to find the length of the string*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int count;
char str[30];
clrscr();
printf("enter string into str:\n");
gets(str);
printf("The string in str is:");
puts(str);
count=strlen(str);
printf("\nThe total no of characters in str is:%d",count);
getch();
}
Output:
enter string into str:
welcome to jkc college
The string in str is:welcome to jkc college
The total no of characters in str is:21
5
2) strcpy():
This function is used to copy the content of one string into another string.
Synatx: strcpy(variablename1,variablename2);
Example: strcpy(str1,str2);
Example:
3) strcat():
This function is used to concatenates one string to the end of another string.
Synatx: strcat(variablename1,variablename2);
Example: strcat(str1,str2);
Here the string contained in str2 concatenates to the end of the string contained in str1.
Example:
4) strcmp():
Synatx: strcmp(variablename1,variablename2);
Example: strcmp(str1,str2);
Here the string contained in str1 is compared with the string in str2.
Example:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i;
char str1[30],str2[30];
7
clrscr();
printf("enter string into str1:\n");
gets(str1);
printf("enter string into str2:\n");
gets(str2);
printf("The string in str1 is:");
puts(str1);
printf("The string in str2 is:");
puts(str2);
i=strcmp(str1,str2);
printf("\ndifferene between str1 and str2 is:%d",i);
getch();
}
Output:
enter string into str1:
bsc
enter string into str2:
bca
The string in str1 is:bsc
The string in str2 is:bca
difference between str1 and str2 is:16
5) strrev():
Synatx: strrev(variablename1);
Example: strrev(str1);
Example:
/* Write a program to reverse the string*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[30];
clrscr();
printf("enter string into str1:\n");
gets(str1);
printf("The string in str1 is:");
puts(str1);
strrev(str1);
printf("\nRevresed string in str1 is:");
puts(str1);
getch();
}
Output:
8
enter string into str1:
bca
The string in str1 is:bca
Reversed string in str1 is:acb
6)strlwr():
The STRLWR() Function used to converts given string to lowercase.
Syntax:
strlwr(str1)
#include<string.h>
int main()
{
char str[100];
printf("Please, Enter the String = ");
gets(str);
printf(" \n The Lower case String is = %s",strlwr(str));
return 0;
}
7.strupr()
The STRUPR() Function used to converts given string to uppercase.
Syntax:
strupr(str1)
#include<string.h>
int main()
{
char str[100];
gets(str);
return 0;