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 (20)

Functions in c
Functions in cFunctions in c
Functions in c
 
Strings
StringsStrings
Strings
 
String C Programming
String C ProgrammingString C Programming
String C Programming
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
Datatypes in c
Datatypes in cDatatypes in c
Datatypes in c
 
Strings in C
Strings in CStrings in C
Strings in C
 
C string
C stringC string
C string
 
Structure & union
Structure & unionStructure & union
Structure & union
 
String functions in C
String functions in CString functions in C
String functions in C
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Union in c language
Union  in c languageUnion  in c language
Union in c language
 
C Pointers
C PointersC Pointers
C Pointers
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdfMANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
 
File in C language
File in C languageFile in C language
File in C language
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
 
Structure in C
Structure in CStructure in C
Structure in C
 
Presentation on c structures
Presentation on c   structures Presentation on c   structures
Presentation on c structures
 

Viewers also liked (16)

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
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
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.docx8759000398
 
Character array (strings)
Character array (strings)Character array (strings)
Character array (strings)sangrampatil81
 
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.pptxAbhimanyuChaure
 
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++Azeemaj101
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programmingmikeymanjiro2090
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11Rumman Ansari
 
fundamentals of c programming_String.pptx
fundamentals of c programming_String.pptxfundamentals of c programming_String.pptx
fundamentals of c programming_String.pptxJStalinAsstProfessor
 
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.pptxJawadTanvir
 
Strings in programming tutorial.
Strings  in programming tutorial.Strings  in programming tutorial.
Strings in programming tutorial.Samsil Arefin
 

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

Distributed databases
Distributed databasesDistributed databases
Distributed databasesSuneel Dogra
 
Data base management system
Data base management systemData base management system
Data base management systemSuneel Dogra
 
Web sitedesignpart1
Web sitedesignpart1Web sitedesignpart1
Web sitedesignpart1Suneel Dogra
 
Web sitedesignpart1
Web sitedesignpart1Web sitedesignpart1
Web sitedesignpart1Suneel 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 aboutSuneel 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.-2014Suneel Dogra
 
Cloud computing application
Cloud computing applicationCloud computing application
Cloud computing applicationSuneel Dogra
 
Fast track to linux
Fast track to linuxFast track to linux
Fast track to linuxSuneel Dogra
 
A sorted linear array
A sorted linear array A sorted linear array
A sorted linear array Suneel Dogra
 
Jumping statements
Jumping statementsJumping statements
Jumping statementsSuneel 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 SG Highway 7397865700 Ridhima Hire Me Full Night
Call Girls SG Highway 7397865700 Ridhima Hire Me Full NightCall Girls SG Highway 7397865700 Ridhima Hire Me Full Night
Call Girls SG Highway 7397865700 Ridhima Hire Me Full Nightssuser7cb4ff
 
Hi Class Call Girls In Goa 7028418221 Call Girls In Anjuna Beach Escort Services
Hi Class Call Girls In Goa 7028418221 Call Girls In Anjuna Beach Escort ServicesHi Class Call Girls In Goa 7028418221 Call Girls In Anjuna Beach Escort Services
Hi Class Call Girls In Goa 7028418221 Call Girls In Anjuna Beach Escort ServicesApsara Of India
 
Call Girls Near Delhi Pride Hotel New Delhi 9873777170
Call Girls Near Delhi Pride Hotel New Delhi 9873777170Call Girls Near Delhi Pride Hotel New Delhi 9873777170
Call Girls Near Delhi Pride Hotel New Delhi 9873777170Sonam Pathan
 
Call Girls Somajiguda Sarani 7001305949 all area service COD available Any Time
Call Girls Somajiguda Sarani 7001305949 all area service COD available Any TimeCall Girls Somajiguda Sarani 7001305949 all area service COD available Any Time
Call Girls Somajiguda Sarani 7001305949 all area service COD available Any Timedelhimodelshub1
 
