SlideShare a Scribd company logo
1 of 4
STRING
A string in C is actually a character array. As an individual character variable can store
only one character, we need an array of characters to store strings. Thus, in C string is
stored in an array of characters. Each character in a string occupies one location in an
array. The null character ‘0’ is put after the last character. This is done so that program
can tell when the end of the string has been reached. The string in C programming
language is actually a one-dimensional array of characters which is terminated by a null
character '0'. Thus a null-terminated string contains the characters that comprise the
string followed by a null.
The following declaration and initialization create a string consisting of the word "Hello".
To hold the null character at the end of the array, the size of the character array
containing the string is one more than the number of characters in the word "Hello."
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '0'};

If you follow the rule of array initialization then you can write the above statement as
follows:
char greeting[] = "Hello";

Following is the memory presentation of above defined string in C.

Actually, you do not place the null character at the end of a string constant. The C
compiler automatically places the '0' at the end of the string when it initializes the array.
Let us try to print above mentioned string:
#include <stdio.h>
void main ()
{
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '0'};
printf("Greeting message: %sn", greeting );
}

getch();

In C programming, array of character are called strings. A string is terminated by null
character /0. For example:
"c string tutorial"

Here, "c string tutorial" is a string. When, compiler encounters strings, it appends null
character at the end of string.

Declaration of strings
Strings are declared in C in similar manner as arrays. Only difference is that, strings are
of char type.
char s[5];
Initialization of strings
In C, string can be initialized in different number of ways.
char c[]="abcd";
OR,
char c[5]="abcd";
OR,
char c[]={'a','b','c','d','0'};
OR;
char c[5]={'a','b','c','d','0'};

Reading words from user.
char c[20];
scanf("%s",c);

String variable c can only take a word. It is beacause when white space is encountered,
the scanf() function terminates.
Write a C program to illustrate how to read string from terminal.
#include <stdio.h>
void main(){
char name[20];
printf("Enter name: ");
scanf("%s",name);
printf("Your name is %s.",name);
getch();
}

Output
Enter name: sunil kumar
Your name is sunil.

Here, program will ignore kumar because, scanf() function takes only string before the
white space.
This process to take string is tedious. There are predefined functions gets() and puts in
C language to read and display string respectively.
#include <stdio.h>
void main(){
char name[30];
printf("Enter name: ");
gets(name);
//Function to read string from user.
printf("Name: ");
puts(name);
//Function to display string.
getch()
}

String handling functions
You can perform different type of string operations manually like: finding length of
string, concatenating(joining) two strings etc. But, for programmers ease, many library
function are defined under header file <string.h> to handle these commonly used talk
in C programming.

strlen()
In C, strlen() function calculates the length of string. It is defined under "string.h" header
file.
It takes only one argument, i.e, string name.
Syntax of strlen()
temp_variable = strlen(string_name);

Function strlen() returns the value of type integer.

Example of strlen()
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[10];
int len;
clrscr();
printf("Enter your String not more than 10 character::");
gets(name);
len=strlen(name);
printf("The Length of String is %d", len);
getch();
}
#include<stdio.h>
#include<conio.h>

#include <string.h>

void main()
{
char name[30]= "hello wass up";
clrscr();
printf("nString is %s",name);
printf("The length of string id %d",strlen(name));
getch();
}

strcpy()
Function strcpy() copies the content of one string to the content of another string. It is
defined under "string.h" header file.
It takes two arguments.

Syntax of strcpy()
strcpy(destination,source);

Here, source and destination are both the name of the string. This statement, copies the
content of string source to the content of string destination.

Example of strcpy()
#include <stdio.h>
#include <string.h>
void main(){
char a[10],b[10];
printf("Enter string: ");
gets(a);
strcpy(b,a);
//Content of string a is copied to string b.
printf("Copied string: ");
puts(b);
getch();
}
Output
Enter string: sunil kumar
Copied string: sunil kumar

