Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pseudocode A way to make programming easier Start with a verbal description of what the program is supposed to do! Slowly transform it into C, defining.

Similar presentations


Presentation on theme: "Pseudocode A way to make programming easier Start with a verbal description of what the program is supposed to do! Slowly transform it into C, defining."— Presentation transcript:

1 Pseudocode A way to make programming easier Start with a verbal description of what the program is supposed to do! Slowly transform it into C, defining variables as you go

2 Exercise - example Write a program that gets a string from the user and checks whether or not it is a palindrome Example for a palindrome: abbcbba (Hint: use strlen…)

3 Exercise – solution step 1 Read a string from the user If it is a palindrome, print so End the program

4 … and in C /* This program checks whether a given string is a palindrome*/ #include int main(void) { char str[101]; printf("Enter a string\n"); scanf("%100s",str); if (isPalindrome(str) == 1) printf("The string is a palindrome!\n"); else printf(“The string is NOT a palindrome!\n”); return 0; }

5 Exercise – solution step 2 Implementing the function… Ask myself – how do I know abbcbba, for example, is a palindrome? One way – when I read the letters from left to right, it’s the same as when I read from right to left A possible solution: copy first string in reverse, and compare to the original

6 … and in C int isPalindrome(char str[]) { char reverse[101]; COPY str IN REVERSE TO reverse if str AND reverse ARE IDENTICAL return 1; else return 0; }

7 How to copy a string in reverse? Start at the end and work your way backwards What variables would we need? One for the next place to copy to One for the next place to copy from

8 … and in C int isPalindrome(char str[]) { char reverse[101]; int from, to; to = 0; for (from = END OF str; from>=0; from--) { reverse[to] = str[from]; to++; } …

9 … and in C int isPalindrome(char str[]) { char reverse[101]; int from, to; to = 0; for (from = strlen(str); from>=0; from--) { reverse[to] = str[from]; to++; } …

10 … and in C int isPalindrome(char str[]) { char reverse[101]; int from, to; to = 0; for (from = strlen(str)-1; from>=0; from--) { reverse[to] = str[from]; to++; } …

11 … and in C int isPalindrome(char str[]) { char reverse[101]; int from, to; to = 0; for (from = strlen(str)-1; from>=0; from--) { reverse[to] = str[from]; to++; } reverse[to] = ‘\0’; …

12 Continuing the function … if str AND reverse ARE IDENTICAL return 1; else return 0; }

13 Continuing the function … if (strcmp(str, reverse) == 0) return 1; else return 0; }

14 Great! But what if we want to not use a second array? Can we ‘read’ from the start and from the end at the same time? And what if we want to ignore non- letters and letter-case? For example – Madam, I’m Adam should would be a palindrome

15 Solution palindrome.c advanced_pal.c

16 Exercise Implement the function void my_strcat(char s1[], char s2[]); Takes two strings, and copies s2 at the end of s1 Write a program that takes two strings from the user and prints their concatenation

17 Solution strcat.c

18 String in string Write a function that takes two strings, str1 and str2, and returns the index of the first time str2 occurs in str1, or -1 if that doesn’t happen For str1 = “abcde”, str2 = “bc”, return 1 For str1 = “abbcc”, str2 = “rg”, return -1

19 Solution – step 1 Ask yourself – how do you know where str2 occurs in str1? For example – str1 = “abbccba”, str2 = “bc” How do you know you shouldn’t return 0? How do you know you shouldn’t return 1? How do you know you should return 2?

20 Solution – pseudocode 1 int strstr(char str1[], char str2[]) { for each location in str1 if str2 occurs in str1 starting in that location return that location if we didn’t find str1 anywhere return -1;

21 Solution – pseudocode 2 int strstr(char str1[], char str2[]) { int i; for (i=0; i<strlen(str1); i++) { if str2 occurs in str1 starting in i return i; } if we didn’t find str1 anywhere return -1;

22 Solution – pseudocode 3 int strstr(char str1[], char str2[]) { int i; for (i=0; i<strlen(str1); i++) { if (occursAtI(str1, str2, i)==1) return i; } if we didn’t find str1 anywhere return -1;

23 Solution – pseudocode 4 int strstr(char str1[], char str2[]) { int i; for (i=0; i<strlen(str1); i++) { if (occursAtI(str1, str2, i)==1) return i; } return -1;

24 Solution – pseudocode 4 int occurs(char str1[], char str2[], int index) { run over str2 if for every letter there’s a corresponding letter in str1, starting at index, return 1; otherwise return 0; }

25 Solution – pseudocode 5 int occurs(char str1[], char str2[], int index) { run over str2 for each letter if there’s no corresponding letter in str1, starting at index return 0; if we found corresponding letters for ALL letters in str2 return 1; }

26 Solution – pseudocode 6 int occurs(char str1[], char str2[], int index) { int i; for (i=0; i<strlen(str2); i++) { if there’s no corresponding letter in str1, starting at index return 0; } if we found corresponding letters for ALL letters in str2 return 1; }

27 Solution – pseudocode 7 int occurs(char str1[], char str2[], int index) { int i; for (i=0; i<strlen(str2); i++) { if (str1[index+i] != str2[i]) return 0; } if we found corresponding letters for ALL letters in str2 return 1; }

28 Solution – pseudocode 8 int occurs(char str1[], char str2[], int index) { int i; for (i=0; i<strlen(str2); i++) { if (str1[index+i] != str2[i]) return 0; } return 1; }

29 Another exercise Implement the following function: Input – str1, str2, integer n Output – 1 if str1 is at most n letters away from str2, 0 otherwise isSimilar(“watar”, “woter”, 2) == 1 isSimilar(“watar”, “woter”, 1) == 0 If the strings are a different length return 0

30 solution is_similar.c

31 Could it be another one? :O Implement the following function – int isInArray(int[] arr, int len, int val) Should return 1 if arr contains val, 0 otherwise Implement the following function – int differentNums(int[] arr, int len) Should return how many distinct numbers are in arr – {1, 2, 4, 1, 5, 1} -> 4 Write a program that takes in numbers from the user and returns how many distinct ones there were

32 solution different_nums.c

33 Last one Implement the following function – Input – two strings str1, str2 Output – pointer to the first instance in str1 of any of the characters contained in Hint - use strchr! Write a program that accepts a string from the user and replaces all punctuation signs (,.;:!?) with spaces

34 solution strcspn.c


Download ppt "Pseudocode A way to make programming easier Start with a verbal description of what the program is supposed to do! Slowly transform it into C, defining."

Similar presentations


Ads by Google