Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Unit-3 String Function

Uploaded by

tv5874
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Unit-3 String Function

Uploaded by

tv5874
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

School of Computing Science and Engineering

Course Code : R1UC101B Name: Programming for Problem Solving-C

UNIT 3
C String Predefined Functions

Faculty Name: Hradesh Kumar Program Name: B.Tech.


String Function

S.No Function Description


1) strlen(string_name) returns the length of string name.
2) strcpy(destination, source) copies the contents of source string to
destination string.
3) strcat(first_string, concats or joins first string with second
second_string) string. The result of the string is stored
in first string.
4) strcmp(first_string, compares the first string with second
second_string) 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.
String Function

 strlen(string_name)
 returns the length of string name

#include<stdio.h>
#include <string.h>
int main(){
char ch[20]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};
printf("Length of string is: %d",strlen(ch));
return 0;
}
Output: Length of string is: 10
String Function

 strcpy(destination, source)
 copies the contents of source string to destination string.

#include<stdio.h>
#include <string.h>
int main(){
char ch[20]={‘g', 'a', ‘l', ‘g', ‘o', ‘t', ‘i', ‘a', ‘s', '\0'};
char ch2[20];
strcpy(ch2,ch);
printf("Value of second string is: %s",ch2);
return 0;
}
Output: Value of second string is: galgotias
String Function

 strcat(first_string, second_string)
 concats or joins first string with second string. The result of the string is

stored in first string.


#include<stdio.h>
#include <string.h>
int main(){
char ch[10]={'h', 'e', 'l', 'l', 'o', '\0'};
char ch2[10]={'c', '\0'};
strcat(ch,ch2);
printf("Value of first string is: %s",ch);
return 0;
} Output: Value of first string is: helloc
String Function

 strcmp(first_string, second_string)
 compares the first string with second string. If both strings are same, it

returns 0. if(strcmp(str1,str2)==0)
#include<stdio.h> printf("Strings are equal");
#include <string.h> else
int main(){ printf("Strings are not equal");
char str1[20],str2[20]; return 0;
printf("Enter 1st string: "); }
gets(str1);//reads string from
console Output: Enter 1st string: hello
printf("Enter 2nd string: "); Enter 2nd string: hello
gets(str2); Strings are equal
String Function

 strrev(string)
 returns reverse string Output:
#include<stdio.h> Enter string: galgotiasuniversity
#include <string.h> String is: galgotiasuniversity
int main(){
Reverse String is:
char str[20];
printf("Enter string: ");
ytisrevinusaitoglag
gets(str);//reads string from console
printf("String is: %s",str);
printf("\nReverse String is: %s",strrev(str));
return 0;
}
String Function

 strlwr(string)
 returns string characters in lowercase.

#include<stdio.h> Output:
#include <string.h> Enter string: GALGOTIASuniversity
int main(){
char str[20];
String is: GALGOTIASuniversity
printf("Enter string: "); Lower String is: galgotiasuniversity
gets(str);//reads string from console
printf("String is: %s",str);
printf("\nLower String is: %s",strlwr(str));
return 0;
}
String Function

 strupr(string)
 returns string characters in uppercase.

#include<stdio.h> Output:
#include <string.h> Enter string: galgotiasuniversity
int main(){
char str[20];
String is: galgotiasuniversity
printf("Enter string: "); Upper String is:
gets(str);//reads string from console GALGOTIASUNIVERSITY
printf("String is: %s",str);
printf("\nUpper String is: %s",strupr(str));
return 0;
}
References

 http://kirste.userpage.fu-berlin.de/chemnet/use/info/libc/libc_7.html
 Let Us C by Yashavant Kanetkar : Authentic Guide to C PROGRAMMING Language 17th
Edition, BPB Publications
 C in Depth by by S.K.Srivastava and Deepali Srivastava, BPB Publications

You might also like