CJ LABS
Basic Concept of C
Topics of Discussion:
• Concept of ASCII
• String
What is ASCII Value?
ASCII stands for American Standard Code for Information Interchange, A
character encoding standard that assigns unique numerical values to letters,
digits, punctuation marks, and other symbols.
A -> 65, B -> 66, C -> 67,……………….., Z -> 90
a -> 97, b -> 98, c -> 99,…………………, z -> 122
Practice Question
Print the following pattern
'A'
A
BB 'A' + 1 = ‘ B'
CCC
DDDD char ch = 'A' + 5;
// ch = 'F'
EEEEE
printf("%c", 65);
// A
Strings
A character array terminated by a '\0' (null character)
null character denotes string termination
EXAMPLE
Character
char name[] = { ‘N’, ‘E’, ‘E’, ‘H’, ‘A’, ‘L’} ;
array
char name[] = { ‘N’, ‘E’, ‘E’, ‘H’, ‘A’, ‘L’, ‘\0’} ; String
Character
char name[] = ‘Neehal’;
array
char name[] = “Neehal”; String
Initializing a string
Assigning string literal without size
char str[] = “CodingJunction” ;
Assigning string literal with size
char str[50] = “CodingJunction” ;
Look for the error
char str[14] = “CodingJunction” ;
Accessing individual characters
char name[] = “Probal”;
P r o b a l
printf(“%c”, name[3]); // b
printf(“%c”, name[4]); // a 0 1 2 3 4 5
printf(“%c”, name[5]); // l
char str[] = “CodingJunction”;
Predict the output
printf(“%c”, str[5]);
printf(“%d”, str[4]);
Modifying individual characters
char name[] = “Suman”; S u m a n
0 1 2 3 4
name[2] = ‘v’; S u v a n
char str[] = “CodingJunction”;
Predict the str after:
str[0] = ‘L’;
str[6] = 70;
String Format Specifier “%s”
INPUT
char str[100];
scanf(“%s”, str);
OUTPUT
printf(“%s”, str);
Practice Problem
1. Write a program to take first name as input from user and:
a.Print it without using loops.
b.Print it using loops.
IMPORTANT
scanf( ) cannot input multi-word strings with spaces.
That’s why we use gets( ) and puts( )
gets( ), fgets( ) and puts( )
gets(str) fgets(str, n, stdin)
Input a string Stops when n-1 chars
(even multiword) input or new line
puts(str)
Output a string
Practice Problem
1. Write a program to take full name as input from user:
a. Using gets and print using puts.
b. Using fgets and print using puts.
2. Write a program to take a string as input and find the
length of the string. (without built in function)
Strings Library Function <string.h>
1. strlen(str)
count number of characters excluding '\0'
2. strcat(firstStr, secStr)
concatenates first string with second string
NOTE: firstStr
should be large
enough
Strings Library Function <string.h>
3. strcpy(newStr, oldStr)
copies value of old string to new string
0 -> string equal
4. strcmp(firstStr, secStr)
positive -> first > second (ASCII)
Compares 2 strings & returns a value
negative -> first < second (ASCII)
Library Function <ctype.h>
1.islower(c)
Checks if character is a lowercase English letter.
2. isupper(c)
Checks if character is a uppercase English letter.
3. tolower(c)
Converts uppercase character to lowercase, if applicable.
4. toupper(c)
Converts lowercase character to uppercase, if applicable.
Practice Problems
1.Count:
a. Vowels and Consonants.
b. Uppercase and Lowercase alphabets.
2.Count total words in a sentence.
3. Check whether the word is palindrome or not.