Programming 1 with CCSNB144
Systems and Networking 1
CHAPTER 8CHARACTER AND STRINGSDepartment of Systems and Networking
Ver.1
Programming 1 with CCSNB144
Systems and Networking 2
FUNDAMENTAL OF STRINGS AND CHARACTERS
• Characters are the fundamental building blocks of source programs.
• A program may contain character constants. • Character constant is an integer value
represented as a character in a single quotes.• Example : ‘a’ represents the integer value of a,
which is 97 (based on ASCII value), while ‘A’ represents the integer value of A, which is 65.
Ver.1
Programming 1 with CCSNB144
Systems and Networking 3
Character Data• 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
• Each character is represented as an integer or as one byte of memory.
• The integer value stored is depends on the character set used by the computer.
• Two common used character sets are ASCII (American Standard Code for Information Interchange- primary used in micro/mini or mainframes computer) and EBCDIC (Extended Binary Coded Decimal Interchange Code-IBM mainframes).
Ver.1
Programming 1 with CCSNB144
Systems and Networking 4
ASCII Character set• There are 128 different characters in ASCII.• Each character is represented by a positive
integer, ranging from 0 to 127.• Required 7 bits to represents the integer
value, while the left-most bit is used for the sign.
Ver.1
Programming 1 with CCSNB144
Systems and Networking 5
ASCII Table (0-127)
Ver.1
Reference: http://www.plcdev.com/ascii_chart
Programming 1 with CCSNB144
Systems and Networking 6
Example
Ver.1
#include <stdio.h>int 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);return(0);
}
ASCII value for A is 65ASCII value for Z is 90ASCII value for a is 97ASCII value for z is 122
65 in ASCII represents A90 in ASCII represents Z97 in ASCII represents a122 in ASCII represents z Press any key to continue . . .
Output
Programming 1 with CCSNB144
Systems and Networking 7
Input and output of Character Data• In addition to printf and scanf for output and
input of character data, standard C has two other functions for input and output.
• getchar – to input the character data• putchar – to display the output of the
character data. • Both are declared in the header file stdio.h
Ver.1
Programming 1 with CCSNB144
Systems and Networking 8
• Example using scanf or getchar to input a character for a variable type char:
char ch; scanf (“%c”,&c); or ch=getchar();• Function getchar has no parameter. • It returns an integer that is counterpart of the
character read in the computer’s character system and stores it in the variable ch.
Ver.1
Programming 1 with CCSNB144
Systems and Networking 9
• Example using printf and putchar to display the character data.
char ch; printf (%c”,ch); or putchar(ch);• Function putchar requires one integer parameter. • It converts the integer to a character and prints it
on the standard output device.
Ver.1
Programming 1 with CCSNB144
Systems and Networking 10
Character-Handling Library Function• The character-handling function perform
useful tests and manipulations of character data.
• These functions are declared in the standard header file ctype.h
#include <ctype.h>• Each function receives a character,
represented as an int or EOF as an argument.
Ver.1
Programming 1 with CCSNB144
Systems and Networking 11
Summary on Character Handling Library
Function prototype Function description
int isdigit (int c); Returns a true value if c is a digit and 0 (false) otherwise.
int isalpha(int c); Returns a true value if c is a letter and 0 otherwise.
int isalnum (int c) ; Returns a true value if 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 is c is a lowercase letter and 0 otherwise.
int isupper(int c); Returns a true value is c is a uppercase letter and 0 otherwise.
int tolower(int c); If c is an uppercase letter, tolower returns c as a lowercase letter, otherwise it will returns the argument unchanged.
int toupper (int c); If c is a lowercase letter, toupper returns c as an uppercase letter, otherwise it will returns the argument unchanged.
Ver.1
Programming 1 with CCSNB144
Systems and Networking 12
continue
Ver.1
Function prototype Function description
int isspace(int c); Returns a true value 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 value if c is a control character and 0 otherwise.
int ispunct(int c); Returns a true value if c is a printing character other than a space, a digit or a letter and returns 0 otherwise.
int isprint(int c); Returns a true value if c is a printing character including a space (‘ ‘) and returns 0 otherwise.
int isgraph(int c); Returns a true value if c is a printing character other than a space (‘ ‘) and returns 0 otherwise.
Programming 1 with CCSNB144
Systems and Networking 13
String• String is a series of characters treated as a single unit.• A string constant is a sequence of characters in a source
program. It is enclosed by double quotation marks (“ “). • A string in C is implemented as an array. • Declaring a string: char str[10];
– This means the variable char str will an array of ten characters.
• Initialize the string variable as below:– char str[10] =“New Year”;
Ver.1
Programming 1 with CCSNB144
Systems and Networking 14
String Initializations[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
N e w Y e a r \o \o
Ver.1
index
data
• Variable str in memory after the declaration with initialization.• At position 8 (index), the variable str contains the character ‘\o’- the null character. This marks the end of a string.
Programming 1 with CCSNB144
Systems and Networking 15
String Initializations• Initialization of string during compile time. Example 1
– char str[10]=“Good”;
Example 2- char str[10]={‘G’, ’o’, ‘o’, ‘d’};- This example is using the notation for array
initialization.Ver.1
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
G o o d \o \o \o \o \o \oindex
data
Programming 1 with CCSNB144
Systems and Networking 16
String Initializations• String also can be initialize without declaring
the array size. Example 3
– char str[] =“Good”;– This will create an array with 5 elements to store
the four characters and the terminating character.
Ver.1
[0] [1] [2] [3] [4]
G o o d \o
Programming 1 with CCSNB144
Systems and Networking 17
Output of string variables• There are two functions to be used:
– printf – puts
char str[10]=“Good”;
Example 1: printf(“%s”,str); Example 2: puts(str);
Ver.1
Good
Good
Programming 1 with CCSNB144
Systems and Networking 18
continue#include <stdio.h>int main(){
char str[10]="Good";
printf("%s\n\n",str);puts(str);
return 0;
}
Ver.1
Good
GoodPress any key to continue . . .
Programming 1 with CCSNB144
Systems and Networking 19Ver.1
Programming 1 with CCSNB144
Systems and Networking 20Ver.1
Programming 1 with CCSNB144
Systems and Networking 21
Input of string variables• Two functions:
– scanf– getschar str[10];Example 1: scanf(“%s”,str);
scanf skips all whitespace characters preceding the string constant type using keyboard.
Hence it is not possible to input a string value containing whitespace. Example : char str[10]; scanf(“%s”,str); user input Good day and press Enter, any whitespace
characters preceding the first word, Good will be skipped.
Ver.1
Programming 1 with CCSNB144
Systems and Networking 22
continue
– The remaining word, day with the new-line character ‘\n’ remain in the input stream to be used by the next input statement, if any in the program.
Example 2: gets(str); char str[10]; gets(str);
gets does not skip whitespace character. It reads characters into its parameter from the standard input device until a
new-line or end-of-file character is encountered. It will returns the string that it has just read.
Ver.1
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
G o o d \o \o \o \o \o \o
Programming 1 with CCSNB144
Systems and Networking 23
continue
char str[10]; gets(str); User input =Good day
Ver.1
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
G o o d d a y \o \o
output
Programming 1 with CCSNB144
Systems and Networking 24
continue#include <stdio.h>int main(){
char name[10];int age;
printf("Please enter your name \n"); scanf("%s",name); printf("Please enter your age\n"); scanf("%d",&age); printf("Your name is %s and your age is %d",name,age);
return 0; }
Ver.1
Please enter your nameasmidarPlease enter your age23Your name is asmidar and your age is 23 Press any key to continue . .
Please enter your nameali ahmadPlease enter your age Your name is ali and your age is -858993460Press any key to continue …
1
2
Why output 2 display the age as random numbers? Why we cannot input the age?
User input
User input
User input????
Programming 1 with CCSNB144
Systems and Networking 25Ver.1
Programming 1 with CCSNB144
Systems and Networking 26
continue
Ver.1
Programming 1 with CCSNB144
Systems and Networking 27
continue#include <stdio.h>int main(){
char name[10];int age;
printf("Please enter your name \n");
gets(name);//gets name with whitespace printf("Please enter your age\n"); scanf("%d",&age);//use scanf for integer data puts(name);//display name with whitespace printf(" age is %d",age);
return 0; }
Ver.1
Please enter your nameasmidar abu bakarPlease enter your age23asmidar abu bakar age is 23Press any key to continue …
Programming 1 with CCSNB144
Systems and Networking 28Ver.1
Programming 1 with CCSNB144
Systems and Networking 29
String processing • To use string manipulation functions, the functions
need to be declared in #include <string.h>• The fundamental string operations include the
following:– Copying string– Comparing strings– Computing lengths of string– Concatenating strings– Searching strings for substrings– Tokenizing string
Ver.1
Programming 1 with CCSNB144
Systems and Networking 30
Copying strings• strcpy – to copy one string into another.• This function has two string parameters. • It copies the second parameter into the first one and
returns the first parameter. Example : char str1[20],str2[20];
let str1 = “C is fun”;
Assume we have the statement :strcpy(str2,str1);
This statement will copy the value of str1 to str2.The value in str2 now will be ‘C is fun”. The value in str1 remain unchanged.
Ver.1
Programming 1 with CCSNB144
Systems and Networking 31
Complete example#include <stdio.h>#include <string.h>int main(){
char str1[20]="I love C";char str2[20];
puts(str1); strcpy(str2,str1); printf(" The value of string 2 is %s\n",str2);
return 0; }
Ver.1
I love C The value of string 2 is I love CPress any key to continue . . .
Programming 1 with CCSNB144
Systems and Networking 32Ver.1
Programming 1 with CCSNB144
Systems and Networking 33
Example 2#include <stdio.h>#include <string.h>int main(){
char str1[20]="I love C";char str2[20]=" C is fun to learn";
puts(str1); strcpy(str2,str1); printf(" The value of string 2 is %s\n",str2);
return 0; }
Ver.1
I love C The value of string 2 is I love CPress any key to continue . . .
Why the output is like this???
Programming 1 with CCSNB144
Systems and Networking 34Ver.1
Programming 1 with CCSNB144
Systems and Networking 35
Comparing strings• strcmp is used to compare two strings.• It has two parameters of type string.• It returns an integer that is less than 0, equal to 0 or greater
than 0, if the first string parameter is less than, equal to or greater than the second string parameter.
• How the comparing is taken place?– Computer will look at the first characters in each strings.– If the first characters match, it continues with the next characters
until the two characters being compared do not match.– If all characters in both strings match and the two strings are the
same length, there are considered equal.
Ver.1
Programming 1 with CCSNB144
Systems and Networking 36
continueExample: based on ASCII computer• The string “data” is less than the string “date”
Ver.1
d a t a d a t e
12 3
4
ASCII value :d = 100, a=97, t=116, e=101•Both characters on strings are same except at position 4, where data is ended with ‘a’, while date is ended with ‘e’. • The value for ‘e’ is bigger than ‘a’, hence it is correct that “data” is less than “date”.
Programming 1 with CCSNB144
Systems and Networking 37
continue• The string “A125” is greater than “15A”• “data” is equal to “data”• “data” is greater than “ data”• “125” is greater than “123”• “data” is less than “data “
Ver.1
Programming 1 with CCSNB144
Systems and Networking 38
Example #include <stdio.h>#include <string.h>int main(){
char str1[20]="data";char str2[20]="date";
if(strcmp(str1,str2)> 0 ) printf("%s is greater than %s",str1,str2);
else printf("%s is not greater than %s",str1,str2);
return 0; }
Ver.1
data is not greater than datePress any key to continue .
The function call strcmp(str1,str2) returns a negative integer, therefore the output produce is as below:
Programming 1 with CCSNB144
Systems and Networking 39
#include <stdio.h>#include <string.h>int main(){
char str1[20]="data";char str2[20]="date";
if(strcmp(str2,str1)> 0 ) printf("%s is greater than %s",str2,str1);
else printf("%s is not greater than %s",str2,str1);
return 0; }
Ver.1
Swap the string
date is greater than dataPress any key to continue . . .