SlideShare a Scribd company logo
Lecture 14
Strings
CSE115: Computing Concepts
Fundamentals of Characters and Strings
• Characters in C consist of any printable or nonprintable character in
the computer’s character set including lowercase letters, uppercase
letters, decimal digits, special characters and escape sequences.
• A character is usually stored in the computer as an 8-bits (1 byte)
integer.
• The integer value stored for a character depends on the character
set used by the computer on which the program is running.
• There are two commonly used character sets:
• ASCII (American Standard Code for Information Interchange)
• EBCDIC (Extended Binary Coded Decimal Interchange Code)
Difference Between an Integer Digit and a
Character Digit
• char num = 1 and char num = ‘1’ are not the same.
• char num = 1 is represented in the computer as
00000001.
• char num = ‘1’ on the other hand is number 49 according
to the ASCII character set. Therefore, it is represented in
the computer as 00110001.
Example: ASCII Character
#include <stdio.h>
void main(void)
{
char my_A = 'A';
char my_Z = 'Z';
char my_a = 'a';
char my_z = 'z';
printf("nASCII value for A is %d", my_A);
printf("nASCII value for Z is %d",my_Z);
printf("nASCII value for a is %d", my_a);
printf("nASCII value for z is %d",my_z);
printf("n");
printf("n65 in ASCII represents %c",65);
printf("n90 in ASCII represents %c",90);
printf("n97 in ASCII represents %c",97);
printf("n122 in ASCII represents %c",122);
}
Sample Output
ASCII value for A is 65
ASCII value for Z is 90
ASCII value for a is 97
ASCII value for z is 122
65 in ASCII represents A
90 in ASCII represents Z
97 in ASCII represents a
122 in ASCII represents z
Example
#include <stdio.h>
int main()
{
char ch;
printf("enter a character: ");
scanf("%c", &ch);
if (ch >= ‘A’ && ch <= ‘Z’)
printf("ncapital lettern");
return 0;
}
equivalent to
#include <stdio.h>
int main()
{
char ch;
printf("enter a character: ");
scanf("%c", &ch);
if (ch >= 65 && ch <= 90)
printf("ncapital lettern");
return 0;
}
String Declaration and Initialization
• A string in C is an array of characters ending with the null character
(‘0’). It is written inside a double quotation mark (“ ”)
• A string can be assigned (in a declaration) to a char array:
• char color[6] = “green”;
color
0 1 2 3 4 5
‘g’ ‘r’ ‘e’ ‘e’ ‘n’ ‘0’
String Declaration and Initialization
• A string can also be defined by specifying the individual characters:
• char color[ ] = {‘g’, ‘r’, ‘e’, ‘e’, ‘n’, ‘0’};
color
0 1 2 3 4 5
‘g’ ‘r’ ‘e’ ‘e’ ‘n’ ‘0’
String Declaration and Initialization
• Notice that even though there are only five characters in the
word ‘green’, six characters are stored in the computer. The
last character, the character ‘0’, is the NULL character which
indicates the end of the string.
• Therefore, if an array of characters is to be used to store a
string, the array must be large enough to store the string and
its terminating NULL character.
String Declaration and Initialization
• If we happen to declare a string like this:
char my_drink[3] = “tea”;
• We will get the following syntax error:
error C2117: 'tea' : array bounds overflow
• Instead, we need to at least declare the array with (the size of
the string + 1) to accommodate the null terminating character
‘0’.
char my_drink[4] = “tea”;
String Declaration and Initialization
• We can initialize string variables at compile time such as;
• char name[10] = “Arris”;
• This initialization creates the following spaces in storage :
0 1 2 3 4 5 6 7 8 9
‘A’ ‘r’ ‘r’ ‘i’ ‘s’ ‘0’ ‘0’ ‘0’ ‘0’ ‘0’
name
Example: String and ‘0’
#include <stdio.h>
void main()
{
char sentence[] = "I love Bangladesh";
int i, count = 0;
for (i = 0; sentence[i] != '0'; i++)
count++;
printf(“%s has %d characters including the whitespace",
sentence, count);
}
Example: String and ‘0’
#include <stdio.h>
void main()
{
char sentence[] = "I love Bangladesh";
int i, count = 0;
for (i = 0; sentence[i] != '0'; i++)
count++;
printf(“%s has %d characters including the whitespace",
sentence, count);
}
Sample output:
I love Bangladesh has 15 characters including the whitespace
String Input/Output Functions
• Standard Functions Input
• scanf( )
• gets( )
• Standard Functions Output
• printf( )
• puts( )
• Use scanf function together with the format specifier %s for
interactive input string. (no whitespace character)
• If the string to be read as an input has embedded whitespace
characters, use standard gets function.
Example: gets, puts, scanf and
printf
#include <stdio.h>
int main()
{
char string1[50];
char string2[50];
printf("Enter a string less than 50 characters with
spaces: n");
gets(string1);
printf("nYou have entered: ");
puts(string1);
printf("nTry entering a string less than 50
characters, with spaces: n");
scanf("%s", string2);
printf("nYou have entered: %sn", string2);
return 0;
}
Example: gets, puts, scanf and
printf
Sample output
Enter a string less than 50 characters with spaces:
hello world
You have entered: hello world
Try entering a string less than 50 characters, with spaces:
hello world
You have entered: hello
Character Handling Library
• Character handling library includes several function that perform
useful tests and manipulation of character data.
• Each function receives a character, represented as an int or EOF, as
an argument.
• When using functions from the character handling library, the
header file <ctype.h> needs to be included.
• Characters in these functions are manipulated as integers (since a
character is basically a 1 byte integer).
Functions in <ctype.h>
Prototype Function Descriptions
int isdigit(int c) Returns a true if value c is a digit, and 0 (false) otherwise.
int isalpha(int c) Returns a true if value c is a letter, and 0 otherwise.
int isalnum(int c) Returns a true if value c is a digit or a letter, and 0 otherwise.
int isxdigit(int c) Returns a true value if c is a hexadecimal digit character, and 0 otherwise.
int islower(int c) Returns a true value if c is a lowercase letter, and 0 otherwise.
int isupper(int c) Returns a true value if c is an uppercase letter, and 0 otherwise.
int tolower(int c) If c is an uppercase letter, tolower returns c as a lowercase letter. Otherwise, tolower returns the
argument unchanged.
int toupper(int c) If c is a lowercase letter, toupper returns c as an uppercase letter. Otherwise toupper returns the
argument unchanged.
int isspace(int c) Returns true if c is a white space character – newline (‘n’), space (‘ ’), form feed (‘f’), carriage
return (‘r’), horizontal tab (‘t’) or vertical tab (‘v’) – and 0 otherwise.
int iscntrl(int c) Returns a true if c is a control character, and 0 otherwise.
int ispunct(int c) Returns a true if c is a printing character other than a space, a digit or a letter, and 0 otherwise.
int isprint(int c) Returns a true value if c is a printing character including space (‘ ’), and 0 otherwise.
int isgraph(int c) Returns a true value if c is a printing character other than space (‘ ’), and 0 otherwise.
String Conversion Functions
• These functions convert strings of digits to integer and floating-
point values.
• To use these functions, the general utilities library <stdlib.h>,
needs to be included.
• Example:
• atoi: string to int
• atof: string to double
Example
/*1. Converting a String Into an int Using
atoi. */
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str1[ ] = "124z3yu87";
char str2[ ] = "-3.4";
char str3[ ] = "e24";
int i1 = atoi(str1), i2 = atoi(str2),
i3 = atoi(str3);
printf("i1: %dn", i1);
printf("i2: %dn", i2);
printf("i3: %dn", i3);
return 0;
}
Example
/*1. Converting a String Into an int Using
atoi. */
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str1[ ] = "124z3yu87";
char str2[ ] = "-3.4";
char str3[ ] = "e24";
int i1 = atoi(str1), i2 = atoi(str2),
i3 = atoi(str3);
printf("i1: %dn", i1);
printf("i2: %dn", i2);
printf("i3: %dn", i3);
return 0;
}
Output:
i1: 124
i2: -3
i3: 0

