Download presentation
1
CHAPTER 8 CHARACTER AND STRINGS
Department of Systems and Networking Ver.1 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 Systems and Networking
3
Systems and Networking
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 Systems and Networking
4
Systems and Networking
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 Systems and Networking
5
Systems and Networking
ASCII Table (0-127) Reference: Ver.1 Systems and Networking
6
Systems and Networking
Example #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); } 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 Press any key to continue . . . Ver.1 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 Systems and Networking
8
Systems and Networking
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 Systems and Networking
9
Systems and Networking
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 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 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 Systems and Networking
12
Systems and Networking
continue 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. Ver.1 Systems and Networking
13
Systems and Networking
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 Systems and Networking
14
String Initializations
index [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] N e w Y a r \o 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. Ver.1 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. [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] G o d \o index data Ver.1 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. [0] [1] [2] [3] [4] G o d \o Ver.1 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); Good Good Ver.1 Systems and Networking
18
Systems and Networking
continue #include <stdio.h> int main() { char str[10]="Good"; printf("%s\n\n",str); puts(str); return 0; } Good Press any key to continue . . . Ver.1 Systems and Networking
19
Systems and Networking
Ver.1 Systems and Networking
20
Systems and Networking
Ver.1 Systems and Networking
21
Input of string variables
Two functions: scanf gets char 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 Systems and Networking
22
Systems and Networking
continue [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] G o d \o 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 Systems and Networking
23
Systems and Networking
continue char str[10]; gets(str); User input =Good day output [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] G o d a y \o Ver.1 Systems and Networking
24
Systems and Networking
continue Please enter your name asmidar Please enter your age 23 Your name is asmidar and your age is 23 Press any key to 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; } 1 User input User input Please enter your name ali ahmad Please enter your age Your name is ali and your age is Press any key to continue … 2 User input ???? Why output 2 display the age as random numbers? Why we cannot input the age? Ver.1 Systems and Networking
25
Systems and Networking
Ver.1 Systems and Networking
26
Systems and Networking
continue Ver.1 Systems and Networking
27
Systems and Networking
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; } Please enter your name asmidar abu bakar Please enter your age 23 age is 23Press any key to continue … Ver.1 Systems and Networking
28
Systems and Networking
Ver.1 Systems and Networking
29
Systems and Networking
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 Systems and Networking
30
Systems and Networking
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 Systems and Networking
31
Systems and Networking
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; } I love C The value of string 2 is I love C Press any key to continue . . . Ver.1 Systems and Networking
32
Systems and Networking
Ver.1 Systems and Networking
33
Systems and Networking
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; } I love C The value of string 2 is I love C Press any key to continue . . . Why the output is like this??? Ver.1 Systems and Networking
34
Systems and Networking
Ver.1 Systems and Networking
35
Systems and Networking
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 Systems and Networking
36
Systems and Networking
continue Example: based on ASCII computer The string “data” is less than the string “date” d a t d a t e 1 2 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”. Ver.1 Systems and Networking
37
Systems and Networking
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 Systems and Networking
38
Systems and Networking
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; } The function call strcmp(str1,str2) returns a negative integer, therefore the output produce is as below: data is not greater than datePress any key to continue . Ver.1 Systems and Networking
39
Systems and Networking
#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; } Swap the string date is greater than dataPress any key to continue . . . Ver.1 Systems and Networking
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.