Sai-Sudhir Degree & PG College: Subject: Programming With C & C++ Unit: III TOPIC: String Function String Function
Sai-Sudhir Degree & PG College: Subject: Programming With C & C++ Unit: III TOPIC: String Function String Function
String function
Syntax:
Initialization:
Example:
Or
char[8]={„w‟,‟e‟,‟l‟,‟c‟,‟o‟,‟m‟,‟e‟,};
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
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;
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
#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;
4
Sai-Sudhir Degree & PG College
1. String length:
5
Sai-Sudhir Degree & PG College
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
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
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;
}
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
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