Short Notes: Common Functions in String | Short Notes for Computer Science Engineering - Computer Science Engineering (CSE) PDF Download

Download, print and study this document offline
Please wait while the PDF view is loading
 Page 1


COMMON FUNCTIONS IN STRING
Type Method Description
char strcpy(s1, s2) Copy string
char strcat(s1, s2) Append string
int strcmp(s1, s2) Compare 2 strings
int strlen(s) Return string length
char strchr(s, int c) Find a character in string
char strstr(s1, s2) Find string s2 in string si
strcpy():
It is used to copy one string to another string. The content of the second string is copied to the 
content of the first string.
Syntax:
strcpy (string 1, string 2);
Example: 
char mystr[10];
mystr = “ Hello” ; / / Error! Illegal!!! Because we are assigning the value to mystr which
is not possible in case of an string. We can only use "= " at declarations of C-String.
strcpy(mystr, “ Hello” );
It sets value of mystr equal to “Hello”.
strcmp():
It is used to compare the contents of the two strings. If any mismatch occurs then it results the 
difference of ASCII values between the first occurrence of 2 different characters.
Syntax:
int strcmp(string 1, string 2);
Page 2


COMMON FUNCTIONS IN STRING
Type Method Description
char strcpy(s1, s2) Copy string
char strcat(s1, s2) Append string
int strcmp(s1, s2) Compare 2 strings
int strlen(s) Return string length
char strchr(s, int c) Find a character in string
char strstr(s1, s2) Find string s2 in string si
strcpy():
It is used to copy one string to another string. The content of the second string is copied to the 
content of the first string.
Syntax:
strcpy (string 1, string 2);
Example: 
char mystr[10];
mystr = “ Hello” ; / / Error! Illegal!!! Because we are assigning the value to mystr which
is not possible in case of an string. We can only use "= " at declarations of C-String.
strcpy(mystr, “ Hello” );
It sets value of mystr equal to “Hello”.
strcmp():
It is used to compare the contents of the two strings. If any mismatch occurs then it results the 
difference of ASCII values between the first occurrence of 2 different characters.
Syntax:
int strcmp(string 1, string 2);
Example:
char mystr_a[10] = “ Hello” ; 
char mystrb[10] = “ Goodbye” ;
- mystr a == mystrb; / / NOT allowed!
The correct way is 
if (strcmp(mystr a, mystr b )) 
printf ("Strings are NOT the same."); 
else
p rin tf "Strings are the same.");
Here it will check the ASCII value of H and G i.e, 72 and 71 and return the diference 1 . 
strcat():
It is used to concatenate i.e, combine the content of two strings.
Syntax:
strcat(string 1, string 2);
Example:
char fname[30]={“ bob” }; 
char lname[]={“ by” }; 
printf(“%s” , strcat(fname,lname));
Output:
bobby.
strlen():
It is used to return the length of a string. 
Syntax:
int strlen(string);
Page 3


COMMON FUNCTIONS IN STRING
Type Method Description
char strcpy(s1, s2) Copy string
char strcat(s1, s2) Append string
int strcmp(s1, s2) Compare 2 strings
int strlen(s) Return string length
char strchr(s, int c) Find a character in string
char strstr(s1, s2) Find string s2 in string si
strcpy():
It is used to copy one string to another string. The content of the second string is copied to the 
content of the first string.
Syntax:
strcpy (string 1, string 2);
Example: 
char mystr[10];
mystr = “ Hello” ; / / Error! Illegal!!! Because we are assigning the value to mystr which
is not possible in case of an string. We can only use "= " at declarations of C-String.
strcpy(mystr, “ Hello” );
It sets value of mystr equal to “Hello”.
strcmp():
It is used to compare the contents of the two strings. If any mismatch occurs then it results the 
difference of ASCII values between the first occurrence of 2 different characters.
Syntax:
int strcmp(string 1, string 2);
Example:
char mystr_a[10] = “ Hello” ; 
char mystrb[10] = “ Goodbye” ;
- mystr a == mystrb; / / NOT allowed!
The correct way is 
if (strcmp(mystr a, mystr b )) 
printf ("Strings are NOT the same."); 
else
p rin tf "Strings are the same.");
Here it will check the ASCII value of H and G i.e, 72 and 71 and return the diference 1 . 
strcat():
It is used to concatenate i.e, combine the content of two strings.
Syntax:
strcat(string 1, string 2);
Example:
char fname[30]={“ bob” }; 
char lname[]={“ by” }; 
printf(“%s” , strcat(fname,lname));
Output:
bobby.
strlen():
It is used to return the length of a string. 
Syntax:
int strlen(string);
Example:
char fname[30]={“ bob” }; 
int length=strlen(fname);
It will return 3
strchr():
It is used to find a character in the string and returns the index of occurrence of the character for 
the first time in the string.
Syntax:
strchr(cstr);
Example:
char mystr[] = "This is a simple string"; 
char pch = strchr(mystr, ‘ s ’ );
The output of pch is mystr[3] 
strstr():
It is used to return the existence of one string inside another string and it results the starting index 
of the string.
Syntax:
strstr(cstr1, cstr2);
Example:
Char mystr[]="This is a simple string"; 
char pch = strstr(mystr, “ simple” );
here pch will point to mystr[10]
Page 4


