Pps Unit-II Strings
Pps Unit-II Strings
Dennis Ritchie
String Handling Functions in C:
C programming language provides a set of pre-defined functions
called string handling functions to work with string values.
The string handling functions are defined in a header file
called string.h. Whenever we want to use any string handling
function we must include the header file called string.h.
Few commonly used string handling functions are:
Function work of function
strlen() computes string's length
strcpy() copies a string to another
strcat() concatenates(joins) two strings
strcmp() compares two strings
strlwr() converts string to lowercase
strupr() converts string to uppercase
1) strlen( ) Function :
strlen( ) function is used to find the length of a character string.
(or)
The strlen() function returns the length of the given string. It
doesn't count null character '\0'.
Syntax: n = strlen(string name);
It is defined in the <string.h> header file.
Ex: int n;
char st[20] = “Bangalore”;
n = strlen(st);
returns total number of characters in string
o/p: 9
#include <stdio.h>
#include <string.h>
int main()
{
char a[20]="Program";
char b[20]= {'P‘,‘ r', 'o ', 'g‘ ,'r ','a‘ ,'m‘ ,'\0'};
printf("Length of string a = %d \n“ ,strlen(a));
printf("Length of string b = %d \n “,strlen(b));
return 0;
}
o/p: Length of string a = 7
Length of string b = 7
2)strcpy( ) Function :
• If destination string length is less than source string, entire source string value
won’t be copied into destination string.
For example, consider destination string length is 20 and source string length is 30.
Then, only 20 characters from source string will be copied into destination
string and remaining 10 characters won’t be copied and will be truncated.
Syntax:
char a = getchar();
putchar(): use to print a single character to the standerd output
The function puts only single character at a time.
All these functions are available in 'stdio.h' header file.
Syntax:
putchar(a);
#include<stdio.h>
void main()
{
char c;
printf(“Enter the character :”);
c=getchar();
printf(“ entered character is :");
putchar(c);
}
o/p: Enter the character : A
entered character is:A