String
String
String: A string is a sequence of character enclosed with in double quotes (“ ”) but ends with \0. The
compiler puts \0 at the end of string to specify the end of the string. Whenever a string constant is written
anywhere in a program it is stored somewhere in a memory as an array of characters terminated by a
NULL character („\0‟).
Example – “m”
“Tajmahal”
To get a value of string variable we can use the two different types of formats.
Using scanf() function as: scanf(“%s”, string variable);
Using gets() function as : gets(string variable);
A sequence of characters enclosed in double quote is called string contants.
Eg: “COMPUTER”, “111111”, “11.001#” etc.
The scanf() function reads the sequence of characters until it encounters whitespace (space, newline, tab,
etc.).
Note: We have used the code name instead of &name with scanf(). This is because name is a
char array. The name in scanf() already points to the address of the first element in the string, which
is why we don't need to use &.
puts() : prints characters from the standard output just like printf() statement.
Syntax:
puts(char_array_variable/string);
/* Example Program For gets() and puts()*/
#include<stdio.h>
#include<conio.h>
void main()
{
char data[100];
printf("Enter a String for gets() :");
//get string input using gets(..) function
gets(data);
printf("Entered Data Is : will be with puts() :");
//print string using puts(..) function
puts(data);
getch();
}
C library supports a large number of string handling functions. Those functions are stored under the header
file #include<string.h> in the program.
1. strlen() function: strlen() is used to return the length of the string , that means counts the number
of characters present in a string.
Syntax integer variable = strlen (string variable);
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
char str[20];
int strlength;
clrscr();
printf(“Enter String:”);
gets(str);
strlength=strlen(str);
printf(“Given String Length Is: %d”, strlength);
getch();
}
OUTPUT:
Enter String:
Welcome
Given String Length Is: 7
2. strcpy() function:
strcpy() function is used to copy one string to another. strcpy() function copy the contents of
second string to first string.
Syntax:
strcpy(StringVariable1, StringVariable2);
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20], str2[20];
int res;
clrscr();
printf(“Enter First String:”);
scanf(“%s”,str1);
printf(“Enter Second String:”);
scanf(“%s”,str2);
strcpy(str1,str2);
printf(“First String is:%s”,str1);
printf(“Second String is:%s”,str2);
getch();
}
3. strcat() function: The strcat() is used to concatenate two strings. The second string will be
appended to the end of the first string. This process is called concatenation.
Syntax:
strcat (StringVariable1, StringVariable 2);
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20],str2[20];
clrscr();
printf(‚Enter First String:‛);
scanf(“%s”,str1);
printf(“Enter Second String:”);
scanf(“%s”,str2);
printf(“Concatenation String is:%s”, strcat(str1,str2));
getch();
}
Output:
Enter First String
Good
Enter Second String
Morning
Concatenation String is: GoodMorning
4. strcmp() function:
strcmp() function is used to compare two strings. strcmp() function does a case sensitive
comparison between two strings. The two strings are compared character by character until there is
a mismatch or end of one of the strings is reached (whichever occurs first). If the two strings are
identical, strcmp( ) returns a value zero. If they are not, it returns the numeric difference between
the ASCII values of the first non-matching pairs of characters.
Syntax:
strcmp(StringVariable1, StringVariable2);
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20], str2[20];
int res;
clrscr();
printf(“Enter First String:”);
scanf(“%s”,str1);
printf(“Enter Second String:”);
scanf(“%s”,str2);
res = strcmp(str1,str2);
printf(“ Compare String Result is:%d”,res);
getch();
}
Output:
Enter First String
Good
Enter Second String
Good
Compare String Result is: 0
5. strrev() function:
strrev() function is used to reverse characters in a given string.
Syntax:
strrev(StringVariable);
Example:
#include<conio.h>
#include<stdio.h>
void main()
{
char str[20];
clrscr();
printf(“Enter String:”);
gets(str);
printf(“Reverse String : %s”, strrev(str));
getch();
}
Output:
Enter String
WELCOME
Reverse String :
EMOCLEW
6. strlwr () function:
This function converts all characters in a given string from uppercase to lowercase letter.
Syntax: strlwr(StringVariable);
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
char str[20];
clrscr();
printf(“Enter String:”);
gets(str);
printf(“Lowercase String : %s”, strlwr(str));
getch();
}
Output:
Enter String
WELCOME
Lowercase String : welcome
7. strupr() function:
strupr() function is used to convert all characters in a given string from lower case to uppercase
letter.
Syntax:
strupr(Stringvariable);
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
char str[20];
clrscr();
printf(“Enter String:”);
gets(str);
printf(“Uppercase String : %s”, strupr(str));
getch();
}
Output:
Enter String
welcome
Uppercase String : WELCOME