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

c programming for engineers characters strings
SMART_READER_LITE
LIVE PREVIEW

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'


slide-1
SLIDE 1

1

C Programming for Engineers Characters & Strings

ICEN 360– Spring 2017

  • Prof. Dola Saha
slide-2
SLIDE 2

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

  • f 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.

slide-3
SLIDE 3

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

slide-4
SLIDE 4

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.

slide-5
SLIDE 5

5

Character Handling Library <ctype.h>

slide-6
SLIDE 6

6

Character Handling Library <ctype.h>

slide-7
SLIDE 7

7

Example using ctype.h (1)

slide-8
SLIDE 8

8

Example using ctype.h (2)

slide-9
SLIDE 9

9

Output of using ctype.h

slide-10
SLIDE 10

10

String conversions from <stdlib.h>

slide-11
SLIDE 11

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.

slide-12
SLIDE 12

12

strtod() Example

slide-13
SLIDE 13

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.

slide-14
SLIDE 14

14

String Functions from <stdio.h>

slide-15
SLIDE 15

15

String Functions from <stdio.h>

slide-16
SLIDE 16

16

sprintf() Example

slide-17
SLIDE 17

17

sscanf() Example

slide-18
SLIDE 18

18

String Manipulation in <string.h>

slide-19
SLIDE 19

19

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

slide-20
SLIDE 20

20

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

slide-21
SLIDE 21

21

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

slide-22
SLIDE 22

22

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

slide-23
SLIDE 23

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.

slide-24
SLIDE 24

24

String Compare Example – C Code

slide-25
SLIDE 25

25

String Compare Example – Output

slide-26
SLIDE 26

26

More string functions (1)

slide-27
SLIDE 27

27

More string functions (2)

slide-28
SLIDE 28

28

strchr()

slide-29
SLIDE 29

29

strchr()

slide-30
SLIDE 30

30

strcspn()

slide-31
SLIDE 31

31

strpbrk()

slide-32
SLIDE 32

32

strtok()

slide-33
SLIDE 33

33

strtok() Output

slide-34
SLIDE 34

34

Memory Functions

slide-35
SLIDE 35

35

memcpy()

slide-36
SLIDE 36

36

memmove()

slide-37
SLIDE 37

37

memcmp()

slide-38
SLIDE 38

38

memchr()

slide-39
SLIDE 39

39

memset()

slide-40
SLIDE 40

40

More functions

slide-41
SLIDE 41

41

strlen()

slide-42
SLIDE 42

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.