SlideShare a Scribd company logo
2
Most read
4
Most read
5
Most read
STRINGS
WHAT IS STRING
The string can be defined as the one-dimensional array of characters terminated by a null ('0').
This null character indicates the end of the string.
Each character in the array occupies one byte of memory, and the last character must always be
0. The termination character ('0') is important in a string since it is the only way to identify
where the string ends.
Strings are always enclosed by double quotes. Whereas, character is enclosed by single quotes in
C.
Example: ‘Kedia’, “Kedia”
DECLARING AND INITIALIZING STRING
There are two ways to declare and initializing a string in c language.
1. By char array
2. By string literal
Declaring String
Syntax: Datatype string_variable_name [array_Size];
Example: char s[5];
When we declare string array with size 5 then it will create sequential memory.
As we know, array index starts from 0, so it will be represented as in the figure given below.
Initializing String:
Initializing means while declaring char array if we assign elements or literals.
Syntax: Datatype string_variable_name [array_size] ={characters};
Example: char s[5]= {‘K’,’E’,’D’,’I’,’A’,’0’};
Prasadu peddi R.G. Kedia College of commerce
What is NULL Char “0”?
'0' represents the end of the string. It is also referred as String terminator & Null Character.
Declaring String by string Literal:
char ch[]="RGKEDIA";
Difference between char array and string literal:
There is main difference between char array and String literal.
We need to add the null character '0' at the end of the array by ourself whereas, it is
appended internally by the compiler in the case of the string literal.
String Example in C
Let's see a simple example where a string is declared and being printed. The '%s' is used as
a format specifier for the string in c language.
#include<stdio.h>
#include <string.h>
Void main()
{
char str1[8]={'R', 'G', 'K', 'E', 'D', 'I', 'A', '0'};
char str2[8] ="RGKEDIA";
printf("Char Array Value is: %sn", str1);
printf("String Literal Value is: %sn", str2);
}
READING AND WRITING STRINGS
Accepting string as the input:
Till now, we have used scanf to accept the input from the user. However, it can also be used
in the case of strings but with a different scenario. The '%s' is used as a format specifier for
the string in c language. In Scanf() for reading integer and float values format specifier %d,
%f and “&” required where as for reading string & not required.
#include<stdio.h>
void main ()
{
Prasadu peddi R.G. Kedia College of commerce
char s[20];
printf("Enter the string?");
scanf("%s",s);
printf("You entered %s",s);
}
Output1:
Enter the string?
Corona
You entered corona
Output2:
Enter the string?
Corona is a virus
You entered corona
Note: in output1, input string is ‘corona’ and it is displayed as it is where as in output2,
input is ‘corona is a virus’ but output is corona. It is clear from the output2 that, the Scanf
will not work for space separated strings.
The problem with the scanf function is that it never reads an entire string. It will halt the reading
process as soon as whitespace, suppose we give input as "B.COM First Year" then the scanf
function will never read an entire string as a whitespace character occurs between the two names.
The scanf function will only read B.COM and it ignores remaining strings.
gets() Function:
The gets() function is similar to scanf() function. In order to read a string contains spaces, we use
the gets() function. Gets ignores the whitespaces. It stops reading when a newline is reached (the
Enter key is pressed).
The gets() function enables the user to enter some characters followed by the enter key. All the
characters entered by the user get stored in a character array. The null character is added to the
array to make it a string. The gets() allows the user to enter the space-separated strings. It returns
the string entered by the user.
Reading string using gets():
#include<stdio.h>
Prasadu peddi R.G. Kedia College of commerce
void main ()
{
char s[30];
printf("Enter the string? ");
gets(s);
printf("You entered %s",s);
}
OUTPUT
Enter the string?
RG Kedia College
You entered RG Kedia College
puts() function
The puts() function is very much similar to printf() function. The puts() function is used to
print the string on the console which is previously read by using gets() or scanf() function.
The puts() function returns an integer value representing the number of characters being
printed on the console. The difference between printf and puts is printf display integer, float,
and char values where as puts used for only to display string.
#include<stdio.h>
#include <string.h>
void main()
{
char name[50];
printf("Enter string: ");
gets(name); //reads string from user
printf("Your string is: ");
puts(name); //displays string
}
OUTPUT
Enter string:
Corona virus disease also called COVID
Your string is: Corona virus disease also called COVID
Prasadu peddi R.G. Kedia College of commerce
STRING STANDARD FUNCTIONS
1) strlen(string_name) function is used to find the length of a character string.
It doesn't count null character '0'.
The strlen() function takes a string as an argument and returns its length. The returned
value is of type int.
It is defined in the <string.h> header file.
Example: int n;
char st[20] = “studnet”;
n = strlen(st);
#include<stdio.h>
#include <string.h>
int main()
{
Int len;
char ch[20]=”student”;
len=strlen(ch);
printf("Length of string is: %d",len);
return 0;
}
2) strcpy(destination, source) copies the contents of source string to destination string.
The strcpy() function copies the string to the another character array.
The strcpy() function is defined in the string.h header file.
strcpy ( str1, str2) – It copies contents of str2 into str1.
strcpy ( str2, str1) – It copies contents of str1 into str2.
#include<stdio.h>
#include <string.h>
int main()
{
Prasadu peddi R.G. Kedia College of commerce
char cp1[20]={'s’,’t’,’u’,’d’,’e’,’n’,’t’,’0’};
char cp2[20];
strcpy(cp2,cp1);
puts(cp2);
return 0;
}
3) strcat(first_string, second_string) concats or joins first string with second string. The
result of the string is stored in first string.
It takes two arguments, i.e, two strings or character arrays, and stores the resultant concatenated
string in the first string specified in the argument.
The strcat() function is defined in <string.h> header file.
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "This is ", str2[] = "online class";
//concatenates str1 and str2 and resultant string is stored in str1.
strcat(str1,str2);
puts(str1);
puts(str2);
return 0;
}
output
This is online class
Online class
Prasadu peddi R.G. Kedia College of commerce
4) strcmp(first_string, second_string) compares the first string with second string. If both
strings are same, it returns 0.
strcmp( ) function is case sensitive. i.e., “A” and “a” are treated as different characters.
If length of string1 < string2, it returns < 0 value. If length of string1 > string2, it returns > 0
value.
It is defined in the string.h header file.
int n;
n = strcmp(city, town);
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
int result;
// comparing strings str1 and str2
result = strcmp(str1, str2);
printf("strcmp(str1, str2) = %dn", result);
// comparing strings str1 and str3
result = strcmp(str1, str3);
printf("strcmp(str1, str3) = %dn", result);
return 0;
}
OUTPUT
strcmp(str1, str2) = 32
strcmp(str1, str3) = 0
Prasadu peddi R.G. Kedia College of commerce
5) strrev(string) returns reverse string.
This function returns reverse of the given string.
#include<stdio.h>
#include <string.h>
int main(){
char str[20];
printf("Enter string: ");
gets(str);//reads string from console
puts(str);
printf("Reverse String is: %s",strrev(str));
return 0;
}
OUTPUT
Enter string: student
String is: student
Reverse String is: tneduts
6) strlwr(string) returns string characters in lowercase.
7) strupr(string)returns string characters in uppercase.
Prasadu peddi R.G. Kedia College of commerce

