SlideShare a Scribd company logo
1 of 22
Download to read offline
Strings
Introduction
Reading and displaying strings
Passing strings to function
String handling functions
• Strings are array of characters i.e. they are
characters arranged one after another in
memory. Thus, a character array is called
string.
• Each character within the string is stored
within one element of the array successively.
• A string is always terminated by a null
character (i.e. slash zero 0).
Introduction
• Operations performed on character strings
include:
– Reading and writing strings
– Copying one string to another
– Combining strings together
– Comparing strings for equality
– Extracting a portion of a string
Arrays and Strings…
• A string variable is declared as an array of
characters.
• Syntax:
char string_name[size];
• E.g. char name[20];
• When the compiler assigns a character string
to a character array, it automatically supplies a
null character (‘0’) at the end of the string
• Strings are initialized in either of the following two forms:
char name[4]={‘R’,‘A’,‘M’, ‘0’};
char name[]={‘R’,‘A’,‘M’, ‘0’};
char name[4]=“RAM”;
char name[]=“RAM”;
• When we initialize a character array by listing its
elements, the null terminator or the size of the array
must be provided explicitly.
Initializing String Variables
R A M 0
name[0] name[1] name[2] name[3]
Reading and displaying Strings
• It can be done manually.
Using printf() and scanf()
Using gets() and puts()
Passing String to function
String handling functions
• Strings need to be manipulated by
programmer.
• It can be done manually but is time
consuming.
#include <stdio.h>
#include <conio.h>
void main()
{
char input_string[50];
int i=0, length=0;
clrscr();
printf("nEnter your text:t");
gets(input_string);
while(input_string[i]!='0')
{
length++;
i++;
}
printf("nThe length of your text is: %d character(s)", length);
getch();
}
Counting length of the string
#include <stdio.h>
#include <conio.h>
void main()
{
char copy[50], paste[50];
int i;
clrscr();
printf("nEnter your name (to copy):t");
gets(copy);
for(i=0;copy[i]!='0';i++)
{
paste[i]=copy[i];
}
paste[i]='0';
printf("nThe name is (pasted as):t");
puts(paste);
getch();
}
Copying one string to another
• There are various string handling functions
define in string.h some of them are:
void main()
{
char input_string[50];
int length;
clrscr();
printf("nEnter your text:t");
gets(input_string);
length=strlen(input_string);
printf("nThe length of your text is: %d character(s)", length);
getch();
}
14
void main()
{
char copy[50], paste[50];
int i;
clrscr();
printf("nEnter your name (to copy):t");
gets(copy);
strcpy(paste, copy);
printf("nThe name is (pasted as):t");
puts(paste);
getch();
}
15
void main()
{
char first_name[30]=“College " ;
char middle_name[]=" of Applied";
char last_name[]=" Business";
clrscr();
strcat(first_name,middle_name);
puts(first_name);
strcat(first_name,last_name);
puts(first_name);
getch();
}
16
void main()
{
char str1[30],str2[40];
int diff;
clrscr();
printf("Enter first string:t");
gets(str1);
printf("nEnter second string:t");
gets(str2);
diff=strcmp(str1, str2);
if(diff>0)
printf("n%s is greater than %s by ASCII value difference %d", str1,
str2, diff);
else if(diff<0)
printf("n%s is smaller than %s by ASCII value difference %d", str1,
str2, diff);
else
printf("n%s is same as %s", str1, str2);
getch();
}
17
void main()
{
char string[25];
clrscr();
printf("nInput string to be reversed:");
gets(string);
strrev(string);
printf("nThe reversed string is: %s", string);
getch();
}
18
• String is array of characters.
• Thus an array of string is 2-D array of
characters.
• E.g.
char names[5][10];
• Here, names[5][10] means 5 names having 10
characters each.
19
Arrays of Strings
Classwork
• WAP to read name of 5 persons using array of
strings and display them
• WAP to sort name of 5 persons in alphabetical
order
void main()
{
char names[5][10];
int i;
clrscr();
printf("nEnter name of 5 persons:");
for(i=0;i<5;i++)
scanf("%s", names[i]);
printf("nThe names are:");
for(i=0;i<5;i++)
printf("n%s", names[i]);
getch();
}
21
void main()
{
char names[5][10],temp[10];
int i, j;
clrscr();
printf("nEnter name of 5 persons:");
for(i=0;i<5;i++)
gets(names[i]);
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(strcmp(names[i], names[j])>0)
{
strcpy(temp, names[i]);
strcpy(names[i], names[j]);
strcpy(names[j], temp);
}
}
}
printf("nNames in ascending order:n");
for(i=0;i<5;i++)
puts(names[i]);
getch();
}
22

