Fundamentals of Programming Session 21 Instructor: Reza - - PowerPoint PPT Presentation

fundamentals of programming
SMART_READER_LITE
LIVE PREVIEW

Fundamentals of Programming Session 21 Instructor: Reza - - PowerPoint PPT Presentation

Fundamentals of Programming Session 21 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2013 These slides have been created using Deitels slides Sharif University of Technology Outlines Fundamentals of Strings and


slide-1
SLIDE 1

Fall 2013

Instructor: Reza Entezari-Maleki

Email: entezari@ce.sharif.edu

Sharif University of Technology

1

Fundamentals of Programming

Session 21

These slides have been created using Deitel’s slides

slide-2
SLIDE 2

Outlines

 Fundamentals of Strings and Characters  String-Conversion Functions  Standard Input/Output Library Functions  String-Manipulation Functions of the String-

Handling Library

 Comparison Functions of the String-

Handling Library

2

slide-3
SLIDE 3

 A string in C is an array of characters ending in the null

character ('\0').

 The definitions

 char

char color[] = color[] = "blue" "blue"; const char const char *colorPtr colorPtr = = "blue" "blue";

each initialize a variable to the string "blue".

 The first definition creates a 5-element array color

containing the characters 'b', 'l', 'u', 'e' and '\0'.

 The second definition creates pointer variable

colorPtr that points to the string "blue" somewhere in memory.

3

Fundamentals of Strings and Characters

slide-4
SLIDE 4

 The character-handling library (<ctype.h>) includes

several functions that perform useful tests and manipulations of character data.

 Each function receives a character—represented as an

int—or EOF as an argument.

 EOF normally has the value –1, and some hardware

architectures do not allow negative values to be stored in char variables, so the character-handling functions manipulate characters as integers.

 Figure 8.1 summarizes the functions of the character-

handling library.

4

Character-Handling Library …

slide-5
SLIDE 5

5

Character-Handling Library …

slide-6
SLIDE 6

6

Character-Handling Library …

slide-7
SLIDE 7

7

Character-Handling Library …

slide-8
SLIDE 8

8

Character-Handling Library …

slide-9
SLIDE 9

9

Character-Handling Library …

slide-10
SLIDE 10

 Figure 8.4 demonstrates functions isspace, iscntrl,

ispunct, isprint and isgraph.

 Function isspace determines if a character is one of

the following white-space characters: space (' '), form feed ('\f'), newline ('\n'), carriage return ('\r'), horizontal tab ('\t') or vertical tab ('\v').

10

Character-Handling Library …

slide-11
SLIDE 11

11

Character-Handling Library …

slide-12
SLIDE 12

12

Character-Handling Library …

slide-13
SLIDE 13

13

Character-Handling Library …

slide-14
SLIDE 14

 What will be the output of the following code?

int main(){ char arr[]="A B C D E F G H L J K M"; double *fptr; fptr=(double *)arr; fptr++; printf("%s",fptr); return 0; }

 Answer:

E F G H L J K M

14

Question 1

slide-15
SLIDE 15

 This section presents the string-conversion functions

from the general utilities library (<stdlib.h>).

 These functions convert strings of digits to integer and

floating-point values.

 Figure 8.5 summarizes the string-conversion functions.  Note the use of const to declare variable nPtr in the

function headers (read from right to left as ―nPtr is a pointer to a character constant‖); const specifies that the argument value will not be modified.

15

String-Conversion Functions

slide-16
SLIDE 16

16

String-Conversion Functions …

slide-17
SLIDE 17

17

String-Conversion Functions …

slide-18
SLIDE 18

18

String-Conversion Functions …

slide-19
SLIDE 19

19

String-Conversion Functions …

slide-20
SLIDE 20

 What will be the output of the following code?

int f1(); int f2(int y); int f3(int (*f4)(int)); int main(){ printf("%d",f1()); return 0;} int f1(){ int x=2; return f3(f2); } int f2(int y){ return y*y; } int f3(int (*f4)(int)){ int x=5; return f4(x)*x; }

Answer:

125

20

Question 2

slide-21
SLIDE 21

 Write a program to get two sizes, both less than 100, and define

