SlideShare a Scribd company logo
1 of 17
Download to read offline
Computing Fundamentals
Dr. Muhammad Yousaf Hamza
Deputy Chief Engineer, PIEAS
Comparison of Strings
Take the help of ASCII Codes
What are these?
Dr. Yousaf, PIEAS
ASCII Codes
Dr. Yousaf, PIEAS
• ASCII stands for American Standard Code for
Information Interchange.
• ASCII character set is a way of representing characters
such as ‘a’, ‘B’, ‘$’, ‘3’, and so on, as numbers.
• Computer's memory stores all data in numeric form.
• There is no direct way to store characters.
• However, a numeric code exists for each character. This is
called the ASCII code.
• The code assigns values between 0 and 255 for upper-
and lowercase letters, numeric digits, punctuation marks,
and other symbols.
Dr. Yousaf, PIEAS
ASCII CODES
ASCII CODES
• The uppercase letters, A to Z, have ASCII
codes 65 through 90.
• The lowercase letters, a to z have ASCII
codes 97 through 122.
Dr. Yousaf, PIEAS
• char x = ‘a’;
• When the C compiler encounters such a character, it
translates it into the corresponding ASCII code.
• The character ‘a’ appearing in a program, for
example, will be translated into 97.
Dr. Yousaf, PIEAS
ASCII CODES
// To determine the ASCII code
#include<stdio.h>
int main()
{
char x = ‘a';
printf("ASCII code of %c is %dn", x, x);
getchar();
return 0;
}
Output: ASCII code of a is 97
Dr. Yousaf, PIEAS
• ASCII code of a is 97
• ASCII code of A is 65
• ASCII code of * is 42
Dr. Yousaf, PIEAS
• The standard ASCII codes go only to 127,
This range includes all letters, numbers, punctuation
marks, and other keyboard symbols.
• The codes from 128 to 255 are the extended ASCII
codes and represent special characters such as
foreign letters and graphics symbols.
Dr. Yousaf, PIEAS
ASCII CODES
#include <stdio.h>
int main()
{
int x;
for (x = 0; x < 255; x++)
{
printf("ASCII code %d is character %cn", x, x);
// note the use of %d and %c
}
getchar(); return 0; }
Dr. Yousaf, PIEAS
Printing Standard and Extended
ASCII Codes
String Matching
• Comparing Strings
• Strings are compared to determine whether they are
equal or unequal. If they are unequal, one string is
"greater than" or "less than" the other.
• Determinations of "greater" and "less" are made
with the ASCII codes of the characters
Dr. Yousaf, PIEAS
String Matching
In the case of letters, this is equivalent to
alphabetical order, with the one seemingly strange
exception that
all uppercase letters are "less than" the lowercase
letters.
This is true because the uppercase letters have ASCII
codes 65 through 90 for A through Z, while lowercase
a through z are represented by 97 through 122. Thus,
"ZEBRA" would be considered to be less than “zebra"
by these C functions.
Dr. Yousaf, PIEAS
#include <stdio.h>
#include <string.h>
int main()
{
int x;
char name1[20] = "Hello";
char name2[20] = "Hello";
x = strcmp(name1,name2);
// x can have one of three values (i) 0 (ii) >0 (iii) < 0.
if (x == 0)
printf("strings '%s' and '%s' are well matched",name1,name2);
else if (x>0)
printf("String '%s' is greater than '%s'", name1,name2);
else
printf("String '%s' is smaller than '%s'", name1,name2);
getchar(); return 0; }
Dr. Yousaf, PIEAS
String Matching
strcpy(name2,name1); // copies name1 to name2
strncpy(name2,name1,3);
// copies first three letters of name1 to name2
strcat(name1,name2); // name1name2
strncat(name1,name2,n);
/* n means how many first characters you want to concatenate
*/
x = strcmp(str1, str2);
x = strncmp(str1, str2, n);
/* n means how many first characters you want to compare */
Dr. Yousaf, PIEAS
#include <stdio.h>
#include<conio.h>
int main()
{
char x, y,z;
puts(“Press a Character and
then Press Enter Key”);
x = getchar();
printf("The user entered
%cnn", x);
puts("Please press a character");
y = getch();
//Just press character. No need of
// Pressing the Enter Key
printf("The user entered %cnn",
y);
puts("Please enter a charactern");
z = getche();
printf("nThe user entered %cn",
z); getchar(); return 0; }
Dr. Yousaf, PIEAS
getchar(), getch(), getche()
getch() and getche require conio.h
getchar () needs enter key; getch(): no echo getche() last e is for echo.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int c, n;
printf("Ten random numbers
in [1,100]n");
for (c = 1; c <= 10; c++)
{
n = rand()%100 + 1;
printf("%dn", n);
}
getchar(); return 0;
}
Dr. Yousaf, PIEAS
To Generate Random Numbers
If we use rand() only as
n = rand();
It can generate random number
of value in thousands. So when
we use
n = rand()%100 + 1;
Whatever be the value of
random number, its modulus
with hundred will always be in
the range 0 to 99. By adding 1, n
will always be in the range from
1 to 100.
Concept of Seed
srand(Study it yourself)
The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3,
5, 8, 13, 21, 34, ... The next number is found by adding up
the two numbers before it. For example, 2 is found by adding
the two numbers before it (1+1)
Dr. Yousaf, PIEAS
To Generate Fibonacci Series
#include<stdio.h>
int main ()
{
int fib[50], nterms,j;
printf("This program generates
Fibonacci series upto 20
termsn");
printf("Please enter the number
of terms: ");
scanf("%d", &nterms);
fib[0] = 0;
fib[1] = 1;
for(j=2;j<nterms;j++)
fib[j] = fib[j-2] + fib[j-1];
printf("n Feibinacci series
is:nn");
for(j=0;j<nterms;j++)
printf("%dt", fib[j]);
getchar(); return 0;
}

More Related Content

Similar to C Language Lecture 14

Similar to C Language Lecture 14 (16)

C Language Lecture 13
C Language Lecture 13C Language Lecture 13
C Language Lecture 13
 
C Language Lecture 5
C Language Lecture  5C Language Lecture  5
C Language Lecture 5
 
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_
 
constants
constantsconstants
constants
 
Cse115 lecture14strings part01
Cse115 lecture14strings part01Cse115 lecture14strings part01
Cse115 lecture14strings part01
 
PHP Strings and Patterns
PHP Strings and PatternsPHP Strings and Patterns
PHP Strings and Patterns
 
php string part 4
php string part 4php string part 4
php string part 4
 
P2 2017 python_strings
P2 2017 python_stringsP2 2017 python_strings
P2 2017 python_strings
 
Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3
 
C language first program
C language first programC language first program
C language first program
 
201801 CSE240 Lecture 08
201801 CSE240 Lecture 08201801 CSE240 Lecture 08
201801 CSE240 Lecture 08
 
Introduction to Regular Expressions
Introduction to Regular ExpressionsIntroduction to Regular Expressions
Introduction to Regular Expressions
 
PHP string-part 1
PHP string-part 1PHP string-part 1
PHP string-part 1
 
COm1407: Character & Strings
COm1407: Character & StringsCOm1407: Character & Strings
COm1407: Character & Strings
 
P3 2018 python_regexes
P3 2018 python_regexesP3 2018 python_regexes
P3 2018 python_regexes
 
9 character string &amp; string library
9  character string &amp; string library9  character string &amp; string library
9 character string &amp; string library
 

More from Shahzaib Ajmal (18)

C Language Lecture 22
C Language Lecture 22C Language Lecture 22
C Language Lecture 22
 
C Language Lecture 21
C Language Lecture 21C Language Lecture 21
C Language Lecture 21
 
C Language Lecture 20
C Language Lecture 20C Language Lecture 20
C Language Lecture 20
 
C Language Lecture 19
C Language Lecture 19C Language Lecture 19
C Language Lecture 19
 
C Language Lecture 18
C Language Lecture 18C Language Lecture 18
C Language Lecture 18
 
C Language Lecture 17
C Language Lecture 17C Language Lecture 17
C Language Lecture 17
 
C Language Lecture 16
C Language Lecture 16C Language Lecture 16
C Language Lecture 16
 
C Language Lecture 15
C Language Lecture 15C Language Lecture 15
C Language Lecture 15
 
C Language Lecture 11
C Language Lecture  11C Language Lecture  11
C Language Lecture 11
 
C Language Lecture 10
C Language Lecture 10C Language Lecture 10
C Language Lecture 10
 
C Language Lecture 9
C Language Lecture 9C Language Lecture 9
C Language Lecture 9
 
C Language Lecture 8
C Language Lecture 8C Language Lecture 8
C Language Lecture 8
 
C Language Lecture 7
C Language Lecture 7C Language Lecture 7
C Language Lecture 7
 
C Language Lecture 6
C Language Lecture 6C Language Lecture 6
C Language Lecture 6
 
C Language Lecture 4
C Language Lecture  4C Language Lecture  4
C Language Lecture 4
 
C Language Lecture 3
C Language Lecture  3C Language Lecture  3
C Language Lecture 3
 
C Language Lecture 2
C Language Lecture  2C Language Lecture  2
C Language Lecture 2
 
C Language Lecture 1
C Language Lecture  1C Language Lecture  1
C Language Lecture 1
 

Recently uploaded

Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 

Recently uploaded (20)

Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptx
 
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
demyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxdemyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptx
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
Climbers and Creepers used in landscaping
Climbers and Creepers used in landscapingClimbers and Creepers used in landscaping
Climbers and Creepers used in landscaping
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
Scopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS PublicationsScopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS Publications
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge App
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 

C Language Lecture 14

  • 1. Computing Fundamentals Dr. Muhammad Yousaf Hamza Deputy Chief Engineer, PIEAS
  • 2. Comparison of Strings Take the help of ASCII Codes What are these? Dr. Yousaf, PIEAS
  • 4. • ASCII stands for American Standard Code for Information Interchange. • ASCII character set is a way of representing characters such as ‘a’, ‘B’, ‘$’, ‘3’, and so on, as numbers. • Computer's memory stores all data in numeric form. • There is no direct way to store characters. • However, a numeric code exists for each character. This is called the ASCII code. • The code assigns values between 0 and 255 for upper- and lowercase letters, numeric digits, punctuation marks, and other symbols. Dr. Yousaf, PIEAS ASCII CODES
  • 5. ASCII CODES • The uppercase letters, A to Z, have ASCII codes 65 through 90. • The lowercase letters, a to z have ASCII codes 97 through 122. Dr. Yousaf, PIEAS
  • 6. • char x = ‘a’; • When the C compiler encounters such a character, it translates it into the corresponding ASCII code. • The character ‘a’ appearing in a program, for example, will be translated into 97. Dr. Yousaf, PIEAS ASCII CODES
  • 7. // To determine the ASCII code #include<stdio.h> int main() { char x = ‘a'; printf("ASCII code of %c is %dn", x, x); getchar(); return 0; } Output: ASCII code of a is 97 Dr. Yousaf, PIEAS
  • 8. • ASCII code of a is 97 • ASCII code of A is 65 • ASCII code of * is 42 Dr. Yousaf, PIEAS
  • 9. • The standard ASCII codes go only to 127, This range includes all letters, numbers, punctuation marks, and other keyboard symbols. • The codes from 128 to 255 are the extended ASCII codes and represent special characters such as foreign letters and graphics symbols. Dr. Yousaf, PIEAS ASCII CODES
  • 10. #include <stdio.h> int main() { int x; for (x = 0; x < 255; x++) { printf("ASCII code %d is character %cn", x, x); // note the use of %d and %c } getchar(); return 0; } Dr. Yousaf, PIEAS Printing Standard and Extended ASCII Codes
  • 11. String Matching • Comparing Strings • Strings are compared to determine whether they are equal or unequal. If they are unequal, one string is "greater than" or "less than" the other. • Determinations of "greater" and "less" are made with the ASCII codes of the characters Dr. Yousaf, PIEAS
  • 12. String Matching In the case of letters, this is equivalent to alphabetical order, with the one seemingly strange exception that all uppercase letters are "less than" the lowercase letters. This is true because the uppercase letters have ASCII codes 65 through 90 for A through Z, while lowercase a through z are represented by 97 through 122. Thus, "ZEBRA" would be considered to be less than “zebra" by these C functions. Dr. Yousaf, PIEAS
  • 13. #include <stdio.h> #include <string.h> int main() { int x; char name1[20] = "Hello"; char name2[20] = "Hello"; x = strcmp(name1,name2); // x can have one of three values (i) 0 (ii) >0 (iii) < 0. if (x == 0) printf("strings '%s' and '%s' are well matched",name1,name2); else if (x>0) printf("String '%s' is greater than '%s'", name1,name2); else printf("String '%s' is smaller than '%s'", name1,name2); getchar(); return 0; } Dr. Yousaf, PIEAS String Matching
  • 14. strcpy(name2,name1); // copies name1 to name2 strncpy(name2,name1,3); // copies first three letters of name1 to name2 strcat(name1,name2); // name1name2 strncat(name1,name2,n); /* n means how many first characters you want to concatenate */ x = strcmp(str1, str2); x = strncmp(str1, str2, n); /* n means how many first characters you want to compare */ Dr. Yousaf, PIEAS
  • 15. #include <stdio.h> #include<conio.h> int main() { char x, y,z; puts(“Press a Character and then Press Enter Key”); x = getchar(); printf("The user entered %cnn", x); puts("Please press a character"); y = getch(); //Just press character. No need of // Pressing the Enter Key printf("The user entered %cnn", y); puts("Please enter a charactern"); z = getche(); printf("nThe user entered %cn", z); getchar(); return 0; } Dr. Yousaf, PIEAS getchar(), getch(), getche() getch() and getche require conio.h getchar () needs enter key; getch(): no echo getche() last e is for echo.
  • 16. #include <stdio.h> #include <stdlib.h> int main() { int c, n; printf("Ten random numbers in [1,100]n"); for (c = 1; c <= 10; c++) { n = rand()%100 + 1; printf("%dn", n); } getchar(); return 0; } Dr. Yousaf, PIEAS To Generate Random Numbers If we use rand() only as n = rand(); It can generate random number of value in thousands. So when we use n = rand()%100 + 1; Whatever be the value of random number, its modulus with hundred will always be in the range 0 to 99. By adding 1, n will always be in the range from 1 to 100. Concept of Seed srand(Study it yourself)
  • 17. The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The next number is found by adding up the two numbers before it. For example, 2 is found by adding the two numbers before it (1+1) Dr. Yousaf, PIEAS To Generate Fibonacci Series #include<stdio.h> int main () { int fib[50], nterms,j; printf("This program generates Fibonacci series upto 20 termsn"); printf("Please enter the number of terms: "); scanf("%d", &nterms); fib[0] = 0; fib[1] = 1; for(j=2;j<nterms;j++) fib[j] = fib[j-2] + fib[j-1]; printf("n Feibinacci series is:nn"); for(j=0;j<nterms;j++) printf("%dt", fib[j]); getchar(); return 0; }