More Related Content

What's hot

Functions in c language
Functions in c language Functions in c language
Functions in c language tanmaymodi4
 
Constants in C Programming
Constants in C ProgrammingConstants in C Programming
Constants in C Programmingprogramming9
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C ProgrammingShuvongkor Barman
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator OverloadingNilesh Dalvi
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arraysNeeru Mittal
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in pythoneShikshak
 
Strings in c
Strings in cStrings in c
Strings in cvampugani
 
Strings Functions in C Programming
Strings Functions in C ProgrammingStrings Functions in C Programming
Strings Functions in C ProgrammingDevoAjit Gupta
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming LanguageMahantesh Devoor
 
Call by value
Call by valueCall by value
Call by valueDharani G
 
Data Types and Variables In C Programming
Data Types and Variables In C ProgrammingData Types and Variables In C Programming
Data Types and Variables In C ProgrammingKamal Acharya
 
Data types in C language
Data types in C languageData types in C language
Data types in C languagekashyap399
 

What's hot (20)

Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Constants in C Programming
Constants in C ProgrammingConstants in C Programming
Constants in C Programming
 
Structure in C
Structure in CStructure in C
Structure in C
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
Function in c
Function in cFunction in c
Function in c
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
Strings in c
Strings in cStrings in c
Strings in c
 
Strings Functions in C Programming
Strings Functions in C ProgrammingStrings Functions in C Programming
Strings Functions in C Programming
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming Language
 
C string
C stringC string
C string
 
Call by value
Call by valueCall by value
Call by value
 
Data Types and Variables In C Programming
Data Types and Variables In C ProgrammingData Types and Variables In C Programming
Data Types and Variables In C Programming
 
Data types in C
Data types in CData types in C
Data types in C
 
Data types in C language
Data types in C languageData types in C language
Data types in C language
 

Viewers also liked (8)

Structure c
Structure cStructure c
Structure 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
 
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
 
Array in c language
Array in c languageArray in c language
Array in c language
 
String c
String cString c
String c
 
String in c
String in cString in c
String in c
 

Similar to Strings in C

strings-150319180934-conversion-gate01.pdf
strings-150319180934-conversion-gate01.pdfstrings-150319180934-conversion-gate01.pdf
strings-150319180934-conversion-gate01.pdfHEMAHEMS5
 
Strings in programming tutorial.
Strings  in programming tutorial.Strings  in programming tutorial.
Strings in programming tutorial.Samsil Arefin
 
SPL 13 | Character Array(String) in C
SPL 13 | Character Array(String) in CSPL 13 | Character Array(String) in C
SPL 13 | Character Array(String) in CMohammad Imam Hossain
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programmingAppili Vamsi Krishna
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programmingmikeymanjiro2090
 
Array , Structure and Basic Algorithms.pptx
Array , Structure and Basic Algorithms.pptxArray , Structure and Basic Algorithms.pptx
Array , Structure and Basic Algorithms.pptxMrNikhilMohanShinde
 
Cse115 lecture14strings part01
Cse115 lecture14strings part01Cse115 lecture14strings part01
Cse115 lecture14strings part01Md. Ashikur Rahman
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiSowmya Jyothi
 
Lecture14.pdf
Lecture14.pdfLecture14.pdf
Lecture14.pdfJoyPalit
 
0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdfssusere19c741
 
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
 

Similar to Strings in C (20)

strings-150319180934-conversion-gate01.pdf
strings-150319180934-conversion-gate01.pdfstrings-150319180934-conversion-gate01.pdf
strings-150319180934-conversion-gate01.pdf
 
Strings in programming tutorial.
Strings  in programming tutorial.Strings  in programming tutorial.
Strings in programming tutorial.
 
SPL 13 | Character Array(String) in C
SPL 13 | Character Array(String) in CSPL 13 | Character Array(String) in C
SPL 13 | Character Array(String) in C
 
COm1407: Character & Strings
COm1407: Character & StringsCOm1407: Character & Strings
COm1407: Character & Strings
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
 