two arrays with the specified sizes. Afterwards, read the elements of both arrays, and then remove the elements of the first array in which their indices have been indicated in the second array.

21

Question 3

slide-22
SLIDE 22

#define ArrLengthMax 100 #define IndLengthMax 100 #define AnsLengthMax 100 int main(){ int Arr[ArrLengthMax], Ind[IndLengthMax], ArrLength, IndLength; int *Rem, i; printf("Enter first array length:\r\n"); scanf("%d", &ArrLength); printf("Enter second array length:\r\n"); scanf("%d", &IndLength); printf("Enter first array:\r\n"); for(int i = 0; i < ArrLength; i++) scanf("%d", (Arr + i)); printf("Enter second array:\r\n"); for(int i = 0; i < IndLength; i++) scanf("%d", (Ind + i)); printf("Result:\r\n"); Rem = Remove(Arr, ArrLength, Ind, IndLength); for(i = 0; i < ArrLength - IndLength; i++) printf("%d ", *(Rem + i)); return 0; }

22

Answer 3

slide-23
SLIDE 23

int * Remove(int *Arr, int ArrLength, int *Ind, int IndLength) { int Ans[AnsLengthMax], Index = 0, i; for(i = 0 ; i < ArrLength; i++) { int Found = 0; for(int j = 0 ; j < IndLength; j++) { if(i == *(Ind + j)) { Found = 1; break; } } if(Found) continue; *(Ans + Index++) = *(Arr + i); } return Ans; }

23

Answer 3 …

slide-24
SLIDE 24

 Write a program to simulate a stack data structure. Your program

should input an integer in range [1, 5] to do the following tasks:

1.

Pop from top of the stack

2.

Push to top of the stack

3.

Pop from bottom of the stack

4.

Push to bottom of the stack

5.

Exit from the program

24

Question 4

slide-25
SLIDE 25

#define SIZE 100 int main(){ int stack[SIZE]={0}; int n=0, temp; int sp1=0, sp2=0; while(n!=5){ scanf("%d",&n); switch(n){ case 1: // pop -- up if(sp1>sp2){ sp1--; printf("%d\n", stack[sp1]); } else printf("The stack is empty\n"); break;

25

Answer 4

slide-26
SLIDE 26

case 2: // push -- up scanf("%d",&temp); if(sp1==SIZE) printf("The stack is full\n"); else { stack[sp1]=temp; sp1++; } break; case 3: // pop -- down if(sp2<sp1){ printf("%d\n", stack[sp2]); sp2++; } else printf("empty\n"); break;

26

Answer 4 …

slide-27
SLIDE 27

case 4: // push -- down if(sp2>0){ sp2--; scanf("%d",&temp); stack[sp2]=temp; } else printf("end of stack\n"); break; default: if(n!=5) printf("wrong input"); } } return 0; }

27

Answer 4 …

slide-28
SLIDE 28

 This

section presents several functions from the standard input/output library (<stdio.h>) specifically for manipulating character and string data.

 Figure

8.12 summarizes the character and string input/output functions of the standard input/output library.

28

Standard Input/Output Library Functions

slide-29
SLIDE 29

29

Standard Input/Output Library Functions …

slide-30
SLIDE 30

30

Standard Input/Output Library Functions …

slide-31
SLIDE 31

31

Standard Input/Output Library Functions …

slide-32
SLIDE 32

32

Standard Input/Output Library Functions …

slide-33
SLIDE 33

 The string-handling library (<string.h>) provides

many useful functions for manipulating string data (copying strings and concatenating strings), comparing strings, searching strings for characters and other strings, tokenizing strings (separating strings into logical pieces) and determining the length of strings.

 This section presents the string-manipulation functions

  • f the string-handling library.

 The functions are summarized in Fig. 8.17.  Every function—except for strncpy—appends the

null character to its result.

33

String-Manipulation Functions of the String- Handling Library

slide-34
SLIDE 34

34

String-Manipulation Functions of the String- Handling Library …

slide-35
SLIDE 35

35

String-Manipulation Functions of the String- Handling Library …

slide-36
SLIDE 36

36

String-Manipulation Functions of the String- Handling Library …