More Related Content

What's hot

String in c
String in cString in c
String in c
Suneel Dogra
 
C++ string
C++ stringC++ string
C++ string
Dheenadayalan18
 
Strings in c language
Strings in  c languageStrings in  c language
Strings in c language
Infinity Tech Solutions
 
strings in c language and its importance
strings in c language and its importancestrings in c language and its importance
strings in c language and its importance
sirmanohar
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
Rakesh Roshan
 
Strings in c
Strings in cStrings in c
Strings in c
vampugani
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
Appili Vamsi Krishna
 
Strings in C
Strings in CStrings in C
Strings in C
Kamal Acharya
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
P M Patil
 
String C Programming
String C ProgrammingString C Programming
String C Programming
Prionto Abdullah
 
Arrays and strings in c++
Arrays and strings in c++Arrays and strings in c++
Arrays and strings in c++
GC University Faisalabad
 
Strings Functions in C Programming
Strings Functions in C ProgrammingStrings Functions in C Programming
Strings Functions in C Programming
DevoAjit Gupta
 
String in c programming
String in c programmingString in c programming
String in c programming
Devan Thakur
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
Neeru Mittal
 
C Programming Unit-3
C Programming Unit-3C Programming Unit-3
C Programming Unit-3
Vikram Nandini
 
