1 / 39

CHAPTER 8 CHARACTER AND STRINGS

CHAPTER 8 CHARACTER AND STRINGS. Department of Systems and Networking. FUNDAMENTAL OF STRINGS AND CHARACTERS. Characters are the fundamental building blocks of source programs. A program may contain character constants.

ohio
Download Presentation

CHAPTER 8 CHARACTER AND STRINGS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CHAPTER 8CHARACTER AND STRINGS Department of Systems and Networking 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. 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). 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. Systems and Networking

  5. ASCII Table (0-127) Reference: http://www.plcdev.com/ascii_chart Systems and Networking

  6. 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 . . . 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 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. 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. 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. Systems and Networking

  11. Summary on Character Handling Library Systems and Networking

  12. continue 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”; Systems and Networking

  14. String Initializations 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. 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. index data 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. 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 Systems and Networking

  18. continue #include <stdio.h> int main() { char str[10]="Good"; printf("%s\n\n",str); puts(str); return 0; } Good Good Press any key to continue . . . Systems and Networking

  19. Systems and Networking

  20. 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. 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. Systems and Networking

  23. continue char str[10]; gets(str); User input =Good day output Systems and Networking

  24. 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 aliahmad Please enter your age Your name is ali and your age is -858993460Press any key to continue … 2 User input ???? Why output 2 display the age as random numbers? Why we cannot input the age? Systems and Networking

  25. Systems and Networking

  26. continue 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; } Please enter your name asmidar abu bakar Please enter your age 23 asmidar abu bakar age is 23Press any key to continue … Systems and Networking

  28. 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 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. 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; } I love C The value of string 2 is I love C Press any key to continue . . . Systems and Networking

  32. 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; } I love C The value of string 2 is I love C Press any key to continue . . . Why the output is like this??? Systems and Networking

  34. 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. Systems and Networking

  36. continue Example: based on ASCII computer • The string “data” is less than the string “date” 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”. 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 “ 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; } 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 . 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; } Swap the string date is greater than dataPress any key to continue . . . Systems and Networking

More Related