[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++
 
Lecture 17 - Strings
Lecture 17 - StringsLecture 17 - Strings
Lecture 17 - Strings
 
Array , Structure and Basic Algorithms.pptx
Array , Structure and Basic Algorithms.pptxArray , Structure and Basic Algorithms.pptx
Array , Structure and Basic Algorithms.pptx
 
Cse115 lecture14strings part01
Cse115 lecture14strings part01Cse115 lecture14strings part01
Cse115 lecture14strings part01
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothi
 
String
StringString
String
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
Lecture14.pdf
Lecture14.pdfLecture14.pdf
Lecture14.pdf
 
0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf
 
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 notes
String notesString notes
String notes
 
CP-STRING (1).ppt
CP-STRING (1).pptCP-STRING (1).ppt
CP-STRING (1).ppt
 
CP-STRING.ppt
CP-STRING.pptCP-STRING.ppt
CP-STRING.ppt
 
CP-STRING.ppt
CP-STRING.pptCP-STRING.ppt
CP-STRING.ppt
 

More from Kamal Acharya

Programming the basic computer
Programming the basic computerProgramming the basic computer
Programming the basic computerKamal Acharya
 
Introduction to Computer Security
Introduction to Computer SecurityIntroduction to Computer Security
Introduction to Computer SecurityKamal Acharya
 
Making decision and repeating in PHP
Making decision and repeating  in PHPMaking decision and repeating  in PHP
Making decision and repeating in PHPKamal Acharya
 
Working with arrays in php
Working with arrays in phpWorking with arrays in php
Working with arrays in phpKamal Acharya
 
Text and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHPText and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHPKamal Acharya
 
Capacity Planning of Data Warehousing
Capacity Planning of Data WarehousingCapacity Planning of Data Warehousing
Capacity Planning of Data WarehousingKamal Acharya
 
Information Privacy and Data Mining
Information Privacy and Data MiningInformation Privacy and Data Mining
Information Privacy and Data MiningKamal Acharya
 
Association Analysis in Data Mining
Association Analysis in Data MiningAssociation Analysis in Data Mining
Association Analysis in Data MiningKamal Acharya
 
Classification techniques in data mining
Classification techniques in data miningClassification techniques in data mining
Classification techniques in data miningKamal Acharya
 
Introduction to Data Mining and Data Warehousing
Introduction to Data Mining and Data WarehousingIntroduction to Data Mining and Data Warehousing
Introduction to Data Mining and Data WarehousingKamal Acharya
 

More from Kamal Acharya (20)

Programming the basic computer
Programming the basic computerProgramming the basic computer
Programming the basic computer
 
Computer Arithmetic
Computer ArithmeticComputer Arithmetic
Computer Arithmetic
 
Introduction to Computer Security
Introduction to Computer SecurityIntroduction to Computer Security
Introduction to Computer Security
 
Session and Cookies
Session and CookiesSession and Cookies
Session and Cookies
 
Functions in php
Functions in phpFunctions in php
Functions in php
 
Web forms in php
Web forms in phpWeb forms in php
Web forms in php
 
Making decision and repeating in PHP
Making decision and repeating  in PHPMaking decision and repeating  in PHP
Making decision and repeating in PHP
 
Working with arrays in php
Working with arrays in phpWorking with arrays in php
Working with arrays in php
 
Text and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHPText and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHP
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Capacity Planning of Data Warehousing
Capacity Planning of Data WarehousingCapacity Planning of Data Warehousing
Capacity Planning of Data Warehousing
 
Data Warehousing
Data WarehousingData Warehousing
Data Warehousing
 
Search Engines
Search EnginesSearch Engines
Search Engines
 
Web Mining
Web MiningWeb Mining
Web Mining
 
Information Privacy and Data Mining
Information Privacy and Data MiningInformation Privacy and Data Mining
Information Privacy and Data Mining
 
Cluster Analysis
Cluster AnalysisCluster Analysis
Cluster Analysis
 
Association Analysis in Data Mining
Association Analysis in Data MiningAssociation Analysis in Data Mining
Association Analysis in Data Mining
 
Classification techniques in data mining
Classification techniques in data miningClassification techniques in data mining
Classification techniques in data mining
 
Data Preprocessing
Data PreprocessingData Preprocessing
Data Preprocessing
 
Introduction to Data Mining and Data Warehousing
Introduction to Data Mining and Data WarehousingIntroduction to Data Mining and Data Warehousing
Introduction to Data Mining and Data Warehousing
 

Recently uploaded

What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?TechSoup
 
UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024UKCGE
 
Philosophy of Education and Educational Philosophy
Philosophy of Education  and Educational PhilosophyPhilosophy of Education  and Educational Philosophy
Philosophy of Education and Educational PhilosophyShuvankar Madhu
 
The Stolen Bacillus by Herbert George Wells
The Stolen Bacillus by Herbert George WellsThe Stolen Bacillus by Herbert George Wells
The Stolen Bacillus by Herbert George WellsEugene Lysak
 
Quality Assurance_GOOD LABORATORY PRACTICE
Quality Assurance_GOOD LABORATORY PRACTICEQuality Assurance_GOOD LABORATORY PRACTICE
Quality Assurance_GOOD LABORATORY PRACTICESayali Powar
 
How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17Celine George
 
Benefits & Challenges of Inclusive Education
Benefits & Challenges of Inclusive EducationBenefits & Challenges of Inclusive Education
Benefits & Challenges of Inclusive EducationMJDuyan
 
The Singapore Teaching Practice document
The Singapore Teaching Practice documentThe Singapore Teaching Practice document
The Singapore Teaching Practice documentXsasf Sfdfasd
 
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptxPISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptxEduSkills OECD
 
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRADUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRATanmoy Mishra
 
Education and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxEducation and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxraviapr7
 
How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17Celine George
 
Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...raviapr7
 
3.21.24 The Origins of Black Power.pptx
3.21.24  The Origins of Black Power.pptx3.21.24  The Origins of Black Power.pptx
3.21.24 The Origins of Black Power.pptxmary850239
 
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptxSandy Millin
 
HED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfHED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfMohonDas
 
Prescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxPrescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxraviapr7
 
How to Manage Cross-Selling in Odoo 17 Sales
How to Manage Cross-Selling in Odoo 17 SalesHow to Manage Cross-Selling in Odoo 17 Sales
How to Manage Cross-Selling in Odoo 17 SalesCeline George
 
Ultra structure and life cycle of Plasmodium.pptx
Ultra structure and life cycle of Plasmodium.pptxUltra structure and life cycle of Plasmodium.pptx
Ultra structure and life cycle of Plasmodium.pptxDr. Asif Anas
 

Recently uploaded (20)

What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?
 
UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024
 
Philosophy of Education and Educational Philosophy
Philosophy of Education  and Educational PhilosophyPhilosophy of Education  and Educational Philosophy
Philosophy of Education and Educational Philosophy
 
The Stolen Bacillus by Herbert George Wells
The Stolen Bacillus by Herbert George WellsThe Stolen Bacillus by Herbert George Wells
The Stolen Bacillus by Herbert George Wells
 
Quality Assurance_GOOD LABORATORY PRACTICE
Quality Assurance_GOOD LABORATORY PRACTICEQuality Assurance_GOOD LABORATORY PRACTICE
Quality Assurance_GOOD LABORATORY PRACTICE
 
How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17
 
Benefits & Challenges of Inclusive Education
Benefits & Challenges of Inclusive EducationBenefits & Challenges of Inclusive Education
Benefits & Challenges of Inclusive Education
 
The Singapore Teaching Practice document
The Singapore Teaching Practice documentThe Singapore Teaching Practice document
The Singapore Teaching Practice document
 
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptxPISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
 
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRADUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
 
Prelims of Kant get Marx 2.0: a general politics quiz
Prelims of Kant get Marx 2.0: a general politics quizPrelims of Kant get Marx 2.0: a general politics quiz
Prelims of Kant get Marx 2.0: a general politics quiz
 
Education and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxEducation and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptx
 
How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17
 
Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...
 
3.21.24 The Origins of Black Power.pptx
3.21.24  The Origins of Black Power.pptx3.21.24  The Origins of Black Power.pptx
3.21.24 The Origins of Black Power.pptx
 
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
 
HED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfHED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdf
 
Prescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxPrescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptx
 
How to Manage Cross-Selling in Odoo 17 Sales
How to Manage Cross-Selling in Odoo 17 SalesHow to Manage Cross-Selling in Odoo 17 Sales
How to Manage Cross-Selling in Odoo 17 Sales
 
Ultra structure and life cycle of Plasmodium.pptx
Ultra structure and life cycle of Plasmodium.pptxUltra structure and life cycle of Plasmodium.pptx
Ultra structure and life cycle of Plasmodium.pptx
 

Strings in C

  • 1. Strings Introduction Reading and displaying strings Passing strings to function String handling functions
  • 2. • Strings are array of characters i.e. they are characters arranged one after another in memory. Thus, a character array is called string. • Each character within the string is stored within one element of the array successively. • A string is always terminated by a null character (i.e. slash zero 0). Introduction
  • 3. • Operations performed on character strings include: – Reading and writing strings – Copying one string to another – Combining strings together – Comparing strings for equality – Extracting a portion of a string Arrays and Strings…
  • 4. • A string variable is declared as an array of characters. • Syntax: char string_name[size]; • E.g. char name[20]; • When the compiler assigns a character string to a character array, it automatically supplies a null character (‘0’) at the end of the string
  • 5. • Strings are initialized in either of the following two forms: char name[4]={‘R’,‘A’,‘M’, ‘0’}; char name[]={‘R’,‘A’,‘M’, ‘0’}; char name[4]=“RAM”; char name[]=“RAM”; • When we initialize a character array by listing its elements, the null terminator or the size of the array must be provided explicitly. Initializing String Variables R A M 0 name[0] name[1] name[2] name[3]
  • 6. Reading and displaying Strings • It can be done manually.
  • 9. Passing String to function
  • 10. String handling functions • Strings need to be manipulated by programmer. • It can be done manually but is time consuming.
  • 11. #include <stdio.h> #include <conio.h> void main() { char input_string[50]; int i=0, length=0; clrscr(); printf("nEnter your text:t"); gets(input_string); while(input_string[i]!='0') { length++; i++; } printf("nThe length of your text is: %d character(s)", length); getch(); } Counting length of the string
  • 12. #include <stdio.h> #include <conio.h> void main() { char copy[50], paste[50]; int i; clrscr(); printf("nEnter your name (to copy):t"); gets(copy); for(i=0;copy[i]!='0';i++) { paste[i]=copy[i]; } paste[i]='0'; printf("nThe name is (pasted as):t"); puts(paste); getch(); } Copying one string to another
  • 13. • There are various string handling functions define in string.h some of them are:
  • 14. void main() { char input_string[50]; int length; clrscr(); printf("nEnter your text:t"); gets(input_string); length=strlen(input_string); printf("nThe length of your text is: %d character(s)", length); getch(); } 14
  • 15. void main() { char copy[50], paste[50]; int i; clrscr(); printf("nEnter your name (to copy):t"); gets(copy); strcpy(paste, copy); printf("nThe name is (pasted as):t"); puts(paste); getch(); } 15
  • 16. void main() { char first_name[30]=“College " ; char middle_name[]=" of Applied"; char last_name[]=" Business"; clrscr(); strcat(first_name,middle_name); puts(first_name); strcat(first_name,last_name); puts(first_name); getch(); } 16
  • 17. void main() { char str1[30],str2[40]; int diff; clrscr(); printf("Enter first string:t"); gets(str1); printf("nEnter second string:t"); gets(str2); diff=strcmp(str1, str2); if(diff>0) printf("n%s is greater than %s by ASCII value difference %d", str1, str2, diff); else if(diff<0) printf("n%s is smaller than %s by ASCII value difference %d", str1, str2, diff); else printf("n%s is same as %s", str1, str2); getch(); } 17
  • 18. void main() { char string[25]; clrscr(); printf("nInput string to be reversed:"); gets(string); strrev(string); printf("nThe reversed string is: %s", string); getch(); } 18
  • 19. • String is array of characters. • Thus an array of string is 2-D array of characters. • E.g. char names[5][10]; • Here, names[5][10] means 5 names having 10 characters each. 19 Arrays of Strings
  • 20. Classwork • WAP to read name of 5 persons using array of strings and display them • WAP to sort name of 5 persons in alphabetical order
  • 21. void main() { char names[5][10]; int i; clrscr(); printf("nEnter name of 5 persons:"); for(i=0;i<5;i++) scanf("%s", names[i]); printf("nThe names are:"); for(i=0;i<5;i++) printf("n%s", names[i]); getch(); } 21
  • 22. void main() { char names[5][10],temp[10]; int i, j; clrscr(); printf("nEnter name of 5 persons:"); for(i=0;i<5;i++) gets(names[i]); for(i=0;i<5;i++) { for(j=i+1;j<5;j++) { if(strcmp(names[i], names[j])>0) { strcpy(temp, names[i]); strcpy(names[i], names[j]); strcpy(names[j], temp); } } } printf("nNames in ascending order:n"); for(i=0;i<5;i++) puts(names[i]); getch(); } 22