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)

Generative AI & Large Language Models Agents
Generative AI & Large Language Models AgentsGenerative AI & Large Language Models Agents
Generative AI & Large Language Models Agents
aasgharbee22seecs
 
22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
Guru Nanak Technical Institutions
 
Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
AI Publications
 
2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt
rakshaiya16
 
Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025
Antonin Danalet
 
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdfLittle Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
gori42199
 
JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...
JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...
JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...
Reflections on Morality, Philosophy, and History
 
Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control Monthly May 2025Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
Evonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdfEvonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdf
szhang13
 
Using the Artificial Neural Network to Predict the Axial Strength and Strain ...
Using the Artificial Neural Network to Predict the Axial Strength and Strain ...Using the Artificial Neural Network to Predict the Axial Strength and Strain ...
Using the Artificial Neural Network to Predict the Axial Strength and Strain ...
Journal of Soft Computing in Civil Engineering
 
Construction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil EngineeringConstruction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil Engineering
Lavish Kashyap
 
acid base ppt and their specific application in food
acid base ppt and their specific application in foodacid base ppt and their specific application in food
acid base ppt and their specific application in food
Fatehatun Noor
 
Autodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User InterfaceAutodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User Interface
Atif Razi
 
Lecture - 7 Canals of the topic of the civil engineering
Lecture - 7  Canals of the topic of the civil engineeringLecture - 7  Canals of the topic of the civil engineering
Lecture - 7 Canals of the topic of the civil engineering
MJawadkhan1
 
Control Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptxControl Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptx
vvsasane
 
DED KOMINFO detail engginering design gedung
DED KOMINFO detail engginering design gedungDED KOMINFO detail engginering design gedung
DED KOMINFO detail engginering design gedung
nabilarizqifadhilah1
 
Design Optimization of Reinforced Concrete Waffle Slab Using Genetic Algorithm
Design Optimization of Reinforced Concrete Waffle Slab Using Genetic AlgorithmDesign Optimization of Reinforced Concrete Waffle Slab Using Genetic Algorithm
Design Optimization of Reinforced Concrete Waffle Slab Using Genetic Algorithm
Journal of Soft Computing in Civil Engineering
 
Slide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptxSlide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptx
vvsasane
 
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdfATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ssuserda39791
 
Generative AI & Large Language Models Agents
Generative AI & Large Language Models AgentsGenerative AI & Large Language Models Agents
Generative AI & Large Language Models Agents
aasgharbee22seecs
 
Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
AI Publications
 
2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt
rakshaiya16
 
Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025
Antonin Danalet
 
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdfLittle Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
gori42199
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
Evonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdfEvonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdf
szhang13
 
Construction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil EngineeringConstruction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil Engineering
Lavish Kashyap
 
acid base ppt and their specific application in food
acid base ppt and their specific application in foodacid base ppt and their specific application in food
acid base ppt and their specific application in food
Fatehatun Noor
 
Autodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User InterfaceAutodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User Interface
Atif Razi
 
Lecture - 7 Canals of the topic of the civil engineering
Lecture - 7  Canals of the topic of the civil engineeringLecture - 7  Canals of the topic of the civil engineering
Lecture - 7 Canals of the topic of the civil engineering
MJawadkhan1
 
Control Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptxControl Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptx
vvsasane
 
DED KOMINFO detail engginering design gedung
DED KOMINFO detail engginering design gedungDED KOMINFO detail engginering design gedung
DED KOMINFO detail engginering design gedung
nabilarizqifadhilah1
 
Slide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptxSlide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptx
vvsasane
 
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdfATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ssuserda39791
 
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
  翻译: