SlideShare a Scribd company logo
Strings
Introduction
Reading and displaying strings
Passing strings to function
String handling functions
C. P. Divate
• Strings are array of characters i.e. they are
arranged one after another in
Thus, a character array is called
characters
memory.
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
include:
– Reading and writing strings
– Copying one string to another
– Combining strings together
– Comparing strings for equality
– Extracting a portion of a string
character strings
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[]=" ofApplied";
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 byASCII 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
• WAPto 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
Ad

More Related Content

Similar to Programming in C - Fundamental Study of Strings (20)

introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
mikeymanjiro2090
 
Basic Algorithms and Array along with Structure.pptx
Basic Algorithms and Array along with Structure.pptxBasic Algorithms and Array along with Structure.pptx
Basic Algorithms and Array along with Structure.pptx
MrNikhilMohanShinde
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
Neeru Mittal
 
[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++
Muhammad Hammad Waseem
 
0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf
ssusere19c741
 
Cse115 lecture14strings part01
Cse115 lecture14strings part01Cse115 lecture14strings part01
Cse115 lecture14strings part01
Md. Ashikur Rahman
 
Lecture14.pdf
Lecture14.pdfLecture14.pdf
Lecture14.pdf
JoyPalit
 
Week6_P_String.pptx
Week6_P_String.pptxWeek6_P_String.pptx
Week6_P_String.pptx
OluwafolakeOjo
 
String notes
String notesString notes
String notes
Prasadu Peddi
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothi
Sowmya Jyothi
 
Array , Structure and Basic Algorithms.pptx
Array , Structure and Basic Algorithms.pptxArray , Structure and Basic Algorithms.pptx
Array , Structure and Basic Algorithms.pptx
MrNikhilMohanShinde
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
P M Patil
 
Lesson in Strings for C Programming Lessons
Lesson in Strings for C Programming LessonsLesson in Strings for C Programming Lessons
Lesson in Strings for C Programming Lessons
JamesChristianGadian
 
Lecture 17 - Strings
Lecture 17 - StringsLecture 17 - Strings
Lecture 17 - Strings
Md. Imran Hossain Showrov
 
string function with example...................
string function with example...................string function with example...................
string function with example...................
NishantsrivastavaV
 
14 strings
14 strings14 strings
14 strings
Rohit Shrivastava
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
웅식 전
 
String in c
String in cString in c
String in c
Suneel Dogra
 
week-7x
week-7xweek-7x
week-7x
KITE www.kitecolleges.com
 
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
AbhimanyuChaure
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
mikeymanjiro2090
 
Basic Algorithms and Array along with Structure.pptx
Basic Algorithms and Array along with Structure.pptxBasic Algorithms and Array along with Structure.pptx
Basic Algorithms and Array along with Structure.pptx
MrNikhilMohanShinde
 
0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf
ssusere19c741
 
Cse115 lecture14strings part01
Cse115 lecture14strings part01Cse115 lecture14strings part01
Cse115 lecture14strings part01
Md. Ashikur Rahman
 
Lecture14.pdf
Lecture14.pdfLecture14.pdf
Lecture14.pdf
JoyPalit
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothi
Sowmya Jyothi
 
Array , Structure and Basic Algorithms.pptx
Array , Structure and Basic Algorithms.pptxArray , Structure and Basic Algorithms.pptx
Array , Structure and Basic Algorithms.pptx
MrNikhilMohanShinde
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
P M Patil
 
Lesson in Strings for C Programming Lessons
Lesson in Strings for C Programming LessonsLesson in Strings for C Programming Lessons
Lesson in Strings for C Programming Lessons
JamesChristianGadian
 
string function with example...................
string function with example...................string function with example...................
string function with example...................
NishantsrivastavaV
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
웅식 전
 
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
AbhimanyuChaure
 

More from Dr. Chandrakant Divate (20)

Yoga and Mediation Lab manual Final for MSBTE Diploma Student
Yoga and Mediation Lab manual Final for MSBTE Diploma StudentYoga and Mediation Lab manual Final for MSBTE Diploma Student
Yoga and Mediation Lab manual Final for MSBTE Diploma Student
Dr. Chandrakant Divate
 
Perform sitting in Dhyan Mudra and meditating. Start with five minute and slo...
Perform sitting in Dhyan Mudra and meditating. Start with five minute and slo...Perform sitting in Dhyan Mudra and meditating. Start with five minute and slo...
Perform sitting in Dhyan Mudra and meditating. Start with five minute and slo...
Dr. Chandrakant Divate
 
Performing Akar, Omkar, Nadishuddhi, Bhastrika, Anulom Vilom, Kapalbhati, Bhr...
Performing Akar, Omkar, Nadishuddhi, Bhastrika, Anulom Vilom, Kapalbhati, Bhr...Performing Akar, Omkar, Nadishuddhi, Bhastrika, Anulom Vilom, Kapalbhati, Bhr...
Performing Akar, Omkar, Nadishuddhi, Bhastrika, Anulom Vilom, Kapalbhati, Bhr...
Dr. Chandrakant Divate
 
Experiment -7 Performing Asanas In Standing Position
Experiment -7 Performing Asanas In Standing PositionExperiment -7 Performing Asanas In Standing Position
Experiment -7 Performing Asanas In Standing Position
Dr. Chandrakant Divate
 
Experiment -6 Performing Asanas In Sitting Position
Experiment -6 Performing Asanas In Sitting PositionExperiment -6 Performing Asanas In Sitting Position
Experiment -6 Performing Asanas In Sitting Position
Dr. Chandrakant Divate
 
Performing Supine Position Asanas- Sleeping on your back.
Performing Supine Position Asanas- Sleeping on your back.Performing Supine Position Asanas- Sleeping on your back.
Performing Supine Position Asanas- Sleeping on your back.
Dr. Chandrakant Divate
 
Performing Prone Position Asanas- Sleeping on your back.
Performing Prone Position Asanas- Sleeping on your back.Performing Prone Position Asanas- Sleeping on your back.
Performing Prone Position Asanas- Sleeping on your back.
Dr. Chandrakant Divate
 
Perform all the postures of Surya Namaskar one by one in a very slow pace, af...
Perform all the postures of Surya Namaskar one by one in a very slow pace, af...Perform all the postures of Surya Namaskar one by one in a very slow pace, af...
Perform all the postures of Surya Namaskar one by one in a very slow pace, af...
Dr. Chandrakant Divate
 
Perform warming up exercises to prepare the body from head to toe for Yoga.
Perform warming up exercises to prepare the body from head to toe for Yoga.Perform warming up exercises to prepare the body from head to toe for Yoga.
Perform warming up exercises to prepare the body from head to toe for Yoga.
Dr. Chandrakant Divate
 
An introduction to yoga - Brief History and Publicity of Yoga in Universe
An introduction to yoga - Brief History and Publicity of Yoga in UniverseAn introduction to yoga - Brief History and Publicity of Yoga in Universe
An introduction to yoga - Brief History and Publicity of Yoga in Universe
Dr. Chandrakant Divate
 
Web Technology LAB MANUAL for Undergraduate Programs
Web Technology  LAB MANUAL for Undergraduate ProgramsWeb Technology  LAB MANUAL for Undergraduate Programs
Web Technology LAB MANUAL for Undergraduate Programs
Dr. Chandrakant Divate
 
UNIVERSAL HUMAN VALUES- Harmony in the Nature
UNIVERSAL HUMAN VALUES- Harmony in the NatureUNIVERSAL HUMAN VALUES- Harmony in the Nature
UNIVERSAL HUMAN VALUES- Harmony in the Nature
Dr. Chandrakant Divate
 
Study of Computer Hardware System using Block Diagram
Study of Computer Hardware System using Block DiagramStudy of Computer Hardware System using Block Diagram
Study of Computer Hardware System using Block Diagram
Dr. Chandrakant Divate
 
Computer System Output Devices Peripherals
Computer System Output  Devices PeripheralsComputer System Output  Devices Peripherals
Computer System Output Devices Peripherals
Dr. Chandrakant Divate
 
Computer system Input Devices Peripherals
Computer system Input  Devices PeripheralsComputer system Input  Devices Peripherals
Computer system Input Devices Peripherals
Dr. Chandrakant Divate
 
Computer system Input and Output Devices
Computer system Input and Output DevicesComputer system Input and Output Devices
Computer system Input and Output Devices
Dr. Chandrakant Divate
 
Introduction to COMPUTER’S MEMORY RAM and ROM
Introduction to COMPUTER’S MEMORY RAM and ROMIntroduction to COMPUTER’S MEMORY RAM and ROM
Introduction to COMPUTER’S MEMORY RAM and ROM
Dr. Chandrakant Divate
 
Introduction to Computer Hardware Systems
Introduction to Computer Hardware SystemsIntroduction to Computer Hardware Systems
Introduction to Computer Hardware Systems
Dr. Chandrakant Divate
 
Fundamentals of Internet of Things (IoT) Part-2
Fundamentals of Internet of Things (IoT) Part-2Fundamentals of Internet of Things (IoT) Part-2
Fundamentals of Internet of Things (IoT) Part-2
Dr. Chandrakant Divate
 
Fundamentals of Internet of Things (IoT)
Fundamentals of Internet of Things (IoT)Fundamentals of Internet of Things (IoT)
Fundamentals of Internet of Things (IoT)
Dr. Chandrakant Divate
 
Yoga and Mediation Lab manual Final for MSBTE Diploma Student
Yoga and Mediation Lab manual Final for MSBTE Diploma StudentYoga and Mediation Lab manual Final for MSBTE Diploma Student
Yoga and Mediation Lab manual Final for MSBTE Diploma Student
Dr. Chandrakant Divate
 
Perform sitting in Dhyan Mudra and meditating. Start with five minute and slo...
Perform sitting in Dhyan Mudra and meditating. Start with five minute and slo...Perform sitting in Dhyan Mudra and meditating. Start with five minute and slo...
Perform sitting in Dhyan Mudra and meditating. Start with five minute and slo...
Dr. Chandrakant Divate
 
Performing Akar, Omkar, Nadishuddhi, Bhastrika, Anulom Vilom, Kapalbhati, Bhr...
Performing Akar, Omkar, Nadishuddhi, Bhastrika, Anulom Vilom, Kapalbhati, Bhr...Performing Akar, Omkar, Nadishuddhi, Bhastrika, Anulom Vilom, Kapalbhati, Bhr...
Performing Akar, Omkar, Nadishuddhi, Bhastrika, Anulom Vilom, Kapalbhati, Bhr...
Dr. Chandrakant Divate
 
Experiment -7 Performing Asanas In Standing Position
Experiment -7 Performing Asanas In Standing PositionExperiment -7 Performing Asanas In Standing Position
Experiment -7 Performing Asanas In Standing Position
Dr. Chandrakant Divate
 
Experiment -6 Performing Asanas In Sitting Position
Experiment -6 Performing Asanas In Sitting PositionExperiment -6 Performing Asanas In Sitting Position
Experiment -6 Performing Asanas In Sitting Position
Dr. Chandrakant Divate
 
Performing Supine Position Asanas- Sleeping on your back.
Performing Supine Position Asanas- Sleeping on your back.Performing Supine Position Asanas- Sleeping on your back.
Performing Supine Position Asanas- Sleeping on your back.
Dr. Chandrakant Divate
 
Performing Prone Position Asanas- Sleeping on your back.
Performing Prone Position Asanas- Sleeping on your back.Performing Prone Position Asanas- Sleeping on your back.
Performing Prone Position Asanas- Sleeping on your back.
Dr. Chandrakant Divate
 
Perform all the postures of Surya Namaskar one by one in a very slow pace, af...
Perform all the postures of Surya Namaskar one by one in a very slow pace, af...Perform all the postures of Surya Namaskar one by one in a very slow pace, af...
Perform all the postures of Surya Namaskar one by one in a very slow pace, af...
Dr. Chandrakant Divate
 
Perform warming up exercises to prepare the body from head to toe for Yoga.
Perform warming up exercises to prepare the body from head to toe for Yoga.Perform warming up exercises to prepare the body from head to toe for Yoga.
Perform warming up exercises to prepare the body from head to toe for Yoga.
Dr. Chandrakant Divate
 
An introduction to yoga - Brief History and Publicity of Yoga in Universe
An introduction to yoga - Brief History and Publicity of Yoga in UniverseAn introduction to yoga - Brief History and Publicity of Yoga in Universe
An introduction to yoga - Brief History and Publicity of Yoga in Universe
Dr. Chandrakant Divate
 
Web Technology LAB MANUAL for Undergraduate Programs
Web Technology  LAB MANUAL for Undergraduate ProgramsWeb Technology  LAB MANUAL for Undergraduate Programs
Web Technology LAB MANUAL for Undergraduate Programs
Dr. Chandrakant Divate
 
UNIVERSAL HUMAN VALUES- Harmony in the Nature
UNIVERSAL HUMAN VALUES- Harmony in the NatureUNIVERSAL HUMAN VALUES- Harmony in the Nature
UNIVERSAL HUMAN VALUES- Harmony in the Nature
Dr. Chandrakant Divate
 
Study of Computer Hardware System using Block Diagram
Study of Computer Hardware System using Block DiagramStudy of Computer Hardware System using Block Diagram
Study of Computer Hardware System using Block Diagram
Dr. Chandrakant Divate
 
Computer System Output Devices Peripherals
Computer System Output  Devices PeripheralsComputer System Output  Devices Peripherals
Computer System Output Devices Peripherals
Dr. Chandrakant Divate
 
Computer system Input Devices Peripherals
Computer system Input  Devices PeripheralsComputer system Input  Devices Peripherals
Computer system Input Devices Peripherals
Dr. Chandrakant Divate
 
Computer system Input and Output Devices
Computer system Input and Output DevicesComputer system Input and Output Devices
Computer system Input and Output Devices
Dr. Chandrakant Divate
 
Introduction to COMPUTER’S MEMORY RAM and ROM
Introduction to COMPUTER’S MEMORY RAM and ROMIntroduction to COMPUTER’S MEMORY RAM and ROM
Introduction to COMPUTER’S MEMORY RAM and ROM
Dr. Chandrakant Divate
 
Introduction to Computer Hardware Systems
Introduction to Computer Hardware SystemsIntroduction to Computer Hardware Systems
Introduction to Computer Hardware Systems
Dr. Chandrakant Divate
 
Fundamentals of Internet of Things (IoT) Part-2
Fundamentals of Internet of Things (IoT) Part-2Fundamentals of Internet of Things (IoT) Part-2
Fundamentals of Internet of Things (IoT) Part-2
Dr. Chandrakant Divate
 
Fundamentals of Internet of Things (IoT)
Fundamentals of Internet of Things (IoT)Fundamentals of Internet of Things (IoT)
Fundamentals of Internet of Things (IoT)
Dr. Chandrakant Divate
 
Ad

Recently uploaded (20)

DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
charlesdick1345
 
Data Structures_Linear data structures Linked Lists.pptx
Data Structures_Linear data structures Linked Lists.pptxData Structures_Linear data structures Linked Lists.pptx
Data Structures_Linear data structures Linked Lists.pptx
RushaliDeshmukh2
 
How to use nRF24L01 module with Arduino
How to use nRF24L01 module with ArduinoHow to use nRF24L01 module with Arduino
How to use nRF24L01 module with Arduino
CircuitDigest
 
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Journal of Soft Computing in Civil Engineering
 
Data Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptxData Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptx
RushaliDeshmukh2
 
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdfMAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
ssuser562df4
 
IntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdfIntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdf
Luiz Carneiro
 
theory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptxtheory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptx
sanchezvanessa7896
 
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Journal of Soft Computing in Civil Engineering
 
15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...
IJCSES Journal
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
Oil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdfOil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdf
M7md3li2
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
Data Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptxData Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptx
RushaliDeshmukh2
 
New Microsoft PowerPoint Presentation.pdf
New Microsoft PowerPoint Presentation.pdfNew Microsoft PowerPoint Presentation.pdf
New Microsoft PowerPoint Presentation.pdf
mohamedezzat18803
 
Resistance measurement and cfd test on darpa subboff model
Resistance measurement and cfd test on darpa subboff modelResistance measurement and cfd test on darpa subboff model
Resistance measurement and cfd test on darpa subboff model
INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR
 
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G..."Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
Infopitaara
 
some basics electrical and electronics knowledge
some basics electrical and electronics knowledgesome basics electrical and electronics knowledge
some basics electrical and electronics knowledge
nguyentrungdo88
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
charlesdick1345
 
Data Structures_Linear data structures Linked Lists.pptx
Data Structures_Linear data structures Linked Lists.pptxData Structures_Linear data structures Linked Lists.pptx
Data Structures_Linear data structures Linked Lists.pptx
RushaliDeshmukh2
 
How to use nRF24L01 module with Arduino
How to use nRF24L01 module with ArduinoHow to use nRF24L01 module with Arduino
How to use nRF24L01 module with Arduino
CircuitDigest
 
Data Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptxData Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptx
RushaliDeshmukh2
 
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdfMAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
ssuser562df4
 
IntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdfIntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdf
Luiz Carneiro
 
theory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptxtheory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptx
sanchezvanessa7896
 
15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...
IJCSES Journal
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
Oil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdfOil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdf
M7md3li2
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
Data Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptxData Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptx
RushaliDeshmukh2
 
New Microsoft PowerPoint Presentation.pdf
New Microsoft PowerPoint Presentation.pdfNew Microsoft PowerPoint Presentation.pdf
New Microsoft PowerPoint Presentation.pdf
mohamedezzat18803
 
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G..."Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
Infopitaara
 
some basics electrical and electronics knowledge
some basics electrical and electronics knowledgesome basics electrical and electronics knowledge
some basics electrical and electronics knowledge
nguyentrungdo88
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
Ad

Programming in C - Fundamental Study of Strings

  • 1. Strings Introduction Reading and displaying strings Passing strings to function String handling functions C. P. Divate
  • 2. • Strings are array of characters i.e. they are arranged one after another in Thus, a character array is called characters memory. 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 include: – Reading and writing strings – Copying one string to another – Combining strings together – Comparing strings for equality – Extracting a portion of a string character strings 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[]=" ofApplied"; 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 byASCII 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 • WAPto 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