SlideShare a Scribd company logo
1 of 24
Download to read offline
HANDLING OF CHARACTER
STRING
MRS.SOWMYA JYOTHI
•A string is a sequence of characters.
•Any sequence or set of characters defined within
double quotation symbols is a constant string.
•Strings are stored in memory as ASCII codes of
characters that make up the string appended with
‘0’(ASCII value of null). Normally each character is
stored in one byte, successive characters are stored in
successive bytes.
In C it is required to do some meaningful operations on
strings they are:
1. Reading string displaying strings
2. Combining or concatenating strings
3. Copying one string to another.
4. Comparing string & checking whether they are equal
5. Extraction of a portion of a string
Character m y a g e i s
ASCII
Code 77 121 32 97 103 10 32 105 115
Character 2 ( t w o ) 0
ASCII
Code 32 50 32 40 116 119 41 0 0
The last character is the null character having ASCII value zero.
Initializing Strings
• Following the discussion on characters arrays, the
initialization of a string must the following form which is
simpler to one dimension array.
• char month1[ ]={‘j’,’a’,’n’,’u’,’a’,’r’,’y’};
• This is perfectly valid but C offers a special way to initialize strings.
• The above string can be initialized char month1[]=”January”;
• The characters of the string are enclosed within a part of double quotes.
• The compiler takes care of string enclosed within a pair of a double quotes.
• The compiler takes care of storing the ASCII codes of characters of the string
in the memory and also stores the null terminator in the end.
J
A
N
U
A
R
Y
0
•/*String.c string variable*/
#include < stdio.h >
main()
{
char month[15];
printf (“Enter the string”);
gets (month);
printf (“The string entered is %s”, month);
}
It is one dimension array. Each character occupies a byte.
A null character (0) that has the ASCII value 0 terminates the
string.
• A string variable is any valid C variable name & is always
declared as an array.
The general form of declaration of a string variable is
char string_name[size];
• The size determines the number of characters in the string
name.
Example:
• char month[10];
char address[100];
• The size of the array should be one byte more than the
actual space occupied by the string since the complier
appends a null character at the end of the string.
Reading Strings from the terminal:
The function scanf with %s format specification is needed to read the
character string from the terminal.
Example:
char address[15];
scanf(“%s”,address);
Scanf statement has a draw back it just terminates the statement
as soon as it finds a blank space, suppose if we type the string
new york then only the string new will be read and since there is
a blank space after word “new” it will terminate the string.
Writing strings to screen:
•The printf statement along with format specifier %s
to print strings on to the screen.
•The format %s can be used to display an array of
characters that is terminated by the null character.
For example
printf(“%s”,name);
can be used to display the entire contents of the array
name.
•We can also get the support of the c library function
to converts a string of digits into their equivalent
integer values
•the general format of the function in x=atoi(string)
here x is an integer variable & string is a character
array containing string of digits.
String operations (string.h)
C library supports a large number of string handling
functions that can be used to array out many o f the
string manipulations such as:
1. Length (number of characters in the string).
2. Concatenation (adding two are more strings)
3. Comparing two strings.
4. Substring (Extract substring from a given string)
5. Copy(copies one string over another)
strlen() function:
This function counts and returns the number of
characters in a string.
The length does not include a null character.
Syntax n=strlen(string);
Where n is integer variable.
Which receives the value of length of the string.
Example
length=strlen(“Hollywood”);
/*write a c program to find the length of the string using strlen() function*/
#include < stdio.h >
include < string.h >
void main()
{
char name[100];
int length;
printf(“Enter the string”);
gets(name);
length=strlen(name);
printf(“nNumber of characters in the string is=%d”, length);
}
strcat() function:
• When you combine two strings, you add the characters of one string
to the end of other string. This process is called concatenation.
• The strcat() function joins 2 strings together.
It takes the following form
•strcat(string1,string2)
• string1 & string2 are character arrays.
• When the function strcat is executed string2 is appended to string1.
the string at string2 remains unchanged.
• Example
• strcpy(string1,”SDM”);
strcpy(string2,”CBM”);
printf(“%s”,strcat(string1,string2);
• From the above program segment the value of string1
becomes SDMCBM.
• The string at str2 remains unchanged as CBM.
strcmp function:
• In c you cannot directly compare the value of 2 strings in a condition
like if(string1==string2)
Most libraries however contain the strcmp() function, which returns a
zero if 2 strings are equal, or a non zero number if the strings are
not the same.
The syntax of strcmp() is given below:
• strcmp(string1,string2)
• String1 & string2 may be string variables or string constants.
• String1, & string2 may be string variables or string constants some
computers return a negative if the string1 is alphabetically less than
the second and
• a positive number if the string is greater than the second.
Examples:
•strcmp(“Newyork”,”Newyork”) will return zero
because 2 strings are equal.
•strcmp(“their”,”there”) will return a 9 which is the
numeric difference between ASCII ‘i’ and ASCII ’r’.
•strcmp(“The”, “the”) will return 32 which is the
numeric difference between ASCII “T” & ASCII “t”.
strcmpi() function
•This function is same as strcmp() which
compares 2 strings but not case sensitive.
•Example
•strcmpi(“THE”,”the”); will return 0.
strcpy() function:
• C does not allow you to assign the characters to a string
directly as in the statement name=”Robert”;
• Instead use the strcpy() function found in most compilers the
syntax of the function is illustrated below.
Syntax: strcpy(string1,string2);
• Strcpy function assigns the contents of string2 to string1.
string2 may be a character array variable or a string constant.
strcpy(Name,”SDM”);
• In the above example SDM is assigned to the string called
name.
strlwr () function:
•This function converts all characters in a string from
uppercase to lowercase.
Syntax
strlwr(string);
For example:
strlwr(“EXFORSYS”) converts to exforsys.
strupr() function:
• This function converts all characters in a string from lower
case to uppercase.
• Syntax
strupr(string);
• For example strupr(“exforsys”) will convert the string to
EXFORSYS.
strrev() function:
•This function reverses the characters in a string.
•Syntax
strrev(string);
•For ex
strrev(“program”) reverses the characters in a string
into “margrop”.
/* Example program to use string functions*/
#include < stdio.h >
#include < string.h >
void main()
{
char s1[20],s2[20],s3[20];
int x,l1,l2,l3;
printf(“Enter the strings”);
scanf(“%s%s”,s1,s2);
x=strcmp(s1,s2);
if(x!=0)
{
printf(“nStrings are not equaln”);
strcat(s1,s2);
}
else
printf(“nStrings are equal”);
strcpy(s3,s1);
l1=strlen(s1);
l2=strlen(s2);
l3=strlen(s3);
printf(“ns1 length=%d”,l1);
printf(“ns2 length=%d”,l2);
printf(“ns3 length=%d”,l3);
}

More Related Content

What's hot

C programming session 04
C programming session 04C programming session 04
C programming session 04
Dushmanta Nath
 
Cpu-fundamental of C
Cpu-fundamental of CCpu-fundamental of C
Cpu-fundamental of C
Suchit Patel
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
Dushmanta Nath
 
C UNIT-3 PREPARED BY M V B REDDY
C UNIT-3 PREPARED BY M V B REDDYC UNIT-3 PREPARED BY M V B REDDY
C UNIT-3 PREPARED BY M V B REDDY
Rajeshkumar Reddy
 
constants, variables and datatypes in C
constants, variables and datatypes in Cconstants, variables and datatypes in C
constants, variables and datatypes in C
Sahithi Naraparaju
 
12 computer science_notes_ch01_overview_of_cpp
12 computer science_notes_ch01_overview_of_cpp12 computer science_notes_ch01_overview_of_cpp
12 computer science_notes_ch01_overview_of_cpp
sharvivek
 

What's hot (20)

POINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdfPOINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
 
C programming session 04
C programming session 04C programming session 04
C programming session 04
 
Strings
StringsStrings
Strings
 
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdfUSER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
 
Managing I/O & String function in C
Managing I/O & String function in CManaging I/O & String function in C
Managing I/O & String function in C
 
C string
C stringC string
C string
 
Cpu-fundamental of C
Cpu-fundamental of CCpu-fundamental of C
Cpu-fundamental of C
 
Unit 2. Elements of C
Unit 2. Elements of CUnit 2. Elements of C
Unit 2. Elements of C
 
Unit 8. Pointers
Unit 8. PointersUnit 8. Pointers
Unit 8. Pointers
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
 
Strings IN C
Strings IN CStrings IN C
Strings IN C
 
C UNIT-3 PREPARED BY M V B REDDY
C UNIT-3 PREPARED BY M V B REDDYC UNIT-3 PREPARED BY M V B REDDY
C UNIT-3 PREPARED BY M V B REDDY
 
constants, variables and datatypes in C
constants, variables and datatypes in Cconstants, variables and datatypes in C
constants, variables and datatypes in C
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
Strings in c language
Strings in  c languageStrings in  c language
Strings in c language
 
12 computer science_notes_ch01_overview_of_cpp
12 computer science_notes_ch01_overview_of_cpp12 computer science_notes_ch01_overview_of_cpp
12 computer science_notes_ch01_overview_of_cpp
 
Getting started with c++.pptx
Getting started with c++.pptxGetting started with c++.pptx
Getting started with c++.pptx
 
Ch08
Ch08Ch08
Ch08
 
[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
 

Similar to Strings in c mrs.sowmya jyothi

String & its application
String & its applicationString & its application
String & its application
Tech_MX
 

Similar to Strings in c mrs.sowmya jyothi (20)

string in C
string in Cstring in C
string in C
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
String notes
String notesString notes
String notes
 
0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf
 
Lecture 05 2017
Lecture 05 2017Lecture 05 2017
Lecture 05 2017
 
Strings in c
Strings in cStrings in c
Strings in c
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
Operation on string presentation
Operation on string presentationOperation on string presentation
Operation on string presentation
 
Strings
StringsStrings
Strings
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
Strings
StringsStrings
Strings
 
[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++
 
String & its application
String & its applicationString & its application
String & its application
 
Array &strings
Array &stringsArray &strings
Array &strings
 
Python data handling
Python data handlingPython data handling
Python data handling
 
Chapter 4 (Part II) - Array and Strings.pdf
Chapter 4 (Part II) - Array and Strings.pdfChapter 4 (Part II) - Array and Strings.pdf
Chapter 4 (Part II) - Array and Strings.pdf
 
strings
stringsstrings
strings
 
Character Arrays and strings in c language
Character Arrays and strings in c languageCharacter Arrays and strings in c language
Character Arrays and strings in c language
 
fundamentals of c programming_String.pptx
fundamentals of c programming_String.pptxfundamentals of c programming_String.pptx
fundamentals of c programming_String.pptx
 

More from Sowmya Jyothi

More from Sowmya Jyothi (20)

Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
 
Functions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothiFunctions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothi
 
Arrays in c unit iii chapter 1 mrs.sowmya jyothi
Arrays in c unit iii chapter 1 mrs.sowmya jyothiArrays in c unit iii chapter 1 mrs.sowmya jyothi
Arrays in c unit iii chapter 1 mrs.sowmya jyothi
 
Bca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothiBca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothi
 
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHIBCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
 
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHIBCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
 
BCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHI
BCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHIBCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHI
BCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHI
 
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHIBCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
 
HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...
HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...
HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...
 
Unit II chapter 4 Loops in C
Unit II chapter 4 Loops in CUnit II chapter 4 Loops in C
Unit II chapter 4 Loops in C
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in C
 
Unit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in cUnit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in c
 
Overview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya JyothiOverview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya Jyothi
 
NETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHI
NETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHINETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHI
NETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHI
 
Introduction to computers MRS. SOWMYA JYOTHI
Introduction to computers MRS. SOWMYA JYOTHIIntroduction to computers MRS. SOWMYA JYOTHI
Introduction to computers MRS. SOWMYA JYOTHI
 
Introduction to graphics
Introduction to graphicsIntroduction to graphics
Introduction to graphics
 
Inter Process Communication PPT
Inter Process Communication PPTInter Process Communication PPT
Inter Process Communication PPT
 
Internal representation of file chapter 4 Sowmya Jyothi
Internal representation of file chapter 4 Sowmya JyothiInternal representation of file chapter 4 Sowmya Jyothi
Internal representation of file chapter 4 Sowmya Jyothi
 
Buffer cache unix ppt Mrs.Sowmya Jyothi
Buffer cache unix ppt Mrs.Sowmya JyothiBuffer cache unix ppt Mrs.Sowmya Jyothi
Buffer cache unix ppt Mrs.Sowmya Jyothi
 
Introduction to the Kernel Chapter 2 Mrs.Sowmya Jyothi
Introduction to the Kernel  Chapter 2 Mrs.Sowmya JyothiIntroduction to the Kernel  Chapter 2 Mrs.Sowmya Jyothi
Introduction to the Kernel Chapter 2 Mrs.Sowmya Jyothi
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
httgc7rh9c
 

Recently uploaded (20)

How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Our Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdfOur Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdf
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptx
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 

Strings in c mrs.sowmya jyothi

  • 2. •A string is a sequence of characters. •Any sequence or set of characters defined within double quotation symbols is a constant string. •Strings are stored in memory as ASCII codes of characters that make up the string appended with ‘0’(ASCII value of null). Normally each character is stored in one byte, successive characters are stored in successive bytes.
  • 3. In C it is required to do some meaningful operations on strings they are: 1. Reading string displaying strings 2. Combining or concatenating strings 3. Copying one string to another. 4. Comparing string & checking whether they are equal 5. Extraction of a portion of a string
  • 4. Character m y a g e i s ASCII Code 77 121 32 97 103 10 32 105 115 Character 2 ( t w o ) 0 ASCII Code 32 50 32 40 116 119 41 0 0 The last character is the null character having ASCII value zero.
  • 5. Initializing Strings • Following the discussion on characters arrays, the initialization of a string must the following form which is simpler to one dimension array. • char month1[ ]={‘j’,’a’,’n’,’u’,’a’,’r’,’y’}; • This is perfectly valid but C offers a special way to initialize strings. • The above string can be initialized char month1[]=”January”; • The characters of the string are enclosed within a part of double quotes. • The compiler takes care of string enclosed within a pair of a double quotes. • The compiler takes care of storing the ASCII codes of characters of the string in the memory and also stores the null terminator in the end.
  • 7. •/*String.c string variable*/ #include < stdio.h > main() { char month[15]; printf (“Enter the string”); gets (month); printf (“The string entered is %s”, month); } It is one dimension array. Each character occupies a byte. A null character (0) that has the ASCII value 0 terminates the string.
  • 8. • A string variable is any valid C variable name & is always declared as an array. The general form of declaration of a string variable is char string_name[size]; • The size determines the number of characters in the string name. Example: • char month[10]; char address[100]; • The size of the array should be one byte more than the actual space occupied by the string since the complier appends a null character at the end of the string.
  • 9. Reading Strings from the terminal: The function scanf with %s format specification is needed to read the character string from the terminal. Example: char address[15]; scanf(“%s”,address); Scanf statement has a draw back it just terminates the statement as soon as it finds a blank space, suppose if we type the string new york then only the string new will be read and since there is a blank space after word “new” it will terminate the string.
  • 10. Writing strings to screen: •The printf statement along with format specifier %s to print strings on to the screen. •The format %s can be used to display an array of characters that is terminated by the null character. For example printf(“%s”,name); can be used to display the entire contents of the array name.
  • 11. •We can also get the support of the c library function to converts a string of digits into their equivalent integer values •the general format of the function in x=atoi(string) here x is an integer variable & string is a character array containing string of digits.
  • 12. String operations (string.h) C library supports a large number of string handling functions that can be used to array out many o f the string manipulations such as: 1. Length (number of characters in the string). 2. Concatenation (adding two are more strings) 3. Comparing two strings. 4. Substring (Extract substring from a given string) 5. Copy(copies one string over another)
  • 13. strlen() function: This function counts and returns the number of characters in a string. The length does not include a null character. Syntax n=strlen(string); Where n is integer variable. Which receives the value of length of the string. Example length=strlen(“Hollywood”);
  • 14. /*write a c program to find the length of the string using strlen() function*/ #include < stdio.h > include < string.h > void main() { char name[100]; int length; printf(“Enter the string”); gets(name); length=strlen(name); printf(“nNumber of characters in the string is=%d”, length); }
  • 15. strcat() function: • When you combine two strings, you add the characters of one string to the end of other string. This process is called concatenation. • The strcat() function joins 2 strings together. It takes the following form •strcat(string1,string2) • string1 & string2 are character arrays. • When the function strcat is executed string2 is appended to string1. the string at string2 remains unchanged.
  • 16. • Example • strcpy(string1,”SDM”); strcpy(string2,”CBM”); printf(“%s”,strcat(string1,string2); • From the above program segment the value of string1 becomes SDMCBM. • The string at str2 remains unchanged as CBM.
  • 17. strcmp function: • In c you cannot directly compare the value of 2 strings in a condition like if(string1==string2) Most libraries however contain the strcmp() function, which returns a zero if 2 strings are equal, or a non zero number if the strings are not the same. The syntax of strcmp() is given below: • strcmp(string1,string2) • String1 & string2 may be string variables or string constants. • String1, & string2 may be string variables or string constants some computers return a negative if the string1 is alphabetically less than the second and • a positive number if the string is greater than the second.
  • 18. Examples: •strcmp(“Newyork”,”Newyork”) will return zero because 2 strings are equal. •strcmp(“their”,”there”) will return a 9 which is the numeric difference between ASCII ‘i’ and ASCII ’r’. •strcmp(“The”, “the”) will return 32 which is the numeric difference between ASCII “T” & ASCII “t”.
  • 19. strcmpi() function •This function is same as strcmp() which compares 2 strings but not case sensitive. •Example •strcmpi(“THE”,”the”); will return 0.
  • 20. strcpy() function: • C does not allow you to assign the characters to a string directly as in the statement name=”Robert”; • Instead use the strcpy() function found in most compilers the syntax of the function is illustrated below. Syntax: strcpy(string1,string2); • Strcpy function assigns the contents of string2 to string1. string2 may be a character array variable or a string constant. strcpy(Name,”SDM”); • In the above example SDM is assigned to the string called name.
  • 21. strlwr () function: •This function converts all characters in a string from uppercase to lowercase. Syntax strlwr(string); For example: strlwr(“EXFORSYS”) converts to exforsys.
  • 22. strupr() function: • This function converts all characters in a string from lower case to uppercase. • Syntax strupr(string); • For example strupr(“exforsys”) will convert the string to EXFORSYS.
  • 23. strrev() function: •This function reverses the characters in a string. •Syntax strrev(string); •For ex strrev(“program”) reverses the characters in a string into “margrop”.
  • 24. /* Example program to use string functions*/ #include < stdio.h > #include < string.h > void main() { char s1[20],s2[20],s3[20]; int x,l1,l2,l3; printf(“Enter the strings”); scanf(“%s%s”,s1,s2); x=strcmp(s1,s2); if(x!=0) { printf(“nStrings are not equaln”); strcat(s1,s2); } else printf(“nStrings are equal”); strcpy(s3,s1); l1=strlen(s1); l2=strlen(s2); l3=strlen(s3); printf(“ns1 length=%d”,l1); printf(“ns2 length=%d”,l2); printf(“ns3 length=%d”,l3); }