The string class
The string classThe string class
The string class
Syed Zaid Irshad
 
C string
C stringC string
String c
String cString c

What's hot (20)

String in c
String in cString in c
String in c
 
C++ string
C++ stringC++ string
C++ string
 
Strings in c language
Strings in  c languageStrings in  c language
Strings in c language
 
strings in c language and its importance
strings in c language and its importancestrings in c language and its importance
strings in c language and its importance
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
 
Strings in c
Strings in cStrings in c
Strings in c
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
 
Strings in C
Strings in CStrings in C
Strings in C
 
Strings
StringsStrings
Strings
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
 
String C Programming
String C ProgrammingString C Programming
String C Programming
 
Arrays and strings in c++
Arrays and strings in c++Arrays and strings in c++
Arrays and strings in c++
 
Strings Functions in C Programming
Strings Functions in C ProgrammingStrings Functions in C Programming
Strings Functions in C Programming
 
String in c programming
String in c programmingString in c programming
String in c programming
 
Savitch Ch 08
Savitch Ch 08Savitch Ch 08
Savitch Ch 08
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
C Programming Unit-3
C Programming Unit-3C Programming Unit-3
C Programming Unit-3
 
The string class
The string classThe string class
The string class
 
C string
C stringC string
C string
 
String c
String cString c
String c
 

Viewers also liked

Lec 3 of bio chemistry of life
Lec 3 of bio chemistry of lifeLec 3 of bio chemistry of life
Lec 3 of bio chemistry of life
Md. Ashikur Rahman
 
Cse115 lecture08repetitionstructures part02
Cse115 lecture08repetitionstructures part02Cse115 lecture08repetitionstructures part02
Cse115 lecture08repetitionstructures part02
Md. Ashikur Rahman
 
Cse115 lecture01numbersystems
Cse115 lecture01numbersystemsCse115 lecture01numbersystems
Cse115 lecture01numbersystems
Md. Ashikur Rahman
 
Cse115 lecture02overviewofprogramming
Cse115 lecture02overviewofprogrammingCse115 lecture02overviewofprogramming
Cse115 lecture02overviewofprogramming
Md. Ashikur Rahman
 
Cse115 lecture04introtoc programming
Cse115 lecture04introtoc programmingCse115 lecture04introtoc programming
Cse115 lecture04introtoc programming
Md. Ashikur Rahman
 
Investment In Stock Market
Investment In Stock MarketInvestment In Stock Market
Investment In Stock Market
kamal_jalal
 
Internet Trading Service
Internet Trading ServiceInternet Trading Service
Internet Trading Service
kamal_jalal
 
Cse115 lecture03problemsolving
Cse115 lecture03problemsolvingCse115 lecture03problemsolving
Cse115 lecture03problemsolving
Md. Ashikur Rahman
 

Viewers also liked (8)

Lec 3 of bio chemistry of life
Lec 3 of bio chemistry of lifeLec 3 of bio chemistry of life
Lec 3 of bio chemistry of life
 
Cse115 lecture08repetitionstructures part02
Cse115 lecture08repetitionstructures part02Cse115 lecture08repetitionstructures part02
Cse115 lecture08repetitionstructures part02
 
Cse115 lecture01numbersystems
Cse115 lecture01numbersystemsCse115 lecture01numbersystems
Cse115 lecture01numbersystems
 
Cse115 lecture02overviewofprogramming
Cse115 lecture02overviewofprogrammingCse115 lecture02overviewofprogramming
Cse115 lecture02overviewofprogramming
 
Cse115 lecture04introtoc programming
Cse115 lecture04introtoc programmingCse115 lecture04introtoc programming
Cse115 lecture04introtoc programming
 
