The document discusses computing fundamentals and string manipulation in C programming. It covers ASCII codes, comparing strings, generating random numbers and the Fibonacci sequence. ASCII codes assign numeric values to characters that are stored in computer memory. Strings can be compared using strcmp() which returns 0 if equal or >0 if the first string is greater. Random numbers are generated between 1-100 using rand()%100+1. The Fibonacci sequence is generated by adding the previous two values to get the next one in the series.
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;
}