strcat()
In C programming, strcat() concatenates(joins) two strings.
It takes two arguments, i.e, two strings and resultant string is stored in the first string
specified in the argument.
Function strcat() is defined under "string.h" header file.

Syntax of strcat()
strcat(first_string,second_string);

Example of strcat()
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[10],name1[10];
clrscr();
printf("Enter the First string");
gets(name);
printf("Enter the Second string");
gets(name1);
strcat(name,name1);
printf("The string after concatenations is %sn",name);
getch();
}

Example of strrev ()
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[10];
printf("Enter the String");
gets(name);
strrev(name);
printf("The String after reverse isn%s",name);
getch();
}

Example of strcmp ()
#include <stdio.h>
#include <string.h>
#include<conio.h>
void main()
{
char *str1 = "sample", *str2 = "sample";
clrscr();
if(strcmp(str1,str2)==0)
printf("strings are equal");
else
printf("strings are not equal");
getch();
}

More Related Content

What's hot

structure and union
structure and unionstructure and union
structure and union
student
 

What's hot (20)

C string
C stringC string
C string
 
String in c programming
String in c programmingString in c programming
String in c programming
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
String C Programming
String C ProgrammingString C Programming
String C Programming
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
 
Array and string
Array and stringArray and string
Array and string
 
string in C
string in Cstring in C
string in C
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
 
datatypes and variables in c language
 datatypes and variables in c language datatypes and variables in c language
datatypes and variables in c language
 
Strings in C
Strings in CStrings in C
Strings in C
 
structure and union
structure and unionstructure and union
structure and union
 
Basic array in c programming
Basic array in c programmingBasic array in c programming
Basic array in c programming
 
C++ string
C++ stringC++ string
C++ string
 
Managing input and output operation in c
Managing input and output operation in cManaging input and output operation in c
Managing input and output operation in c
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
C Pointers
C PointersC Pointers
C Pointers
 
Function in c
Function in cFunction in c
Function in c
 
Structure in C
Structure in CStructure in C
Structure in C
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 

Viewers also liked

Array in c language
Array in c languageArray in c language
Array in c language
home
 

Viewers also liked (15)

Applications of dielectric material
Applications of dielectric materialApplications of dielectric material
Applications of dielectric material
 
Dielectrics and its applications
Dielectrics and its applicationsDielectrics and its applications
Dielectrics and its applications
 
DIELECTRICS PPT
DIELECTRICS PPTDIELECTRICS PPT
DIELECTRICS PPT
 
Dielectric Material and properties
Dielectric Material and propertiesDielectric Material and properties
Dielectric Material and properties
 
Function in c
Function in cFunction in c
Function in c
 
Loops Basics
Loops BasicsLoops Basics
Loops Basics
 
Structure in C
Structure in CStructure in C
Structure in C
 
Structure in c
Structure in cStructure in c
Structure in c
 
Structure of a C program
Structure of a C programStructure of a C program
Structure of a C program
 
Structure c
Structure cStructure c
Structure c
 
Array in c language
Array in c languageArray in c language
Array in c language
 
Loops in C
Loops in CLoops in C
Loops in C
 
Loops in C Programming
Loops in C ProgrammingLoops in C Programming
Loops in C Programming
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Function in C program
Function in C programFunction in C program
Function in C program
 

Similar to String in c

C Programming Strings.docx
C Programming Strings.docxC Programming Strings.docx
C Programming Strings.docx
8759000398
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptx
JawadTanvir
 

Similar to String in c (20)

C Programming Strings.docx
C Programming Strings.docxC Programming Strings.docx
C Programming Strings.docx
 
14 strings
14 strings14 strings
14 strings
 
Character array (strings)
Character array (strings)Character array (strings)
Character array (strings)
 
Computer Programming- Lecture 5
Computer Programming- Lecture 5 Computer Programming- Lecture 5
Computer Programming- Lecture 5
 
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptxINDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
 