Investment In Stock Market
Investment In Stock MarketInvestment In Stock Market
Investment In Stock Market
 
Internet Trading Service
Internet Trading ServiceInternet Trading Service
Internet Trading Service
 
Cse115 lecture03problemsolving
Cse115 lecture03problemsolvingCse115 lecture03problemsolving
Cse115 lecture03problemsolving
 

Similar to Cse115 lecture14strings part01

Presentation more c_programmingcharacter_and_string_handling_
Presentation more c_programmingcharacter_and_string_handling_Presentation more c_programmingcharacter_and_string_handling_
Presentation more c_programmingcharacter_and_string_handling_
KarthicaMarasamy
 
fundamentals of c programming_String.pptx
fundamentals of c programming_String.pptxfundamentals of c programming_String.pptx
fundamentals of c programming_String.pptx
JStalinAsstProfessor
 
Computer Programming- Lecture 5
Computer Programming- Lecture 5 Computer Programming- Lecture 5
Computer Programming- Lecture 5
Dr. Md. Shohel Sayeed
 
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
 
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
 
Learn C LANGUAGE at ASIT
Learn C LANGUAGE at ASITLearn C LANGUAGE at ASIT
Learn C LANGUAGE at ASIT
ASIT
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
mikeymanjiro2090
 
C language
C languageC language
C language
TaranjeetKaur72
 
Programming in C - Fundamental Study of Strings
Programming in C - Fundamental Study of  StringsProgramming in C - Fundamental Study of  Strings
Programming in C - Fundamental Study of Strings
Chandrakant Divate
 
Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3
karmuhtam
 
Arrays & Strings.pptx
Arrays & Strings.pptxArrays & Strings.pptx
Arrays & Strings.pptx
AnkurRajSingh2
 
0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf
ssusere19c741
 
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
CHAITRAB29
 
strings-150319180934-conversion-gate01.pdf
strings-150319180934-conversion-gate01.pdfstrings-150319180934-conversion-gate01.pdf
strings-150319180934-conversion-gate01.pdf
HEMAHEMS5
 
Type header file in c++ and its function
Type header file in c++ and its functionType header file in c++ and its function
Type header file in c++ and its functionFrankie Jones
 
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdfSTRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
CPSTRINGSARGAVISTRINGS.PPT
CPSTRINGSARGAVISTRINGS.PPTCPSTRINGSARGAVISTRINGS.PPT
CPSTRINGSARGAVISTRINGS.PPT
Sasideepa
 
BHARGAVISTRINGS.PPT
BHARGAVISTRINGS.PPTBHARGAVISTRINGS.PPT
BHARGAVISTRINGS.PPT
Sasideepa
 

Similar to Cse115 lecture14strings part01 (20)

Presentation more c_programmingcharacter_and_string_handling_
Presentation more c_programmingcharacter_and_string_handling_Presentation more c_programmingcharacter_and_string_handling_
Presentation more c_programmingcharacter_and_string_handling_
 
fundamentals of c programming_String.pptx
fundamentals of c programming_String.pptxfundamentals of c programming_String.pptx
fundamentals of c programming_String.pptx
 
Computer Programming- Lecture 5
Computer Programming- Lecture 5 Computer Programming- Lecture 5
Computer Programming- Lecture 5
 
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
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothi
 
Learn C LANGUAGE at ASIT
Learn C LANGUAGE at ASITLearn C LANGUAGE at ASIT
Learn C LANGUAGE at ASIT
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
 
C language
C languageC language
C language
 
Ch09
Ch09Ch09
Ch09
 
Programming in C - Fundamental Study of Strings
Programming in C - Fundamental Study of  StringsProgramming in C - Fundamental Study of  Strings
Programming in C - Fundamental Study of Strings
 
Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3
 
Arrays & Strings.pptx
Arrays & Strings.pptxArrays & Strings.pptx
Arrays & Strings.pptx
 
0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf
 
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
 
strings-150319180934-conversion-gate01.pdf
strings-150319180934-conversion-gate01.pdfstrings-150319180934-conversion-gate01.pdf
strings-150319180934-conversion-gate01.pdf
 
Type header file in c++ and its function
Type header file in c++ and its functionType header file in c++ and its function
Type header file in c++ and its function
 
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdfSTRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
 
Karakter dan String
Karakter dan StringKarakter dan String
Karakter dan String
 