More Related Content

What's hot (20)

DOCX
Nor gate
Anupam Narang
 
PPTX
Matter in our surrounding
Abhishek Kumar
 
PPTX
Presentation on bipolar junction transistor
Kawsar Ahmed
 
PPTX
9th sound NCERT
Arif Ansari
 
PPTX
Rectifier and Filter circuits (chapter14)
DHARUN MUGHILAN
 
PPTX
Electricity- Class-X-CBSE
Imaginative Brain Science
 
PPT
Outer Space Powerpoint
AHAL0366
 
PDF
Class 7th science chapter 8. Winds, Storms and Cyclones
Swayam Khobragade
 
PPTX
Class 6 chapter 8 ppt 2
Ashish Jaswal
 
PPTX
Air around us
Jaideep Ramesh
 
DOCX
Light Emitting Diode Presentation Report
Devansh Gupta
 
PPTX
Basics of MOSFET
EFY HR
 
PPT
Chemical effects of electric current
Deep Sharma
 
PPTX
Instrumentation & Measurement: Electromechanical Instruments
Muhammad Junaid Asif
 
PPTX
Coal and Petroleum
Fiza A.
 
PPTX
Transistor notes
Priyank Jain
 
PPTX
Solenoid
Rishikesh Borse
 
PDF
Periodic Classification of Elements
Pawan Kumar Sahu
 