String in programming language in c or c++
String in programming language in c or c++String in programming language in c or c++
String in programming language in c or c++
 
[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
 
String notes
String notesString notes
String notes
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
 
Week6_P_String.pptx
Week6_P_String.pptxWeek6_P_String.pptx
Week6_P_String.pptx
 
COm1407: Character & Strings
COm1407: Character & StringsCOm1407: Character & Strings
COm1407: Character & Strings
 
Unit 2
Unit 2Unit 2
Unit 2
 
fundamentals of c programming_String.pptx
fundamentals of c programming_String.pptxfundamentals of c programming_String.pptx
fundamentals of c programming_String.pptx
 
Lecture 2. mte 407
Lecture 2. mte 407Lecture 2. mte 407
Lecture 2. mte 407
 
C Programming Unit-3
C Programming Unit-3C Programming Unit-3
C Programming Unit-3
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptx
 
Strings in programming tutorial.
Strings  in programming tutorial.Strings  in programming tutorial.
Strings in programming tutorial.
 
Unitii string
Unitii stringUnitii string
Unitii string
 
CP-STRING (1).ppt
CP-STRING (1).pptCP-STRING (1).ppt
CP-STRING (1).ppt
 

More from Suneel Dogra (20)

Business model
Business modelBusiness model
Business model
 
Internet
InternetInternet
Internet
 
Html
HtmlHtml
Html
 
Dreamweaver
DreamweaverDreamweaver
Dreamweaver
 
Advanced html
Advanced htmlAdvanced html
Advanced html
 
Sql
SqlSql
Sql
 
File organisation
File organisationFile organisation
File organisation
 
Distributed databases
Distributed databasesDistributed databases
Distributed databases
 
Database models
Database models Database models
Database models
 
Data base management system
Data base management systemData base management system
Data base management system
 
Web sitedesignpart1
Web sitedesignpart1Web sitedesignpart1
Web sitedesignpart1
 
Web sitedesignpart1
Web sitedesignpart1Web sitedesignpart1
Web sitedesignpart1
 
Internet security
Internet securityInternet security
Internet security
 
What is the linux
What is the linuxWhat is the linux
What is the linux
 
He 12 different types of servers that every techie should know about
He 12 different types of servers that every techie should know aboutHe 12 different types of servers that every techie should know about
He 12 different types of servers that every techie should know about
 
Bachelor of computer application b.c.a.-2014
Bachelor of computer application b.c.a.-2014Bachelor of computer application b.c.a.-2014
Bachelor of computer application b.c.a.-2014
 
Cloud computing application
Cloud computing applicationCloud computing application
Cloud computing application
 
Fast track to linux
Fast track to linuxFast track to linux
Fast track to linux
 
A sorted linear array
A sorted linear array A sorted linear array
A sorted linear array
 
Jumping statements
Jumping statementsJumping statements
Jumping statements
 

Recently uploaded

Desi Bhabhi Call Girls In Goa 💃 730 02 72 001💃desi Bhabhi Escort Goa
Desi Bhabhi Call Girls  In Goa  💃 730 02 72 001💃desi Bhabhi Escort GoaDesi Bhabhi Call Girls  In Goa  💃 730 02 72 001💃desi Bhabhi Escort Goa
Desi Bhabhi Call Girls In Goa 💃 730 02 72 001💃desi Bhabhi Escort Goa
russian goa call girl and escorts service
 
Call Girls Agency In Goa 💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...
Call Girls  Agency In Goa  💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...Call Girls  Agency In Goa  💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...
Call Girls Agency In Goa 💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...
russian goa call girl and escorts service
 

Recently uploaded (20)

Borum Call Girls Service ☎ ️93326-06886 ❤️‍🔥 Enjoy 24/7 Escort Service
Borum Call Girls Service ☎ ️93326-06886 ❤️‍🔥 Enjoy 24/7 Escort ServiceBorum Call Girls Service ☎ ️93326-06886 ❤️‍🔥 Enjoy 24/7 Escort Service
Borum Call Girls Service ☎ ️93326-06886 ❤️‍🔥 Enjoy 24/7 Escort Service
 
Verified Trusted Call Girls Tambaram Chennai ✔✔7427069034 Independent Chenna...
Verified Trusted Call Girls Tambaram Chennai ✔✔7427069034  Independent Chenna...Verified Trusted Call Girls Tambaram Chennai ✔✔7427069034  Independent Chenna...
Verified Trusted Call Girls Tambaram Chennai ✔✔7427069034 Independent Chenna...
 
5* Hotels Call Girls In Goa {{07028418221}} Call Girls In North Goa Escort Se...
5* Hotels Call Girls In Goa {{07028418221}} Call Girls In North Goa Escort Se...5* Hotels Call Girls In Goa {{07028418221}} Call Girls In North Goa Escort Se...
5* Hotels Call Girls In Goa {{07028418221}} Call Girls In North Goa Escort Se...
 
Top Rated Pune Call Girls Pimpri Chinchwad ⟟ 6297143586 ⟟ Call Me For Genuin...
Top Rated  Pune Call Girls Pimpri Chinchwad ⟟ 6297143586 ⟟ Call Me For Genuin...Top Rated  Pune Call Girls Pimpri Chinchwad ⟟ 6297143586 ⟟ Call Me For Genuin...
Top Rated Pune Call Girls Pimpri Chinchwad ⟟ 6297143586 ⟟ Call Me For Genuin...
 
Desi Bhabhi Call Girls In Goa 💃 730 02 72 001💃desi Bhabhi Escort Goa
Desi Bhabhi Call Girls  In Goa  💃 730 02 72 001💃desi Bhabhi Escort GoaDesi Bhabhi Call Girls  In Goa  💃 730 02 72 001💃desi Bhabhi Escort Goa
Desi Bhabhi Call Girls In Goa 💃 730 02 72 001💃desi Bhabhi Escort Goa
 
↑Top Model (Kolkata) Call Girls Rajpur ⟟ 8250192130 ⟟ High Class Call Girl In...
↑Top Model (Kolkata) Call Girls Rajpur ⟟ 8250192130 ⟟ High Class Call Girl In...↑Top Model (Kolkata) Call Girls Rajpur ⟟ 8250192130 ⟟ High Class Call Girl In...
↑Top Model (Kolkata) Call Girls Rajpur ⟟ 8250192130 ⟟ High Class Call Girl In...
 
Hotel And Home Service Available Kolkata Call Girls Sonagachi ✔ 6297143586 ✔C...
Hotel And Home Service Available Kolkata Call Girls Sonagachi ✔ 6297143586 ✔C...Hotel And Home Service Available Kolkata Call Girls Sonagachi ✔ 6297143586 ✔C...
Hotel And Home Service Available Kolkata Call Girls Sonagachi ✔ 6297143586 ✔C...
 
Independent Sonagachi Escorts ✔ 9332606886✔ Full Night With Room Online Booki...
Independent Sonagachi Escorts ✔ 9332606886✔ Full Night With Room Online Booki...Independent Sonagachi Escorts ✔ 9332606886✔ Full Night With Room Online Booki...
Independent Sonagachi Escorts ✔ 9332606886✔ Full Night With Room Online Booki...
 
Call Girls in Barasat | 7001035870 At Low Cost Cash Payment Booking
Call Girls in Barasat | 7001035870 At Low Cost Cash Payment BookingCall Girls in Barasat | 7001035870 At Low Cost Cash Payment Booking
Call Girls in Barasat | 7001035870 At Low Cost Cash Payment Booking
 
Call Girls Agency In Goa 💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...
Call Girls  Agency In Goa  💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...Call Girls  Agency In Goa  💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...
Call Girls Agency In Goa 💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...
 
❤Personal Whatsapp Number Keylong Call Girls 8617697112 💦✅.
❤Personal Whatsapp Number Keylong Call Girls 8617697112 💦✅.❤Personal Whatsapp Number Keylong Call Girls 8617697112 💦✅.
❤Personal Whatsapp Number Keylong Call Girls 8617697112 💦✅.
 
Hotel And Home Service Available Kolkata Call Girls South End Park ✔ 62971435...
Hotel And Home Service Available Kolkata Call Girls South End Park ✔ 62971435...Hotel And Home Service Available Kolkata Call Girls South End Park ✔ 62971435...
Hotel And Home Service Available Kolkata Call Girls South End Park ✔ 62971435...
 
Behala ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Ready ...
Behala ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Ready ...Behala ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Ready ...
Behala ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Ready ...
 
Top Rated Pune Call Girls Dhayari ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...
Top Rated  Pune Call Girls Dhayari ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...Top Rated  Pune Call Girls Dhayari ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...
Top Rated Pune Call Girls Dhayari ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...
 
Almora call girls 📞 8617697112 At Low Cost Cash Payment Booking
Almora call girls 📞 8617697112 At Low Cost Cash Payment BookingAlmora call girls 📞 8617697112 At Low Cost Cash Payment Booking
Almora call girls 📞 8617697112 At Low Cost Cash Payment Booking
 
Model Call Girls In Velappanchavadi WhatsApp Booking 7427069034 call girl ser...
Model Call Girls In Velappanchavadi WhatsApp Booking 7427069034 call girl ser...Model Call Girls In Velappanchavadi WhatsApp Booking 7427069034 call girl ser...
Model Call Girls In Velappanchavadi WhatsApp Booking 7427069034 call girl ser...
 
Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
 
Independent Joka Escorts ✔ 8250192130 ✔ Full Night With Room Online Booking 2...
Independent Joka Escorts ✔ 8250192130 ✔ Full Night With Room Online Booking 2...Independent Joka Escorts ✔ 8250192130 ✔ Full Night With Room Online Booking 2...
Independent Joka Escorts ✔ 8250192130 ✔ Full Night With Room Online Booking 2...
 
📞 Contact Number 8617697112 VIP Ganderbal Call Girls
📞 Contact Number 8617697112 VIP Ganderbal Call Girls📞 Contact Number 8617697112 VIP Ganderbal Call Girls
📞 Contact Number 8617697112 VIP Ganderbal Call Girls
 
College Call Girls Pune 8617697112 Short 1500 Night 6000 Best call girls Service
College Call Girls Pune 8617697112 Short 1500 Night 6000 Best call girls ServiceCollege Call Girls Pune 8617697112 Short 1500 Night 6000 Best call girls Service
College Call Girls Pune 8617697112 Short 1500 Night 6000 Best call girls Service
 

String in c

  • 1. STRING A string in C is actually a character array. As an individual character variable can store only one character, we need an array of characters to store strings. Thus, in C string is stored in an array of characters. Each character in a string occupies one location in an array. The null character ‘0’ is put after the last character. This is done so that program can tell when the end of the string has been reached. The string in C programming language is actually a one-dimensional array of characters which is terminated by a null character '0'. Thus a null-terminated string contains the characters that comprise the string followed by a null. The following declaration and initialization create a string consisting of the word "Hello". To hold the null character at the end of the array, the size of the character array containing the string is one more than the number of characters in the word "Hello." char greeting[6] = {'H', 'e', 'l', 'l', 'o', '0'}; If you follow the rule of array initialization then you can write the above statement as follows: char greeting[] = "Hello"; Following is the memory presentation of above defined string in C. Actually, you do not place the null character at the end of a string constant. The C compiler automatically places the '0' at the end of the string when it initializes the array. Let us try to print above mentioned string: #include <stdio.h> void main () { char greeting[6] = {'H', 'e', 'l', 'l', 'o', '0'}; printf("Greeting message: %sn", greeting ); } getch(); In C programming, array of character are called strings. A string is terminated by null character /0. For example: "c string tutorial" Here, "c string tutorial" is a string. When, compiler encounters strings, it appends null character at the end of string. Declaration of strings Strings are declared in C in similar manner as arrays. Only difference is that, strings are of char type. char s[5];
  • 2. Initialization of strings In C, string can be initialized in different number of ways. char c[]="abcd"; OR, char c[5]="abcd"; OR, char c[]={'a','b','c','d','0'}; OR; char c[5]={'a','b','c','d','0'}; Reading words from user. char c[20]; scanf("%s",c); String variable c can only take a word. It is beacause when white space is encountered, the scanf() function terminates. Write a C program to illustrate how to read string from terminal. #include <stdio.h> void main(){ char name[20]; printf("Enter name: "); scanf("%s",name); printf("Your name is %s.",name); getch(); } Output Enter name: sunil kumar Your name is sunil. Here, program will ignore kumar because, scanf() function takes only string before the white space. This process to take string is tedious. There are predefined functions gets() and puts in C language to read and display string respectively. #include <stdio.h> void main(){ char name[30]; printf("Enter name: "); gets(name); //Function to read string from user. printf("Name: "); puts(name); //Function to display string. getch() } String handling functions You can perform different type of string operations manually like: finding length of string, concatenating(joining) two strings etc. But, for programmers ease, many library function are defined under header file <string.h> to handle these commonly used talk in C programming. strlen() In C, strlen() function calculates the length of string. It is defined under "string.h" header file. It takes only one argument, i.e, string name.
  • 3. Syntax of strlen() temp_variable = strlen(string_name); Function strlen() returns the value of type integer. Example of strlen() #include<stdio.h> #include<conio.h> #include<string.h> void main() { char name[10]; int len; clrscr(); printf("Enter your String not more than 10 character::"); gets(name); len=strlen(name); printf("The Length of String is %d", len); getch(); } #include<stdio.h> #include<conio.h> #include <string.h> void main() { char name[30]= "hello wass up"; clrscr(); printf("nString is %s",name); printf("The length of string id %d",strlen(name)); getch(); } strcpy() Function strcpy() copies the content of one string to the content of another string. It is defined under "string.h" header file. It takes two arguments. Syntax of strcpy() strcpy(destination,source); Here, source and destination are both the name of the string. This statement, copies the content of string source to the content of string destination. Example of strcpy() #include <stdio.h> #include <string.h> void main(){ char a[10],b[10]; printf("Enter string: "); gets(a); strcpy(b,a); //Content of string a is copied to string b. printf("Copied string: "); puts(b); getch(); }
  • 4. Output Enter string: sunil kumar Copied string: sunil kumar strcat() In C programming, strcat() concatenates(joins) two strings. It takes two arguments, i.e, two strings and resultant string is stored in the first string specified in the argument. Function strcat() is defined under "string.h" header file. Syntax of strcat() strcat(first_string,second_string); Example of strcat() #include<stdio.h> #include<conio.h> #include<string.h> void main() { char name[10],name1[10]; clrscr(); printf("Enter the First string"); gets(name); printf("Enter the Second string"); gets(name1); strcat(name,name1); printf("The string after concatenations is %sn",name); getch(); } Example of strrev () #include<stdio.h> #include<conio.h> #include<string.h> void main() { char name[10]; printf("Enter the String"); gets(name); strrev(name); printf("The String after reverse isn%s",name); getch(); } Example of strcmp () #include <stdio.h> #include <string.h> #include<conio.h> void main() { char *str1 = "sample", *str2 = "sample"; clrscr(); if(strcmp(str1,str2)==0) printf("strings are equal"); else printf("strings are not equal"); getch(); }