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

Data types in C language
Data types in C languageData types in C language
Data types in C language
kashyap399
Β 

What's hot (20)

Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
Β 
String c
String cString c
String c
Β 
Storage classes in C
Storage classes in CStorage classes in C
Storage classes in C
Β 
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdfSTRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
Β 
Function in C program
Function in C programFunction in C program
Function in C program
Β 
Keywords, identifiers ,datatypes in C++
Keywords, identifiers ,datatypes in C++Keywords, identifiers ,datatypes in C++
Keywords, identifiers ,datatypes in C++
Β 
Strings in c++
Strings in c++Strings in c++
Strings in c++
Β 
Array and string
Array and stringArray and string
Array and string
Β 
Data types in C language
Data types in C languageData types in C language
Data types in C language
Β 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
Β 
Strings IN C
Strings IN CStrings IN C
Strings IN C
Β 
Function in c
Function in cFunction in c
Function in c
Β 
Character set of c
Character set of cCharacter set of c
Character set of c
Β 
C introduction by thooyavan
C introduction by  thooyavanC introduction by  thooyavan
C introduction by thooyavan
Β 
String functions in C
String functions in CString functions in C
String functions in C
Β 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
Β 
Tokens expressionsin C++
Tokens expressionsin C++Tokens expressionsin C++
Tokens expressionsin C++
Β 
Structure & union
Structure & unionStructure & union
Structure & union
Β 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
Β 
Java String Handling
Java String HandlingJava String Handling
Java String Handling
Β 

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
Β 
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
Β 

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
Β 
Strings
StringsStrings
Strings
Β 
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
Β 

More from 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