PPT
PHYSICS OF SEMICONDUCTOR DEVICES
Vaishnavi Bathina
 
PPTX
FOOD: WHERE DOES IT COME FROM? -CLASS VI (CBSE)
SONU ACADEMY
 
Nor gate
Anupam Narang
 
Matter in our surrounding
Abhishek Kumar
 
Presentation on bipolar junction transistor
Kawsar Ahmed
 
9th sound NCERT
Arif Ansari
 
Rectifier and Filter circuits (chapter14)
DHARUN MUGHILAN
 
Electricity- Class-X-CBSE
Imaginative Brain Science
 
Outer Space Powerpoint
AHAL0366
 
Class 7th science chapter 8. Winds, Storms and Cyclones
Swayam Khobragade
 
Class 6 chapter 8 ppt 2
Ashish Jaswal
 
Air around us
Jaideep Ramesh
 
Light Emitting Diode Presentation Report
Devansh Gupta
 
Basics of MOSFET
EFY HR
 
Chemical effects of electric current
Deep Sharma
 
Instrumentation & Measurement: Electromechanical Instruments
Muhammad Junaid Asif
 
Coal and Petroleum
Fiza A.
 
Transistor notes
Priyank Jain
 
Solenoid
Rishikesh Borse
 
Periodic Classification of Elements
Pawan Kumar Sahu
 
PHYSICS OF SEMICONDUCTOR DEVICES
Vaishnavi Bathina
 
FOOD: WHERE DOES IT COME FROM? -CLASS VI (CBSE)
SONU ACADEMY
 

Similar to String notes (20)

PPT
Strings
Mitali Chugh
 
PPTX
Computer Programming Utilities the subject of BE first year students, and thi...
jr2710
 
PPTX
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
hanumanthumanideeph6
 
PPTX
programming for problem solving using C-STRINGSc
TonderaiMayisiri
 
PDF
[ITP - Lecture 17] Strings in C/C++
Muhammad Hammad Waseem
 
PDF
Strings IN C
yndaravind
 
PPTX
pps unit 3.pptx
mathesh0303
 
PPTX
Week6_P_String.pptx
OluwafolakeOjo
 
PPT
14 strings
Rohit Shrivastava
 
PPT
THE FORMAT AND USAGE OF STRINGS IN C.PPT
shanthabalaji2013
 
DOCX
C Programming Strings.docx
8759000398
 
PPT
Cfbcgdhfghdfhghggfhghghgfhgfhgfhhapter11.PPT
JITENDER773791
 
PPT
Chapterabcdefghijklmnopqrdstuvwxydanniipo
abritip
 
PDF
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
PPT
Lecture 05 2017
Jesmin Akhter
 
PPTX
Handling of character strings C programming
Appili Vamsi Krishna
 
PDF
Principals of Programming in CModule -5.pdfModule-4.pdf
anilcsbs
 
PPTX
String_Slide.pptxStructure.pptxStructure.pptxStructure.pptxStructure.pptx
monimhossain14
 
PPTX
C programming - String
Achyut Devkota
 
DOCX
Unitii string
Sowri Rajan
 
Strings
Mitali Chugh
 
Computer Programming Utilities the subject of BE first year students, and thi...
jr2710
 
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
hanumanthumanideeph6
 
programming for problem solving using C-STRINGSc
TonderaiMayisiri
 
[ITP - Lecture 17] Strings in C/C++
Muhammad Hammad Waseem
 
Strings IN C
yndaravind
 
pps unit 3.pptx
mathesh0303
 
Week6_P_String.pptx
OluwafolakeOjo
 
14 strings
Rohit Shrivastava
 
THE FORMAT AND USAGE OF STRINGS IN C.PPT
shanthabalaji2013
 
