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

Function in c
Function in cFunction in c
Function in c
Raj Tandukar
ย 
Structure of a C program
Structure of a C programStructure of a C program
Structure of a C program
David Livingston J
ย 
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
ย 
Unitii string
Unitii stringUnitii string
Unitii string
Sowri Rajan
ย 

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

Dreamweaver
DreamweaverDreamweaver
Dreamweaver
Suneel Dogra
ย 
Advanced html
Advanced htmlAdvanced html
Advanced html
Suneel Dogra
ย 
File organisation
File organisationFile organisation
File organisation
Suneel Dogra
ย 
Distributed databases
Distributed databasesDistributed databases
Distributed databases
Suneel Dogra
ย 
Database models
Database models Database models
Database models
Suneel Dogra
ย 
Data base management system
Data base management systemData base management system
Data base management system
Suneel Dogra
ย 
Web sitedesignpart1
Web sitedesignpart1Web sitedesignpart1
Web sitedesignpart1
Suneel Dogra
ย 
Web sitedesignpart1
Web sitedesignpart1Web sitedesignpart1
Web sitedesignpart1
Suneel Dogra
ย 
What is the linux
What is the linuxWhat is the linux
What is the linux
Suneel Dogra
ย 
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
Suneel Dogra
ย 
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
Suneel Dogra
ย 
Jumping statements
Jumping statementsJumping statements
Jumping statements
Suneel Dogra
ย 

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

Call Girls In Goa 9316020077 Goa Call Girl By Indian Call Girls Goa
Call Girls In Goa  9316020077 Goa  Call Girl By Indian Call Girls GoaCall Girls In Goa  9316020077 Goa  Call Girl By Indian Call Girls Goa
Call Girls In Goa 9316020077 Goa Call Girl By Indian Call Girls Goa
sexy call girls service in goa
ย 
โ†‘Top Model (Kolkata) Call Girls Behala โŸŸ 8250192130 โŸŸ High Class Call Girl In...
โ†‘Top Model (Kolkata) Call Girls Behala โŸŸ 8250192130 โŸŸ High Class Call Girl In...โ†‘Top Model (Kolkata) Call Girls Behala โŸŸ 8250192130 โŸŸ High Class Call Girl In...
โ†‘Top Model (Kolkata) Call Girls Behala โŸŸ 8250192130 โŸŸ High Class Call Girl In...
noor ahmed
ย 
Model Call Girls In Ariyalur WhatsApp Booking 7427069034 call girl service 24...
Model Call Girls In Ariyalur WhatsApp Booking 7427069034 call girl service 24...Model Call Girls In Ariyalur WhatsApp Booking 7427069034 call girl service 24...
Model Call Girls In Ariyalur WhatsApp Booking 7427069034 call girl service 24...
Shivani Pandey
ย 
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...
noor ahmed
ย 
VIP Model Call Girls Koregaon Park ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Koregaon Park ( Pune ) Call ON 8005736733 Starting From ...VIP Model Call Girls Koregaon Park ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Koregaon Park ( Pune ) Call ON 8005736733 Starting From ...
SUHANI PANDEY
ย 
โ†‘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...
noor ahmed
ย 
Independent Garulia Escorts โœ” 9332606886โœ” Full Night With Room Online Booking...
Independent Garulia Escorts โœ” 9332606886โœ” Full Night With Room Online Booking...Independent Garulia Escorts โœ” 9332606886โœ” Full Night With Room Online Booking...
Independent Garulia Escorts โœ” 9332606886โœ” Full Night With Room Online Booking...
Riya Pathan
ย 
VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...
VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...
VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...
SUHANI PANDEY
ย 
Karnal Call Girls 8860008073 Dyal Singh Colony Call Girls Service in Karnal E...
Karnal Call Girls 8860008073 Dyal Singh Colony Call Girls Service in Karnal E...Karnal Call Girls 8860008073 Dyal Singh Colony Call Girls Service in Karnal E...
Karnal Call Girls 8860008073 Dyal Singh Colony Call Girls Service in Karnal E...
Apsara Of India
ย 
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...
ritikasharma
ย 
Nayabad Call Girls โœ” 8005736733 โœ” Hot Model With Sexy Bhabi Ready For Sex At ...
Nayabad Call Girls โœ” 8005736733 โœ” Hot Model With Sexy Bhabi Ready For Sex At ...Nayabad Call Girls โœ” 8005736733 โœ” Hot Model With Sexy Bhabi Ready For Sex At ...
Nayabad Call Girls โœ” 8005736733 โœ” Hot Model With Sexy Bhabi Ready For Sex At ...
aamir
ย 
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...
rahim quresi
ย 
Science City Kolkata ( Call Girls ) Kolkata โœ” 6297143586 โœ” Hot Model With Sex...
Science City Kolkata ( Call Girls ) Kolkata โœ” 6297143586 โœ” Hot Model With Sex...Science City Kolkata ( Call Girls ) Kolkata โœ” 6297143586 โœ” Hot Model With Sex...
Science City Kolkata ( Call Girls ) Kolkata โœ” 6297143586 โœ” Hot Model With Sex...
rahim quresi
ย 

