c programming for engineers characters strings
play

C Programming for Engineers Characters & Strings ICEN 360 - PowerPoint PPT Presentation

C Programming for Engineers Characters & Strings ICEN 360 Spring 2017 Prof. Dola Saha 1 Fundamentals The value of a character constant is the integer value of the character in the machines character set. Example: 'z'


  1. C Programming for Engineers Characters & Strings ICEN 360– Spring 2017 Prof. Dola Saha 1

  2. Fundamentals Ø The value of a character constant is the integer value of the character in the machine’s character set. § Example: 'z' represents the integer value of z, and '\n' the integer value of newline (122 and 10 in ASCII, respectively). Ø A character is written in single quotes. Ø A string is a series of characters treated as a single unit. Ø A string may include letters, digits and various special characters such as +, -, *, / and $. Ø String literals, or string constants, in C are written in double quotation marks. 2

  3. String Ø An array of characters ending in the null character ('\0'). Ø Accessed via a pointer to the first character in the string. Ø Value is the address of its first character. Ø In C, a string is a pointer—or, a pointer to the string’s first character. Ø Are like arrays à an array is also a pointer to its first element. Ø A character array or a variable of type char * can be initialized with a string in a definition. § char color[] = "blue"; // Array § const char *colorPtr = "blue"; // Pointer to // somewhere in memory § char color[] = {'b','l','u','e','\0'}; // Explicit 3

  4. Scanning string Ø Function scanf will read characters until a space , tab , newline or end-of-file indicator is encountered. Ø The string2 should be no longer than 19 characters to leave room for the terminating null character. Ø If the user types 20 or more characters, your program may crash or create a security vulerability. Ø For this reason, we used the conversion specifier %19s so that scanf reads a maximum of 19 characters and does not write characters into memory beyond the end of the array string2 . 4

  5. Character Handling Library < ctype.h > 5

  6. Character Handling Library < ctype.h > 6

  7. Example using ctype.h (1) 7

  8. Example using ctype.h (2) 8

  9. Output of using ctype.h 9

  10. String conversions from <stdlib.h> 10

  11. strtod() Ø The function uses the char ** argument to modify a char * in the calling function (stringPtr) so that it points to the location of the first character after the converted portion of the string or to the entire string if no portion can be converted. d = strtod(string, &stringPtr); Ø indicates that d is assigned the double value converted from string , and stringPtr is assigned the location of the first character after the converted value in string. 11

  12. strtod() Example 12

  13. Classroom Assignment Ø Write a program that inputs N strings that represent integers, converts the strings to integers, sums the values and prints the total of the four values. 13

  14. String Functions from <stdio.h> 14

  15. String Functions from <stdio.h> 15

  16. sprintf() Example 16

  17. sscanf() Example 17

  18. String Manipulation in <string.h> 18

  19. Functions: strcpy() and strncpy() (1) 19

  20. Functions: strcpy() and strncpy() (2) 20

  21. Functions: strcat() and strncat() (1) 21

  22. Functions: strcat() and strncat() (2) 22

  23. String Compare -1 : if the ASCII value of first unmatched character is less than second. 1: if the ASCII value of first unmatched character is greater than second. 23

  24. String Compare Example – C Code 24

  25. String Compare Example – Output 25

  26. More string functions (1) 26

  27. More string functions (2) 27

  28. strchr() 28

  29. strchr() 29

  30. strcspn() 30

  31. strpbrk() 31

  32. strtok() 32

  33. strtok() Output 33

  34. Memory Functions 34

  35. memcpy() 35

  36. memmove() 36

  37. memcmp() 37

  38. memchr() 38

  39. memset() 39

  40. More functions 40

  41. strlen() 41

  42. Classwork Assignment Ø Write a program to input a line of text with spaces and print a table indicating the number of occurrences of each letter of the alphabet in the text. 42

Recommend


More recommend