C Programming Strings.docx
8759000398
 
Cfbcgdhfghdfhghggfhghghgfhgfhgfhhapter11.PPT
JITENDER773791
 
Chapterabcdefghijklmnopqrdstuvwxydanniipo
abritip
 
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Lecture 05 2017
Jesmin Akhter
 
Handling of character strings C programming
Appili Vamsi Krishna
 
Principals of Programming in CModule -5.pdfModule-4.pdf
anilcsbs
 
String_Slide.pptxStructure.pptxStructure.pptxStructure.pptxStructure.pptx
monimhossain14
 
C programming - String
Achyut Devkota
 
Unitii string
Sowri Rajan
 
Ad

More from Prasadu Peddi (17)

PDF
Pointers
Prasadu Peddi
 
DOCX
B.Com 1year Lab programs
Prasadu Peddi
 
DOCX
COMPUTING SEMANTIC SIMILARITY OF CONCEPTS IN KNOWLEDGE GRAPHS
Prasadu Peddi
 
DOCX
Energy-efficient Query Processing in Web Search Engines
Prasadu Peddi
 
DOCX
MINING COMPETITORS FROM LARGE UNSTRUCTURED DATASETS
Prasadu Peddi
 
DOCX
GENERATING QUERY FACETS USING KNOWLEDGE BASES
Prasadu Peddi
 
DOCX
UNDERSTAND SHORTTEXTS BY HARVESTING & ANALYZING SEMANTIKNOWLEDGE
Prasadu Peddi
 
DOCX
SOCIRANK: IDENTIFYING AND RANKING PREVALENT NEWS TOPICS USING SOCIAL MEDIA FA...
Prasadu Peddi
 
DOCX
QUERY EXPANSION WITH ENRICHED USER PROFILES FOR PERSONALIZED SEARCH UTILIZING...
Prasadu Peddi
 
DOCX
COLLABORATIVE FILTERING-BASED RECOMMENDATION OF ONLINE SOCIAL VOTING
Prasadu Peddi
 
DOCX
DYNAMIC FACET ORDERING FOR FACETED PRODUCT SEARCH ENGINES
Prasadu Peddi
 
PPTX
A Cross Tenant Access Control (CTAC) Model for Cloud Computing: Formal Specif...
Prasadu Peddi
 
PPTX
Time and Attribute Factors Combined Access Control on Time-Sensitive Data in ...
Prasadu Peddi
 
PPTX
Attribute Based Storage Supporting Secure Deduplication of Encrypted D...
Prasadu Peddi
 
PPTX
RAAC: Robust and Auditable Access Control with Multiple Attribute Authorities...
Prasadu Peddi
 
PPTX
Provably Secure Key-Aggregate Cryptosystems with Broadcast Aggregate Keys for...
Prasadu Peddi
 
PPTX
Identity-Based Remote Data Integrity Checking With Perfect Data Privacy Prese...
Prasadu Peddi
 
Pointers
Prasadu Peddi
 
B.Com 1year Lab programs
Prasadu Peddi
 
COMPUTING SEMANTIC SIMILARITY OF CONCEPTS IN KNOWLEDGE GRAPHS
Prasadu Peddi
 
Energy-efficient Query Processing in Web Search Engines
Prasadu Peddi
 
MINING COMPETITORS FROM LARGE UNSTRUCTURED DATASETS
Prasadu Peddi
 
GENERATING QUERY FACETS USING KNOWLEDGE BASES
Prasadu Peddi
 
UNDERSTAND SHORTTEXTS BY HARVESTING & ANALYZING SEMANTIKNOWLEDGE
Prasadu Peddi
 
SOCIRANK: IDENTIFYING AND RANKING PREVALENT NEWS TOPICS USING SOCIAL MEDIA FA...
Prasadu Peddi
 
QUERY EXPANSION WITH ENRICHED USER PROFILES FOR PERSONALIZED SEARCH UTILIZING...
Prasadu Peddi
 
COLLABORATIVE FILTERING-BASED RECOMMENDATION OF ONLINE SOCIAL VOTING
Prasadu Peddi
 
DYNAMIC FACET ORDERING FOR FACETED PRODUCT SEARCH ENGINES
Prasadu Peddi
 
