Sai-Sudhir Degree & PG College
Course: B.com (App) - I Year Date: 16-04-2020
Subject: Programming with C & C++ Unit: III
TOPIC: String function
String function
String is the collection of characters enclosed in double
quotes
The size of the string is equal to number of characters
plus one where plus one indicates the null character („\0‟)
which indicates the end of the string.
We have use character data type to declare the string
variable and we have to mention the size of the string
inside the square brackets.
String is also called as array of characters.
Declare the string
Syntax:
<Data type><Variable name>[size];
Ex) char ch[8];
Form the above syntax we should use data type as
character, variable name is user define name and size
is the no of characters in the string.
Initialization:
Initialzation means assign the value to the string
variable.
1
Sai-Sudhir Degree & PG College
The value can be given in double quotes or in the
single quotes
Example:
Char ch[15]=”morning students”;
Or
char[8]={„w‟,‟e‟,‟l‟,‟c‟,‟o‟,‟m‟,‟e‟,};
Read and write String using scanf() and printf() in C
To read a string we can use a input function scanf() and
to printf the output we can use the output function
printf()
Syntax:
Scanf(“format sting”, Address list of variable)
We can use %d for interger , %f for float , %c for
character and %s for string
Address list of variable indicates the address of the
variable which is stored in the memory,so we have to use
& along with variable name.
Syntax:
printf(“format sting”, list of variable)
We can use %d for interger , %f for float , %c for
character and %s for string
List of variable indicates the variables which are to be
printed on the screen
2
Sai-Sudhir Degree & PG College
Note: Input function scanf() can accept the string
without blank space if recognizes the space it thinks the
string has ended
Sample program:
/* Write a program in C to read and print the string
using scanf() and printf() function */
#include<stdio.h>
#include<conio.h>
int main()
{
char str[20];
printf("Enter any sentence: ");
scanf("%s", str);
printf("\nYou have entered:\n");
printf("%s", str);
getch();
return 0;
Read and Print String using gets() and puts() in C
To read a string we can use a input function gets() and to
print the string we have to use output function puts()
Syntax:
Gets( variable name);
3
Sai-Sudhir Degree & PG College
Char c[6];
Gets(c );
To write a string we can use a output function puts() and
Syntax:
puts( variable name);
Char c[6];
puts(c );
Sample program
Write a program in C to read and print the string using
gets() and puts() function
#include<stdio.h>
#include<conio.h>
int main()
{
char str[100];
printf("Enter any sentence: ");
gets(str);
printf("\nYou have entered:\n");
puts(str);
getch();
return 0;
String standard functions or string built-in functions
4
Sai-Sudhir Degree & PG College
All the Built-in functions are stored in the header file
#include<string.h>
Some of the Built-in function are listed below which are
frequently used they are:
No. Function Description
1) strlen(string_name) Returns the length of string
name.
2) strcpy(destination, Copies the contents of source
source) string to destination string.
3) strcat(first_string, concats or joins first string with
second-string) second string. The result of the
string is stored in first string.
4) strcmp(first_string, Compares the first string with
second_string) second string. If both strings
are same, it returns 0.
5) strrev(string) Returns reverse string.
6) strlwr(string) Returns string characters in
lowercase.
7) strupr(string) Returns string characters in
uppercase.
1. String length:
5
Sai-Sudhir Degree & PG College
The Built-in function strlen() is used to find the length
of the given string
The Built-in function strlen() is stored in the header file
#include<string.h>
Sample program
#include<stdio.h>
#include <string.h>
int main()
{
char ch[20]=”Good morning students”;
printf("Length of string is: %d",strlen(ch));
return 0;
}
2. String Copy:
The Built-in function strcpy() is used to copy the string
which is already existing
The Built-in function strcpy()is stored in the header
file #include<string.h>
Sample program
#include<stdio.h>
#include <string.h>
int main()
{
6
Sai-Sudhir Degree & PG College
char ch1[20]=”Good morning students”;
char ch2[20];
strcpy(ch2,ch1);
printf("Value of second string is: %s",ch2);
return 0;
}
3. String Concatenate:
The Built-in function strcat() is combine the two string
and displays as single string
The Built-in function strcat()is stored in the header file
#include<string.h>
Sample program
#include<stdio.h>
#include <string.h>
int main()
{
char ch1[10]=Morning;
char ch2[10]=students;
strcat(ch1,ch2);
printf("Value of first string is: %s",ch1);
return 0;
}
4. String Comparison
7
Sai-Sudhir Degree & PG College
The Built-in function strcmp() is used to compare two
string whether they are equal or not
The Built-in function strcmp()is stored in the header
file #include<string.h>
Sample program:
#include<stdio.h>
#include <string.h>
int main()
{
char str1[6]=”hello”;
char str2[6]=”help” ;
if(strcmp(str1,str2)==0)
printf("Strings are equal");
else
printf("Strings are not equal");
return 0;
}
5. String Reverse:
The Built-in function strrev() is used to display the
given string in reverse order(Back to front)
The Built-in function strrev()is stored in the header file
#include<string.h>
8
Sai-Sudhir Degree & PG College
Sample program
#include<stdio.h>
#include <string.h>
int main()
{
char str[9] = “Welcome”;
printf("String is: %s",str);
printf("\nReverse String is: %s",strrev(str));
return 0;
}
6. String Lower case:
The Built-in function strlwr() is used to convert the
given string into lower case
The Built-in function strlwr()is stored in the header file
#include<string.h>
Sample program:
#include<stdio.h>
#include <string.h>
int main()
{
char str[17]=”WELCOME STUDENTS”;
printf("String is: %s",str);
9
Sai-Sudhir Degree & PG College
printf("\nLower String is: %s",strlwr(str));
return 0;
}
7. String Upper case:
The Built-in function strupr() is used to convert the
given string into upper case
The Built-in function strupr()is stored in the header
file #include<string.h>
Sample program:
#include<stdio.h>
#include <string.h>
int main()
{
char str[17]=”welcome students”;
printf("String is: %s",str);
printf("\nUpper String is: %s",strupr(str));
return 0;
}
10