CPSTRINGSARGAVISTRINGS.PPT
CPSTRINGSARGAVISTRINGS.PPTCPSTRINGSARGAVISTRINGS.PPT
CPSTRINGSARGAVISTRINGS.PPT
 
BHARGAVISTRINGS.PPT
BHARGAVISTRINGS.PPTBHARGAVISTRINGS.PPT
BHARGAVISTRINGS.PPT
 

Recently uploaded

Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
Kamal Acharya
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
ShahidSultan24
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
ankuprajapati0525
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
abh.arya
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
PrashantGoswami42
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
DuvanRamosGarzon1
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
ssuser9bd3ba
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 

Recently uploaded (20)

Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 

Cse115 lecture14strings part01

  • 2. Fundamentals of Characters and Strings • Characters in C consist of any printable or nonprintable character in the computer’s character set including lowercase letters, uppercase letters, decimal digits, special characters and escape sequences. • A character is usually stored in the computer as an 8-bits (1 byte) integer. • The integer value stored for a character depends on the character set used by the computer on which the program is running. • There are two commonly used character sets: • ASCII (American Standard Code for Information Interchange) • EBCDIC (Extended Binary Coded Decimal Interchange Code)
  • 3. Difference Between an Integer Digit and a Character Digit • char num = 1 and char num = ‘1’ are not the same. • char num = 1 is represented in the computer as 00000001. • char num = ‘1’ on the other hand is number 49 according to the ASCII character set. Therefore, it is represented in the computer as 00110001.
  • 4. Example: ASCII Character #include <stdio.h> void main(void) { char my_A = 'A'; char my_Z = 'Z'; char my_a = 'a'; char my_z = 'z'; printf("nASCII value for A is %d", my_A); printf("nASCII value for Z is %d",my_Z); printf("nASCII value for a is %d", my_a); printf("nASCII value for z is %d",my_z); printf("n"); printf("n65 in ASCII represents %c",65); printf("n90 in ASCII represents %c",90); printf("n97 in ASCII represents %c",97); printf("n122 in ASCII represents %c",122); }
  • 5. Sample Output ASCII value for A is 65 ASCII value for Z is 90 ASCII value for a is 97 ASCII value for z is 122 65 in ASCII represents A 90 in ASCII represents Z 97 in ASCII represents a 122 in ASCII represents z
  • 6. Example #include <stdio.h> int main() { char ch; printf("enter a character: "); scanf("%c", &ch); if (ch >= ‘A’ && ch <= ‘Z’) printf("ncapital lettern"); return 0; } equivalent to #include <stdio.h> int main() { char ch; printf("enter a character: "); scanf("%c", &ch); if (ch >= 65 && ch <= 90) printf("ncapital lettern"); return 0; }
  • 7. String Declaration and Initialization • A string in C is an array of characters ending with the null character (‘0’). It is written inside a double quotation mark (“ ”) • A string can be assigned (in a declaration) to a char array: • char color[6] = “green”; color 0 1 2 3 4 5 ‘g’ ‘r’ ‘e’ ‘e’ ‘n’ ‘0’
  • 8. String Declaration and Initialization • A string can also be defined by specifying the individual characters: • char color[ ] = {‘g’, ‘r’, ‘e’, ‘e’, ‘n’, ‘0’}; color 0 1 2 3 4 5 ‘g’ ‘r’ ‘e’ ‘e’ ‘n’ ‘0’
  • 9. String Declaration and Initialization • Notice that even though there are only five characters in the word ‘green’, six characters are stored in the computer. The last character, the character ‘0’, is the NULL character which indicates the end of the string. • Therefore, if an array of characters is to be used to store a string, the array must be large enough to store the string and its terminating NULL character.
  • 10. String Declaration and Initialization • If we happen to declare a string like this: char my_drink[3] = “tea”; • We will get the following syntax error: error C2117: 'tea' : array bounds overflow • Instead, we need to at least declare the array with (the size of the string + 1) to accommodate the null terminating character ‘0’. char my_drink[4] = “tea”;
  • 11. String Declaration and Initialization • We can initialize string variables at compile time such as; • char name[10] = “Arris”; • This initialization creates the following spaces in storage : 0 1 2 3 4 5 6 7 8 9 ‘A’ ‘r’ ‘r’ ‘i’ ‘s’ ‘0’ ‘0’ ‘0’ ‘0’ ‘0’ name
  • 12. Example: String and ‘0’ #include <stdio.h> void main() { char sentence[] = "I love Bangladesh"; int i, count = 0; for (i = 0; sentence[i] != '0'; i++) count++; printf(“%s has %d characters including the whitespace", sentence, count); }
  • 13. Example: String and ‘0’ #include <stdio.h> void main() { char sentence[] = "I love Bangladesh"; int i, count = 0; for (i = 0; sentence[i] != '0'; i++) count++; printf(“%s has %d characters including the whitespace", sentence, count); } Sample output: I love Bangladesh has 15 characters including the whitespace
  • 14. String Input/Output Functions • Standard Functions Input • scanf( ) • gets( ) • Standard Functions Output • printf( ) • puts( ) • Use scanf function together with the format specifier %s for interactive input string. (no whitespace character) • If the string to be read as an input has embedded whitespace characters, use standard gets function.
  • 15. Example: gets, puts, scanf and printf #include <stdio.h> int main() { char string1[50]; char string2[50]; printf("Enter a string less than 50 characters with spaces: n"); gets(string1); printf("nYou have entered: "); puts(string1); printf("nTry entering a string less than 50 characters, with spaces: n"); scanf("%s", string2); printf("nYou have entered: %sn", string2); return 0; }
  • 16. Example: gets, puts, scanf and printf Sample output Enter a string less than 50 characters with spaces: hello world You have entered: hello world Try entering a string less than 50 characters, with spaces: hello world You have entered: hello
  • 17. Character Handling Library • Character handling library includes several function that perform useful tests and manipulation of character data. • Each function receives a character, represented as an int or EOF, as an argument. • When using functions from the character handling library, the header file <ctype.h> needs to be included. • Characters in these functions are manipulated as integers (since a character is basically a 1 byte integer).
  • 18. Functions in <ctype.h> Prototype Function Descriptions int isdigit(int c) Returns a true if value c is a digit, and 0 (false) otherwise. int isalpha(int c) Returns a true if value c is a letter, and 0 otherwise. int isalnum(int c) Returns a true if value c is a digit or a letter, and 0 otherwise. int isxdigit(int c) Returns a true value if c is a hexadecimal digit character, and 0 otherwise. int islower(int c) Returns a true value if c is a lowercase letter, and 0 otherwise. int isupper(int c) Returns a true value if c is an uppercase letter, and 0 otherwise. int tolower(int c) If c is an uppercase letter, tolower returns c as a lowercase letter. Otherwise, tolower returns the argument unchanged. int toupper(int c) If c is a lowercase letter, toupper returns c as an uppercase letter. Otherwise toupper returns the argument unchanged. int isspace(int c) Returns true if c is a white space character – newline (‘n’), space (‘ ’), form feed (‘f’), carriage return (‘r’), horizontal tab (‘t’) or vertical tab (‘v’) – and 0 otherwise. int iscntrl(int c) Returns a true if c is a control character, and 0 otherwise. int ispunct(int c) Returns a true if c is a printing character other than a space, a digit or a letter, and 0 otherwise. int isprint(int c) Returns a true value if c is a printing character including space (‘ ’), and 0 otherwise. int isgraph(int c) Returns a true value if c is a printing character other than space (‘ ’), and 0 otherwise.
  • 19. String Conversion Functions • These functions convert strings of digits to integer and floating- point values. • To use these functions, the general utilities library <stdlib.h>, needs to be included. • Example: • atoi: string to int • atof: string to double
  • 20. Example /*1. Converting a String Into an int Using atoi. */ #include <stdio.h> #include <stdlib.h> int main() { char str1[ ] = "124z3yu87"; char str2[ ] = "-3.4"; char str3[ ] = "e24"; int i1 = atoi(str1), i2 = atoi(str2), i3 = atoi(str3); printf("i1: %dn", i1); printf("i2: %dn", i2); printf("i3: %dn", i3); return 0; }
  • 21. Example /*1. Converting a String Into an int Using atoi. */ #include <stdio.h> #include <stdlib.h> int main() { char str1[ ] = "124z3yu87"; char str2[ ] = "-3.4"; char str3[ ] = "e24"; int i1 = atoi(str1), i2 = atoi(str2), i3 = atoi(str3); printf("i1: %dn", i1); printf("i2: %dn", i2); printf("i3: %dn", i3); return 0; } Output: i1: 124 i2: -3 i3: 0