SlideShare a Scribd company logo
1 of 22
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

Looping statements in C
Looping statements in CLooping statements in C
Looping statements in C
Jeya Lakshmi
 
Presentation on array
Presentation on array Presentation on array
Presentation on array
topu93
 

What's hot (20)

Array in c
Array in cArray in c
Array in c
 
Structure in C
Structure in CStructure in C
Structure in C
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
 
Looping statements in C
Looping statements in CLooping statements in C
Looping statements in C
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
 
C string
C stringC string
C string
 
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
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming Language
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
 
String C Programming
String C ProgrammingString C Programming
String C Programming
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
Variables in C Programming
Variables in C ProgrammingVariables in C Programming
Variables in C Programming
 
Array Of Pointers
Array Of PointersArray Of Pointers
Array Of Pointers
 
Presentation on array
Presentation on array Presentation on array
Presentation on array
 

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.pdf
HEMAHEMS5
 
Array , Structure and Basic Algorithms.pptx
Array , Structure and Basic Algorithms.pptxArray , Structure and Basic Algorithms.pptx
Array , Structure and Basic Algorithms.pptx
MrNikhilMohanShinde
 

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
 
Module-2_Strings concepts in c programming
Module-2_Strings concepts in c programmingModule-2_Strings concepts in c programming
Module-2_Strings concepts in 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
 

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

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 

Recently uploaded (20)

Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 

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