Fundamentals of Programming Session 21 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2013 These slides have been created using Deitel’s slides Sharif University of Technology
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
Fundamentals of Strings and Characters 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
Character- Handling Library … 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 … 5
Character- Handling Library … 6
Character- Handling Library … 7
Character- Handling Library … 8
Character- Handling Library … 9
Character- Handling Library … 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 … 11
Character- Handling Library … 12
Character- Handling Library … 13
Question 1 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
String-Conversion Functions 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 … 16
String- Conversion Functions … 17
String- Conversion Functions … 18
String- Conversion Functions … 19
Question 2 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 3 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
Answer 3 #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)); 22 return 0; }
Answer 3 … 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
Question 4 Write a program to simulate a stack data structure. Your program should input an integer in range [1, 5] to do the following tasks: Pop from top of the stack 1. Push to top of the stack 2. Pop from bottom of the stack 3. Push to bottom of the stack 4. Exit from the program 5. 24
Answer 4 #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 … 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 … 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
Standard Input/Output Library Functions 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 … 29
Standard Input/Output Library Functions … 30
Standard Input/Output Library Functions … 31
Standard Input/Output Library Functions … 32
String-Manipulation Functions of the String- Handling Library 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 of 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 … 34
String-Manipulation Functions of the String- Handling Library … 35
String-Manipulation Functions of the String- Handling Library … 36
Recommend
More recommend