SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

String functions in C
String functions in CString functions in C
String functions in C
 
Arrays
ArraysArrays
Arrays
 
Array in c++
Array in c++Array in c++
Array in c++
 
Strings in c
Strings in cStrings in c
Strings in c
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
 
Structure in C
Structure in CStructure in C
Structure in C
 
arrays and pointers
arrays and pointersarrays and pointers
arrays and pointers
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION 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
 
Functions in C
Functions in CFunctions in C
Functions in C
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
 
Data types in C
Data types in CData types in C
Data types in C
 
Strings in python
Strings in pythonStrings in python
Strings in python
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
Enumerated data types in C
Enumerated data types in CEnumerated data types in C
Enumerated data types in C
 
Array and string
Array and stringArray and string
Array and string
 

Andere mochten auch (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
 

Ähnlich wie 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
 

Ähnlich wie 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
 

Mehr von Kamal Acharya

Mehr von 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
 

Kürzlich hochgeladen

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
SanaAli374401
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
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
 

Kürzlich hochgeladen (20)

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
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
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
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
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
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
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...
 
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
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
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
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
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
 
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...
 

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