Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Counting words in a line Input: a line Output: the number of words in the line #include <stdio.h> int main() { char c, line[80]; int i, num = 0, word = 0; gets(line); e.g. “ This is a test. “ output is 4 How do you count words? Data structure: line[], i, c, num, word for (i = 0; (c = line[i])!= '\0'; ++i) if (c ==' ') word = 0; else if (word == 0) { word = 1; num++; } printf("There are %d words in the line\n", num); Algorithm: 1. Get an input string line[], num = 0, word = 0 2. For i from 0 to strlen(line) step 1 3. if line[i] = ‘ ‘ word = 0 4. else if word = 0 then 5. word =1, num = num + 1 6. Print num What’s the running time of the algorithm? The running time is O(n), where n is the length of the input string } Ex. Write the a program to count the number of words in a file CP104 Introduction to Programming String Lecture 27 __ 1 Find for a string in a line Input: a line and a string Output: the position of the string in the line if found; otherwise not found e.g. input: “This is a test.”, test output “test” is found at position 10 How do you do the search? Data structure: line[80], i, to_find[20], substr[20], found, position Algorithm: 1. Get an input line, and string to_find find_len = strlen(to_find), found = 0, i = 0; 2. While !found && I <= strlen(line)-find_len do 3. strncpy(substr, &line[i], find_len) 4. strcmp(substr, to_find) = 0 5. found = 1 6. else increase i = i +1 7. If found then output i, else output not fou What’s the running time of this algorithm? The running time is O(n), where n is the length of the input string #include <stdio.h> #include <string.h> #define LINE_LEN 80 int main() { char line[LINE_LEN], to_find[20], substr[20]; int i = 0, find_len, found = 0; printf("Input a line>\n"); gets(line); printf("Input a searching string>\n"); gets(to_find); find_len = strlen(to_find); while (!found && i <= strlen(line)-find_len) { strncpy(substr, &line[i], find_len); substr[find_len] = '\0'; if (strcmp(substr, to_find) == 0) found = 1; else ++i; } if (found) printf("The string is found at position %d.", i); else printf(“Not found"); } Ex. Write the a program to count the number of a given word in a file CP104 Introduction to Programming String Lecture 27 __ 2 String Sorting Input: a sequence of words Output: the sequence of the words in increasing order e.g. input: this, is, a, test output: a, is, test, this Application: dictionary How do you store the words e.g. Use str[50][20] to hold 50 words How do you do the sorting? Bubble sort, selection sort The running time is O(n2), where n is the number of words. CP104 Introduction to Programming String void selection_sort(char words[][20], int n) { int fill, index_of_min; char temp[20]; for (fill = 0; fill < n; ++fill) { index_of_min = find_min(words, fill, n-1); if (fill != index_of_min) { strcpy(temp, words[index_of_min]); strcpy(words[index_of_min], words[fill]); strcpy(words[fill], temp); } } } int find_min(char words[][20], int fill, int n){ int i, cur_min_posi; cur_min_posi = fill; for (i = fill+1; i <= n; ++i) if (strcmp(words[i], words[cur_min_posi])<0) {cur_min_posi = i;} return (cur_min_posi); } Lecture 27 __ 3 More comments on running of an algoirthm • The running time of an algorithm is defined to be the number of basic operations done by the algorithm. • Example: – A linear time algorithm is one with running time O(n), ie. Cn, where n is the data length, and C is a constant. • Simple search, find max, find min, are all linear algorithms. – A logarithmic time algorithm is one with running time O(log n). • Binary search algorithm is a logarithmic time algorith CP104 Introduction to Programming String Lecture 27 __ 4 Comparison and exchange in selection sort CP104 Introduction to Programming String Lecture 27 __ 5 More problems about string operation • • • • • Count the number of a particular string in a file Delete a substring from a file Copy a string into a particular position of a file Insert a string in a given position of a file Copy one file to another file CP104 Introduction to Programming String Lecture 27 __ 6 Sentinel-Controlled Loop for String Input CP104 Introduction to Programming String Lecture 27 __ 7 3n+1 problem Consider the following algorithm: 1. input n 2. print n 3. if n = 1 then STOP 4. if n is odd then n = 3n+1 5. else n = n/2 6. GOTO 2 For example: given 13 the following sequnce of n will be output for n: 40 20 10 5 16 8 4 2 1 write a program test all input integers n < 10000. Conjecture: the algorithm above will terminate (when a 1 is printed) for any integer input value. CP104 Introduction to Programming String Lecture 27 __ 8 An Array of Pointers CP104 Introduction to Programming String Lecture 27 __ 9 Two Orderings of One List Using an Array of Pointers (I) CP104 Introduction to Programming String Lecture 27 __ 10 Two Orderings of One List Using an Array of Pointers (II) CP104 Introduction to Programming String Lecture 27 __ 11 Two Orderings of One List Using an Array of Pointers (III) CP104 Introduction to Programming String Lecture 27 __ 12 Structure Chart for Text Editor Program (I) CP104 Introduction to Programming String Lecture 27 __ 13 Text Editor Program (II) CP104 Introduction to Programming String Lecture 27 __ 14 Text Editor Program (III) CP104 Introduction to Programming String Lecture 27 __ 15 Text Editor Program (IV) CP104 Introduction to Programming String Lecture 27 __ 16 Text Editor Program (V) CP104 Introduction to Programming String Lecture 27 __ 17 Text Editor Program (VI) CP104 Introduction to Programming String Lecture 27 __ 18 Text Editor Program (VII) CP104 Introduction to Programming String Lecture 27 __ 19 Sample Run of Text Editor Program CP104 Introduction to Programming String Lecture 27 __ 20