Beautiful πŸ˜‹ Call girls in Lahore 03210033448
Beautiful πŸ˜‹ Call girls in Lahore 03210033448Beautiful πŸ˜‹ Call girls in Lahore 03210033448
Beautiful πŸ˜‹ Call girls in Lahore 03210033448
ont65320
Β 
CHEAP Call Girls in Malviya Nagar, (-DELHI )πŸ” 9953056974πŸ”(=)/CALL GIRLS SERVICE
CHEAP Call Girls in  Malviya Nagar, (-DELHI )πŸ” 9953056974πŸ”(=)/CALL GIRLS SERVICECHEAP Call Girls in  Malviya Nagar, (-DELHI )πŸ” 9953056974πŸ”(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Malviya Nagar, (-DELHI )πŸ” 9953056974πŸ”(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
Β 
Jodhpur Park ( Call Girls ) Kolkata βœ” 6297143586 βœ” Hot Model With Sexy Bhabi ...
Jodhpur Park ( Call Girls ) Kolkata βœ” 6297143586 βœ” Hot Model With Sexy Bhabi ...Jodhpur Park ( Call Girls ) Kolkata βœ” 6297143586 βœ” Hot Model With Sexy Bhabi ...
Jodhpur Park ( Call Girls ) Kolkata βœ” 6297143586 βœ” Hot Model With Sexy Bhabi ...
ritikasharma
Β 
Top Rated Kolkata Call Girls Dum Dum ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...
Top Rated Kolkata Call Girls Dum Dum ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...Top Rated Kolkata Call Girls Dum Dum ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...
Top Rated Kolkata Call Girls Dum Dum ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...
ritikasharma
Β 
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...
Shivani Pandey
Β 
Verified Trusted Call Girls Ambattur Chennai βœ”βœ”7427069034 Independent Chenna...
Verified Trusted Call Girls Ambattur Chennai βœ”βœ”7427069034  Independent Chenna...Verified Trusted Call Girls Ambattur Chennai βœ”βœ”7427069034  Independent Chenna...
Verified Trusted Call Girls Ambattur Chennai βœ”βœ”7427069034 Independent Chenna...
Shivani Pandey
Β 
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
Β 
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
Β 
Dum Dum ( Call Girls ) Kolkata βœ” 6297143586 βœ” Hot Model With Sexy Bhabi Ready...
Dum Dum ( Call Girls ) Kolkata βœ” 6297143586 βœ” Hot Model With Sexy Bhabi Ready...Dum Dum ( Call Girls ) Kolkata βœ” 6297143586 βœ” Hot Model With Sexy Bhabi Ready...
Dum Dum ( Call Girls ) Kolkata βœ” 6297143586 βœ” Hot Model With Sexy Bhabi Ready...
ritikasharma
Β 
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...
ritikasharma
Β 
Hotel And Home Service Available Kolkata Call Girls Diamond Harbour βœ” 6297143...
Hotel And Home Service Available Kolkata Call Girls Diamond Harbour βœ” 6297143...Hotel And Home Service Available Kolkata Call Girls Diamond Harbour βœ” 6297143...
Hotel And Home Service Available Kolkata Call Girls Diamond Harbour βœ” 6297143...
ritikasharma
Β 

Recently uploaded (20)

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
Β 
Beautiful πŸ˜‹ Call girls in Lahore 03210033448
Beautiful πŸ˜‹ Call girls in Lahore 03210033448Beautiful πŸ˜‹ Call girls in Lahore 03210033448
Beautiful πŸ˜‹ Call girls in Lahore 03210033448
Β 
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...
Β 
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
Β 
CHEAP Call Girls in Malviya Nagar, (-DELHI )πŸ” 9953056974πŸ”(=)/CALL GIRLS SERVICE
CHEAP Call Girls in  Malviya Nagar, (-DELHI )πŸ” 9953056974πŸ”(=)/CALL GIRLS SERVICECHEAP Call Girls in  Malviya Nagar, (-DELHI )πŸ” 9953056974πŸ”(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Malviya Nagar, (-DELHI )πŸ” 9953056974πŸ”(=)/CALL GIRLS SERVICE
Β 
πŸ“ž Contact Number 8617697112 VIP East Sikkim Call Girls
πŸ“ž Contact Number 8617697112 VIP East Sikkim Call GirlsπŸ“ž Contact Number 8617697112 VIP East Sikkim Call Girls
πŸ“ž Contact Number 8617697112 VIP East Sikkim Call Girls
Β 
❀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 πŸ’¦βœ….
Β 
Jodhpur Park ( Call Girls ) Kolkata βœ” 6297143586 βœ” Hot Model With Sexy Bhabi ...
Jodhpur Park ( Call Girls ) Kolkata βœ” 6297143586 βœ” Hot Model With Sexy Bhabi ...Jodhpur Park ( Call Girls ) Kolkata βœ” 6297143586 βœ” Hot Model With Sexy Bhabi ...
Jodhpur Park ( Call Girls ) Kolkata βœ” 6297143586 βœ” Hot Model With Sexy Bhabi ...
Β 
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
Β 
Top Rated Kolkata Call Girls Dum Dum ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...
Top Rated Kolkata Call Girls Dum Dum ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...Top Rated Kolkata Call Girls Dum Dum ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...
Top Rated Kolkata Call Girls Dum Dum ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...
Β 
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...
Β 
Verified Trusted Call Girls Ambattur Chennai βœ”βœ”7427069034 Independent Chenna...
Verified Trusted Call Girls Ambattur Chennai βœ”βœ”7427069034  Independent Chenna...Verified Trusted Call Girls Ambattur Chennai βœ”βœ”7427069034  Independent Chenna...
Verified Trusted Call Girls Ambattur Chennai βœ”βœ”7427069034 Independent Chenna...
Β 
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...
Β 
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...
Β 
Dum Dum ( Call Girls ) Kolkata βœ” 6297143586 βœ” Hot Model With Sexy Bhabi Ready...
Dum Dum ( Call Girls ) Kolkata βœ” 6297143586 βœ” Hot Model With Sexy Bhabi Ready...Dum Dum ( Call Girls ) Kolkata βœ” 6297143586 βœ” Hot Model With Sexy Bhabi Ready...
Dum Dum ( Call Girls ) Kolkata βœ” 6297143586 βœ” Hot Model With Sexy Bhabi Ready...
Β 
❀Personal Whatsapp Number Mukteshwar Call Girls 8617697112 πŸ’¦βœ….
❀Personal Whatsapp Number Mukteshwar Call Girls 8617697112 πŸ’¦βœ….❀Personal Whatsapp Number Mukteshwar Call Girls 8617697112 πŸ’¦βœ….
❀Personal Whatsapp Number Mukteshwar Call Girls 8617697112 πŸ’¦βœ….
Β 
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...
Β 
2k Shot Call girls Laxmi Nagar Delhi 9205541914
2k Shot Call girls Laxmi Nagar Delhi 92055419142k Shot Call girls Laxmi Nagar Delhi 9205541914
2k Shot Call girls Laxmi Nagar Delhi 9205541914
Β 
Hotel And Home Service Available Kolkata Call Girls Diamond Harbour βœ” 6297143...
Hotel And Home Service Available Kolkata Call Girls Diamond Harbour βœ” 6297143...Hotel And Home Service Available Kolkata Call Girls Diamond Harbour βœ” 6297143...
Hotel And Home Service Available Kolkata Call Girls Diamond Harbour βœ” 6297143...
Β 
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...
Β 

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