Amil Baba in karachi Kala jadu Expert Amil baba Black magic Specialist in Isl...
Amil Baba in karachi Kala jadu Expert Amil baba Black magic Specialist in Isl...Amil Baba in karachi Kala jadu Expert Amil baba Black magic Specialist in Isl...
Amil Baba in karachi Kala jadu Expert Amil baba Black magic Specialist in Isl...Amil Baba Company
 
Cash Payment Contact:- 7028418221 Goa Call Girls Service North Goa Escorts
Cash Payment Contact:- 7028418221 Goa Call Girls Service North Goa EscortsCash Payment Contact:- 7028418221 Goa Call Girls Service North Goa Escorts
Cash Payment Contact:- 7028418221 Goa Call Girls Service North Goa EscortsApsara Of India
 
Call Girls CG Road 7397865700 Independent Call Girls
Call Girls CG Road 7397865700  Independent Call GirlsCall Girls CG Road 7397865700  Independent Call Girls
Call Girls CG Road 7397865700 Independent Call Girlsssuser7cb4ff
 
ViP Call Girls In Udaipur 9602870969 Gulab Bagh Escorts SeRvIcE
ViP Call Girls In Udaipur 9602870969 Gulab Bagh Escorts SeRvIcEViP Call Girls In Udaipur 9602870969 Gulab Bagh Escorts SeRvIcE
ViP Call Girls In Udaipur 9602870969 Gulab Bagh Escorts SeRvIcEApsara Of India
 
Call Girls Chorasi 7397865700 Ridhima Hire Me Full Night
Call Girls Chorasi 7397865700 Ridhima Hire Me Full NightCall Girls Chorasi 7397865700 Ridhima Hire Me Full Night
Call Girls Chorasi 7397865700 Ridhima Hire Me Full Nightssuser7cb4ff
 
Real NO1 Amil baba in Faisalabad Kala jadu in faisalabad Aamil baba Faisalaba...
Real NO1 Amil baba in Faisalabad Kala jadu in faisalabad Aamil baba Faisalaba...Real NO1 Amil baba in Faisalabad Kala jadu in faisalabad Aamil baba Faisalaba...
Real NO1 Amil baba in Faisalabad Kala jadu in faisalabad Aamil baba Faisalaba...Amil Baba Company
 
定制(UofT毕业证书)加拿大多伦多大学毕业证成绩单原版一比一
定制(UofT毕业证书)加拿大多伦多大学毕业证成绩单原版一比一定制(UofT毕业证书)加拿大多伦多大学毕业证成绩单原版一比一
定制(UofT毕业证书)加拿大多伦多大学毕业证成绩单原版一比一lvtagr7
 
Call Girls in Faridabad 9000000000 Faridabad Escorts Service
Call Girls in Faridabad 9000000000 Faridabad Escorts ServiceCall Girls in Faridabad 9000000000 Faridabad Escorts Service
Call Girls in Faridabad 9000000000 Faridabad Escorts ServiceTina Ji
 
Gripping Adult Web Series You Can't Afford to Miss
Gripping Adult Web Series You Can't Afford to MissGripping Adult Web Series You Can't Afford to Miss
Gripping Adult Web Series You Can't Afford to Missget joys
 
Authentic No 1 Amil Baba In Pakistan Authentic No 1 Amil Baba In Karachi No 1...
Authentic No 1 Amil Baba In Pakistan Authentic No 1 Amil Baba In Karachi No 1...Authentic No 1 Amil Baba In Pakistan Authentic No 1 Amil Baba In Karachi No 1...
Authentic No 1 Amil Baba In Pakistan Authentic No 1 Amil Baba In Karachi No 1...First NO1 World Amil baba in Faisalabad
 
Amil baba in Pakistan amil baba Karachi amil baba in pakistan amil baba in la...
Amil baba in Pakistan amil baba Karachi amil baba in pakistan amil baba in la...Amil baba in Pakistan amil baba Karachi amil baba in pakistan amil baba in la...
Amil baba in Pakistan amil baba Karachi amil baba in pakistan amil baba in la...Amil Baba Company
 
