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
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
Fall 2013
Sharif University of Technology
1
Session 21
These slides have been created using Deitel’s slides
2
A string in C is an array of characters ending in the null
The definitions
char
char color[] = color[] = "blue" "blue"; const char const char *colorPtr colorPtr = = "blue" "blue";
The first definition creates a 5-element array color
The second definition creates pointer variable
3
The character-handling library (<ctype.h>) includes
Each function receives a character—represented as an
EOF normally has the value –1, and some hardware
Figure 8.1 summarizes the functions of the character-
4
5
6
7
8
9
Figure 8.4 demonstrates functions isspace, iscntrl,
Function isspace determines if a character is one of
10
11
12
13
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
This section presents the string-conversion functions
These functions convert strings of digits to integer and
Figure 8.5 summarizes the string-conversion functions. Note the use of const to declare variable nPtr in the
15
16
17
18
19
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; }
125
20
Write a program to get two sizes, both less than 100, and define
21
#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
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
Write a program to simulate a stack data structure. Your program
1.
2.
3.
4.
5.
24
#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
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
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
This
Figure
28
29
30
31
32
The string-handling library (<string.h>) provides
This section presents the string-manipulation functions
The functions are summarized in Fig. 8.17. Every function—except for strncpy—appends the
33
34
35
36