A Cross Tenant Access Control (CTAC) Model for Cloud Computing: Formal Specif...
Prasadu Peddi
 
Time and Attribute Factors Combined Access Control on Time-Sensitive Data in ...
Prasadu Peddi
 
Attribute Based Storage Supporting Secure Deduplication of Encrypted D...
Prasadu Peddi
 
RAAC: Robust and Auditable Access Control with Multiple Attribute Authorities...
Prasadu Peddi
 
Provably Secure Key-Aggregate Cryptosystems with Broadcast Aggregate Keys for...
Prasadu Peddi
 
Identity-Based Remote Data Integrity Checking With Perfect Data Privacy Prese...
Prasadu Peddi
 
Ad

Recently uploaded (20)

PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PPTX
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPTX
GRADE-3-PPT-EVE-2025-ENG-Q1-LESSON-1.pptx
EveOdrapngimapNarido
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
GRADE-3-PPT-EVE-2025-ENG-Q1-LESSON-1.pptx
EveOdrapngimapNarido
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
Horarios de distribución de agua en julio
pegazohn1978
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
Dimensions of Societal Planning in Commonism
StefanMz
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 

String notes

  • 1. STRINGS WHAT IS STRING The string can be defined as the one-dimensional array of characters terminated by a null ('0'). This null character indicates the end of the string. Each character in the array occupies one byte of memory, and the last character must always be 0. The termination character ('0') is important in a string since it is the only way to identify where the string ends. Strings are always enclosed by double quotes. Whereas, character is enclosed by single quotes in C. Example: ‘Kedia’, “Kedia” DECLARING AND INITIALIZING STRING There are two ways to declare and initializing a string in c language. 1. By char array 2. By string literal Declaring String Syntax: Datatype string_variable_name [array_Size]; Example: char s[5]; When we declare string array with size 5 then it will create sequential memory. As we know, array index starts from 0, so it will be represented as in the figure given below. Initializing String: Initializing means while declaring char array if we assign elements or literals. Syntax: Datatype string_variable_name [array_size] ={characters}; Example: char s[5]= {‘K’,’E’,’D’,’I’,’A’,’0’}; Prasadu peddi R.G. Kedia College of commerce
  • 2. What is NULL Char “0”? '0' represents the end of the string. It is also referred as String terminator & Null Character. Declaring String by string Literal: char ch[]="RGKEDIA"; Difference between char array and string literal: There is main difference between char array and String literal. We need to add the null character '0' at the end of the array by ourself whereas, it is appended internally by the compiler in the case of the string literal. String Example in C Let's see a simple example where a string is declared and being printed. The '%s' is used as a format specifier for the string in c language. #include<stdio.h> #include <string.h> Void main() { char str1[8]={'R', 'G', 'K', 'E', 'D', 'I', 'A', '0'}; char str2[8] ="RGKEDIA"; printf("Char Array Value is: %sn", str1); printf("String Literal Value is: %sn", str2); } READING AND WRITING STRINGS Accepting string as the input: Till now, we have used scanf to accept the input from the user. However, it can also be used in the case of strings but with a different scenario. The '%s' is used as a format specifier for the string in c language. In Scanf() for reading integer and float values format specifier %d, %f and “&” required where as for reading string & not required. #include<stdio.h> void main () { Prasadu peddi R.G. Kedia College of commerce
  • 3. char s[20]; printf("Enter the string?"); scanf("%s",s); printf("You entered %s",s); } Output1: Enter the string? Corona You entered corona Output2: Enter the string? Corona is a virus You entered corona Note: in output1, input string is ‘corona’ and it is displayed as it is where as in output2, input is ‘corona is a virus’ but output is corona. It is clear from the output2 that, the Scanf will not work for space separated strings. The problem with the scanf function is that it never reads an entire string. It will halt the reading process as soon as whitespace, suppose we give input as "B.COM First Year" then the scanf function will never read an entire string as a whitespace character occurs between the two names. The scanf function will only read B.COM and it ignores remaining strings. gets() Function: The gets() function is similar to scanf() function. In order to read a string contains spaces, we use the gets() function. Gets ignores the whitespaces. It stops reading when a newline is reached (the Enter key is pressed). The gets() function enables the user to enter some characters followed by the enter key. All the characters entered by the user get stored in a character array. The null character is added to the array to make it a string. The gets() allows the user to enter the space-separated strings. It returns the string entered by the user. Reading string using gets(): #include<stdio.h> Prasadu peddi R.G. Kedia College of commerce
  • 4. void main () { char s[30]; printf("Enter the string? "); gets(s); printf("You entered %s",s); } OUTPUT Enter the string? RG Kedia College You entered RG Kedia College puts() function The puts() function is very much similar to printf() function. The puts() function is used to print the string on the console which is previously read by using gets() or scanf() function. The puts() function returns an integer value representing the number of characters being printed on the console. The difference between printf and puts is printf display integer, float, and char values where as puts used for only to display string. #include<stdio.h> #include <string.h> void main() { char name[50]; printf("Enter string: "); gets(name); //reads string from user printf("Your string is: "); puts(name); //displays string } OUTPUT Enter string: Corona virus disease also called COVID Your string is: Corona virus disease also called COVID Prasadu peddi R.G. Kedia College of commerce
  • 5. STRING STANDARD FUNCTIONS 1) strlen(string_name) function is used to find the length of a character string. It doesn't count null character '0'. The strlen() function takes a string as an argument and returns its length. The returned value is of type int. It is defined in the <string.h> header file. Example: int n; char st[20] = “studnet”; n = strlen(st); #include<stdio.h> #include <string.h> int main() { Int len; char ch[20]=”student”; len=strlen(ch); printf("Length of string is: %d",len); return 0; } 2) strcpy(destination, source) copies the contents of source string to destination string. The strcpy() function copies the string to the another character array. The strcpy() function is defined in the string.h header file. strcpy ( str1, str2) – It copies contents of str2 into str1. strcpy ( str2, str1) – It copies contents of str1 into str2. #include<stdio.h> #include <string.h> int main() { Prasadu peddi R.G. Kedia College of commerce
  • 6. char cp1[20]={'s’,’t’,’u’,’d’,’e’,’n’,’t’,’0’}; char cp2[20]; strcpy(cp2,cp1); puts(cp2); return 0; } 3) strcat(first_string, second_string) concats or joins first string with second string. The result of the string is stored in first string. It takes two arguments, i.e, two strings or character arrays, and stores the resultant concatenated string in the first string specified in the argument. The strcat() function is defined in <string.h> header file. #include <stdio.h> #include <string.h> int main() { char str1[] = "This is ", str2[] = "online class"; //concatenates str1 and str2 and resultant string is stored in str1. strcat(str1,str2); puts(str1); puts(str2); return 0; } output This is online class Online class Prasadu peddi R.G. Kedia College of commerce
  • 7. 4) strcmp(first_string, second_string) compares the first string with second string. If both strings are same, it returns 0. strcmp( ) function is case sensitive. i.e., “A” and “a” are treated as different characters. If length of string1 < string2, it returns < 0 value. If length of string1 > string2, it returns > 0 value. It is defined in the string.h header file. int n; n = strcmp(city, town); #include <stdio.h> #include <string.h> int main() { char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd"; int result; // comparing strings str1 and str2 result = strcmp(str1, str2); printf("strcmp(str1, str2) = %dn", result); // comparing strings str1 and str3 result = strcmp(str1, str3); printf("strcmp(str1, str3) = %dn", result); return 0; } OUTPUT strcmp(str1, str2) = 32 strcmp(str1, str3) = 0 Prasadu peddi R.G. Kedia College of commerce
  • 8. 5) strrev(string) returns reverse string. This function returns reverse of the given string. #include<stdio.h> #include <string.h> int main(){ char str[20]; printf("Enter string: "); gets(str);//reads string from console puts(str); printf("Reverse String is: %s",strrev(str)); return 0; } OUTPUT Enter string: student String is: student Reverse String is: tneduts 6) strlwr(string) returns string characters in lowercase. 7) strupr(string)returns string characters in uppercase. Prasadu peddi R.G. Kedia College of commerce