North Avenue Call Girls Services, Hire Now for Full Fun
North Avenue Call Girls Services, Hire Now for Full FunNorth Avenue Call Girls Services, Hire Now for Full Fun
North Avenue Call Girls Services, Hire Now for Full FunKomal Khan
 
Call Girls Ellis Bridge 7397865700 Independent Call Girls
Call Girls Ellis Bridge 7397865700 Independent Call GirlsCall Girls Ellis Bridge 7397865700 Independent Call Girls
Call Girls Ellis Bridge 7397865700 Independent Call Girlsssuser7cb4ff
 
QUIZ BOLLYWOOD ( weekly quiz ) - SJU quizzers
QUIZ BOLLYWOOD ( weekly quiz ) - SJU quizzersQUIZ BOLLYWOOD ( weekly quiz ) - SJU quizzers
QUIZ BOLLYWOOD ( weekly quiz ) - SJU quizzersSJU Quizzers
 
Call US '' 8377087607'' !! Call Girls In Model Town Metro (Delhi NCR)
Call US '' 8377087607'' !! Call Girls In Model Town Metro (Delhi NCR)Call US '' 8377087607'' !! Call Girls In Model Town Metro (Delhi NCR)
Call US '' 8377087607'' !! Call Girls In Model Town Metro (Delhi NCR)dollysharma2066
 

Recently uploaded (20)

Call Girls SG Highway 7397865700 Ridhima Hire Me Full Night
Call Girls SG Highway 7397865700 Ridhima Hire Me Full NightCall Girls SG Highway 7397865700 Ridhima Hire Me Full Night
Call Girls SG Highway 7397865700 Ridhima Hire Me Full Night
 
Hi Class Call Girls In Goa 7028418221 Call Girls In Anjuna Beach Escort Services
Hi Class Call Girls In Goa 7028418221 Call Girls In Anjuna Beach Escort ServicesHi Class Call Girls In Goa 7028418221 Call Girls In Anjuna Beach Escort Services
Hi Class Call Girls In Goa 7028418221 Call Girls In Anjuna Beach Escort Services
 
Call Girls Near Delhi Pride Hotel New Delhi 9873777170
Call Girls Near Delhi Pride Hotel New Delhi 9873777170Call Girls Near Delhi Pride Hotel New Delhi 9873777170
Call Girls Near Delhi Pride Hotel New Delhi 9873777170
 
Call Girls Somajiguda Sarani 7001305949 all area service COD available Any Time
Call Girls Somajiguda Sarani 7001305949 all area service COD available Any TimeCall Girls Somajiguda Sarani 7001305949 all area service COD available Any Time
Call Girls Somajiguda Sarani 7001305949 all area service COD available Any Time
 
Amil Baba in karachi Kala jadu Expert Amil baba Black magic Specialist in Isl...
Amil Baba in karachi Kala jadu Expert Amil baba Black magic Specialist in Isl...Amil Baba in karachi Kala jadu Expert Amil baba Black magic Specialist in Isl...
Amil Baba in karachi Kala jadu Expert Amil baba Black magic Specialist in Isl...
 
Cash Payment Contact:- 7028418221 Goa Call Girls Service North Goa Escorts
Cash Payment Contact:- 7028418221 Goa Call Girls Service North Goa EscortsCash Payment Contact:- 7028418221 Goa Call Girls Service North Goa Escorts
Cash Payment Contact:- 7028418221 Goa Call Girls Service North Goa Escorts
 
Call Girls CG Road 7397865700 Independent Call Girls
Call Girls CG Road 7397865700  Independent Call GirlsCall Girls CG Road 7397865700  Independent Call Girls
Call Girls CG Road 7397865700 Independent Call Girls
 
ViP Call Girls In Udaipur 9602870969 Gulab Bagh Escorts SeRvIcE
ViP Call Girls In Udaipur 9602870969 Gulab Bagh Escorts SeRvIcEViP Call Girls In Udaipur 9602870969 Gulab Bagh Escorts SeRvIcE
ViP Call Girls In Udaipur 9602870969 Gulab Bagh Escorts SeRvIcE
 