Recently uploaded (20)

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
ย 
Call Girls In Goa 9316020077 Goa Call Girl By Indian Call Girls Goa
Call Girls In Goa  9316020077 Goa  Call Girl By Indian Call Girls GoaCall Girls In Goa  9316020077 Goa  Call Girl By Indian Call Girls Goa
Call Girls In Goa 9316020077 Goa Call Girl By Indian Call Girls Goa
ย 
โ†‘Top Model (Kolkata) Call Girls Behala โŸŸ 8250192130 โŸŸ High Class Call Girl In...
โ†‘Top Model (Kolkata) Call Girls Behala โŸŸ 8250192130 โŸŸ High Class Call Girl In...โ†‘Top Model (Kolkata) Call Girls Behala โŸŸ 8250192130 โŸŸ High Class Call Girl In...
โ†‘Top Model (Kolkata) Call Girls Behala โŸŸ 8250192130 โŸŸ High Class Call Girl In...
ย 
Model Call Girls In Ariyalur WhatsApp Booking 7427069034 call girl service 24...
Model Call Girls In Ariyalur WhatsApp Booking 7427069034 call girl service 24...Model Call Girls In Ariyalur WhatsApp Booking 7427069034 call girl service 24...
Model Call Girls In Ariyalur WhatsApp Booking 7427069034 call girl service 24...
ย 
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...
ย 
VIP Model Call Girls Koregaon Park ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Koregaon Park ( Pune ) Call ON 8005736733 Starting From ...VIP Model Call Girls Koregaon Park ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Koregaon Park ( Pune ) Call ON 8005736733 Starting From ...
ย 
Bhimtal โคCALL GIRL 8617697112 โคCALL GIRLS IN Bhimtal ESCORT SERVICEโคCALL GIRL
Bhimtal โคCALL GIRL 8617697112 โคCALL GIRLS IN Bhimtal ESCORT SERVICEโคCALL GIRLBhimtal โคCALL GIRL 8617697112 โคCALL GIRLS IN Bhimtal ESCORT SERVICEโคCALL GIRL
Bhimtal โคCALL GIRL 8617697112 โคCALL GIRLS IN Bhimtal ESCORT SERVICEโคCALL GIRL
ย 
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
ย 
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
ย 
โ†‘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...
ย 
Independent Garulia Escorts โœ” 9332606886โœ” Full Night With Room Online Booking...
Independent Garulia Escorts โœ” 9332606886โœ” Full Night With Room Online Booking...Independent Garulia Escorts โœ” 9332606886โœ” Full Night With Room Online Booking...
Independent Garulia Escorts โœ” 9332606886โœ” Full Night With Room Online Booking...
ย 
VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...
VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...
VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...
ย 
Call Girls Manjri Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Manjri Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Manjri Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Manjri Call Me 7737669865 Budget Friendly No Advance Booking
ย 
Karnal Call Girls 8860008073 Dyal Singh Colony Call Girls Service in Karnal E...
Karnal Call Girls 8860008073 Dyal Singh Colony Call Girls Service in Karnal E...Karnal Call Girls 8860008073 Dyal Singh Colony Call Girls Service in Karnal E...
Karnal Call Girls 8860008073 Dyal Singh Colony Call Girls Service in Karnal E...
ย 
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...
ย 
Kanpur call girls ๐Ÿ“ž 8617697112 At Low Cost Cash Payment Booking
Kanpur call girls ๐Ÿ“ž 8617697112 At Low Cost Cash Payment BookingKanpur call girls ๐Ÿ“ž 8617697112 At Low Cost Cash Payment Booking
Kanpur call girls ๐Ÿ“ž 8617697112 At Low Cost Cash Payment Booking
ย 
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...
ย 
Nayabad Call Girls โœ” 8005736733 โœ” Hot Model With Sexy Bhabi Ready For Sex At ...
Nayabad Call Girls โœ” 8005736733 โœ” Hot Model With Sexy Bhabi Ready For Sex At ...Nayabad Call Girls โœ” 8005736733 โœ” Hot Model With Sexy Bhabi Ready For Sex At ...
Nayabad Call Girls โœ” 8005736733 โœ” Hot Model With Sexy Bhabi Ready For Sex At ...
ย 
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...
ย 
Science City Kolkata ( Call Girls ) Kolkata โœ” 6297143586 โœ” Hot Model With Sex...
Science City Kolkata ( Call Girls ) Kolkata โœ” 6297143586 โœ” Hot Model With Sex...Science City Kolkata ( Call Girls ) Kolkata โœ” 6297143586 โœ” Hot Model With Sex...
Science City Kolkata ( Call Girls ) Kolkata โœ” 6297143586 โœ” Hot Model With Sex...
ย 

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(); }