COMMON FUNCTIONS IN STRING
Type Method Description
char strcpy(s1, s2) Copy string
char strcat(s1, s2) Append string
int strcmp(s1, s2) Compare 2 strings
int strlen(s) Return string length
char strchr(s, int c) Find a character in string
char strstr(s1, s2) Find string s2 in string si
strcpy():
It is used to copy one string to another string. The content of the second string is copied to the 
content of the first string.
Syntax:
strcpy (string 1, string 2);
Example: 
char mystr[10];
mystr = “ Hello” ; / / Error! Illegal!!! Because we are assigning the value to mystr which
is not possible in case of an string. We can only use "= " at declarations of C-String.
strcpy(mystr, “ Hello” );
It sets value of mystr equal to “Hello”.
strcmp():
It is used to compare the contents of the two strings. If any mismatch occurs then it results the 
difference of ASCII values between the first occurrence of 2 different characters.
Syntax:
int strcmp(string 1, string 2);
Example:
char mystr_a[10] = “ Hello” ; 
char mystrb[10] = “ Goodbye” ;
- mystr a == mystrb; / / NOT allowed!
The correct way is 
if (strcmp(mystr a, mystr b )) 
printf ("Strings are NOT the same."); 
else
p rin tf "Strings are the same.");
Here it will check the ASCII value of H and G i.e, 72 and 71 and return the diference 1 . 
strcat():
It is used to concatenate i.e, combine the content of two strings.
Syntax:
strcat(string 1, string 2);
Example:
char fname[30]={“ bob” }; 
char lname[]={“ by” }; 
printf(“%s” , strcat(fname,lname));
Output:
bobby.
strlen():
It is used to return the length of a string. 
Syntax:
int strlen(string);
Example:
char fname[30]={“ bob” }; 
int length=strlen(fname);
It will return 3
strchr():
It is used to find a character in the string and returns the index of occurrence of the character for 
the first time in the string.
Syntax:
strchr(cstr);
Example:
char mystr[] = "This is a simple string"; 
char pch = strchr(mystr, ‘ s ’ );
The output of pch is mystr[3] 
strstr():
It is used to return the existence of one string inside another string and it results the starting index 
of the string.
Syntax:
strstr(cstr1, cstr2);
Example:
Char mystr[]="This is a simple string"; 
char pch = strstr(mystr, “ simple” );
here pch will point to mystr[10]
String input/output library functions
Function prototype Function description
int getchar(void); Inputs the next character from the 
standard input and returns it as integer
int putchar(int c); Prints the character stored in c and 
returns it as an integer
int puts( char s); Prints the string s followed by new line 
character. Returns a non-zero integer if 
possible or EOF if an error occurs
int sprint(char s, char format,....) Equivalent to printf,except the output is 
stored in the array s instead of printed in 
the screen. Returns the no.of characters 
written to s, or EOF if an error occurs
int sprint(char s, char form at,..) Equivalent to scanf, except the input is 
read from the array s rather than from 
the keyboard. Returns the no.of items 
successfully read by the function , or 
EOF if an error occurs
NOTE:
Character arrays are known as strings.
Self-review exercises:
1 . Find the error in each of the following program segments and explain how to correct it:
• char s[10];
• strcpy(s,” hello” ,5);
• prinf(“%s\n” ,s);
• printf(“%s” , ’ a ’ );
• char s[12]; strcpy(s,” welcome home ” );
• I f ( strcmp(string 1, sring 2))
{ printf(“ the strings are equal\n” );
}
2. Show 2 different methods of initializing character array vowel with the string of vowels 
“AEIOU”?
3. Writ a program to convert string to an integer?
Page 5