Call Girls Chorasi 7397865700 Ridhima Hire Me Full Night
Call Girls Chorasi 7397865700 Ridhima Hire Me Full NightCall Girls Chorasi 7397865700 Ridhima Hire Me Full Night
Call Girls Chorasi 7397865700 Ridhima Hire Me Full Night
 
Real NO1 Amil baba in Faisalabad Kala jadu in faisalabad Aamil baba Faisalaba...
Real NO1 Amil baba in Faisalabad Kala jadu in faisalabad Aamil baba Faisalaba...Real NO1 Amil baba in Faisalabad Kala jadu in faisalabad Aamil baba Faisalaba...
Real NO1 Amil baba in Faisalabad Kala jadu in faisalabad Aamil baba Faisalaba...
 
定制(UofT毕业证书)加拿大多伦多大学毕业证成绩单原版一比一
定制(UofT毕业证书)加拿大多伦多大学毕业证成绩单原版一比一定制(UofT毕业证书)加拿大多伦多大学毕业证成绩单原版一比一
定制(UofT毕业证书)加拿大多伦多大学毕业证成绩单原版一比一
 
Call Girls in Faridabad 9000000000 Faridabad Escorts Service
Call Girls in Faridabad 9000000000 Faridabad Escorts ServiceCall Girls in Faridabad 9000000000 Faridabad Escorts Service
Call Girls in Faridabad 9000000000 Faridabad Escorts Service
 
young call girls in Hari Nagar,🔝 9953056974 🔝 escort Service
young call girls in Hari Nagar,🔝 9953056974 🔝 escort Serviceyoung call girls in Hari Nagar,🔝 9953056974 🔝 escort Service
young call girls in Hari Nagar,🔝 9953056974 🔝 escort Service
 
Gripping Adult Web Series You Can't Afford to Miss
Gripping Adult Web Series You Can't Afford to MissGripping Adult Web Series You Can't Afford to Miss
Gripping Adult Web Series You Can't Afford to Miss
 
Authentic No 1 Amil Baba In Pakistan Authentic No 1 Amil Baba In Karachi No 1...
Authentic No 1 Amil Baba In Pakistan Authentic No 1 Amil Baba In Karachi No 1...Authentic No 1 Amil Baba In Pakistan Authentic No 1 Amil Baba In Karachi No 1...
Authentic No 1 Amil Baba In Pakistan Authentic No 1 Amil Baba In Karachi No 1...
 
Amil baba in Pakistan amil baba Karachi amil baba in pakistan amil baba in la...
Amil baba in Pakistan amil baba Karachi amil baba in pakistan amil baba in la...Amil baba in Pakistan amil baba Karachi amil baba in pakistan amil baba in la...
Amil baba in Pakistan amil baba Karachi amil baba in pakistan amil baba in la...
 
North Avenue Call Girls Services, Hire Now for Full Fun
North Avenue Call Girls Services, Hire Now for Full FunNorth Avenue Call Girls Services, Hire Now for Full Fun
North Avenue Call Girls Services, Hire Now for Full Fun
 
Call Girls Ellis Bridge 7397865700 Independent Call Girls
Call Girls Ellis Bridge 7397865700 Independent Call GirlsCall Girls Ellis Bridge 7397865700 Independent Call Girls
Call Girls Ellis Bridge 7397865700 Independent Call Girls
 
QUIZ BOLLYWOOD ( weekly quiz ) - SJU quizzers
QUIZ BOLLYWOOD ( weekly quiz ) - SJU quizzersQUIZ BOLLYWOOD ( weekly quiz ) - SJU quizzers
QUIZ BOLLYWOOD ( weekly quiz ) - SJU quizzers
 
Call US '' 8377087607'' !! Call Girls In Model Town Metro (Delhi NCR)
Call US '' 8377087607'' !! Call Girls In Model Town Metro (Delhi NCR)Call US '' 8377087607'' !! Call Girls In Model Town Metro (Delhi NCR)
Call US '' 8377087607'' !! Call Girls In Model Town Metro (Delhi NCR)
 

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