COMMON FUNCTIONS IN STRING
Type Method Description
char strcpy(s1, s2) Copy string
char strcat(s1, s2) Append string
int strcmp(s1, s2) Compare 2 strings
int strlen(s) Return string length
char strchr(s, int c) Find a character in string
char strstr(s1, s2) Find string s2 in string si
strcpy():
It is used to copy one string to another string. The content of the second string is copied to the 
content of the first string.
Syntax:
strcpy (string 1, string 2);
Example: 
char mystr[10];
mystr = “ Hello” ; / / Error! Illegal!!! Because we are assigning the value to mystr which
is not possible in case of an string. We can only use "= " at declarations of C-String.
strcpy(mystr, “ Hello” );
It sets value of mystr equal to “Hello”.
strcmp():
It is used to compare the contents of the two strings. If any mismatch occurs then it results the 
difference of ASCII values between the first occurrence of 2 different characters.
Syntax:
int strcmp(string 1, string 2);
Example:
char mystr_a[10] = “ Hello” ; 
char mystrb[10] = “ Goodbye” ;
- mystr a == mystrb; / / NOT allowed!
The correct way is 
if (strcmp(mystr a, mystr b )) 
printf ("Strings are NOT the same."); 
else
p rin tf "Strings are the same.");
Here it will check the ASCII value of H and G i.e, 72 and 71 and return the diference 1 . 
strcat():
It is used to concatenate i.e, combine the content of two strings.
Syntax:
strcat(string 1, string 2);
Example:
char fname[30]={“ bob” }; 
char lname[]={“ by” }; 
printf(“%s” , strcat(fname,lname));
Output:
bobby.
strlen():
It is used to return the length of a string. 
Syntax:
int strlen(string);
Example:
char fname[30]={“ bob” }; 
int length=strlen(fname);
It will return 3
strchr():
It is used to find a character in the string and returns the index of occurrence of the character for 
the first time in the string.
Syntax:
strchr(cstr);
Example:
char mystr[] = "This is a simple string"; 
char pch = strchr(mystr, ‘ s ’ );
The output of pch is mystr[3] 
strstr():
It is used to return the existence of one string inside another string and it results the starting index 
of the string.
Syntax:
strstr(cstr1, cstr2);
Example:
Char mystr[]="This is a simple string"; 
char pch = strstr(mystr, “ simple” );
here pch will point to mystr[10]
String input/output library functions
Function prototype Function description
int getchar(void); Inputs the next character from the 
standard input and returns it as integer
int putchar(int c); Prints the character stored in c and 
returns it as an integer
int puts( char s); Prints the string s followed by new line 
character. Returns a non-zero integer if 
possible or EOF if an error occurs
int sprint(char s, char format,....) Equivalent to printf,except the output is 
stored in the array s instead of printed in 
the screen. Returns the no.of characters 
written to s, or EOF if an error occurs
int sprint(char s, char form at,..) Equivalent to scanf, except the input is 
read from the array s rather than from 
the keyboard. Returns the no.of items 
successfully read by the function , or 
EOF if an error occurs
NOTE:
Character arrays are known as strings.
Self-review exercises:
1 . Find the error in each of the following program segments and explain how to correct it:
• char s[10];
• strcpy(s,” hello” ,5);
• prinf(“%s\n” ,s);
• printf(“%s” , ’ a ’ );
• char s[12]; strcpy(s,” welcome home ” );
• I f ( strcmp(string 1, sring 2))
{ printf(“ the strings are equal\n” );
}
2. Show 2 different methods of initializing character array vowel with the string of vowels 
“AEIOU”?
3. Writ a program to convert string to an integer?
4. Write a program to accept a line of text and a word. Display the no. of occurrences of that 
word in the text?
5. Write a program to read a word and re-write its characters in alphabetical order.
6. Write a program to insert a word before a given word in the text.
7. Write a program to count the number of characters, words and lines in the given text.
Read More
90 docs

Top Courses for Computer Science Engineering (CSE)

Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

study material

,

Important questions

,

Semester Notes

,

Free

,

Exam

,

ppt

,

Short Notes: Common Functions in String | Short Notes for Computer Science Engineering - Computer Science Engineering (CSE)

,

Objective type Questions

,

past year papers

,

Short Notes: Common Functions in String | Short Notes for Computer Science Engineering - Computer Science Engineering (CSE)

,

Viva Questions

,

Short Notes: Common Functions in String | Short Notes for Computer Science Engineering - Computer Science Engineering (CSE)

,

Summary

,

pdf

,

Sample Paper

,

shortcuts and tricks

,

video lectures

,

MCQs

,

practice quizzes

,

Previous Year Questions with Solutions

,

mock tests for examination

,

Extra Questions

;