# 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 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:

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

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…)

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

… 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; }

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

… 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; }

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

… 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++; } …

… 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++; } …

… 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++; } …

… 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’; …

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

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

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

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

Solution strcat.c

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

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?

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;

Solution – pseudocode 2 int strstr(char str1[], char str2[]) { int i; for (i=0; i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/11/3184229/slides/slide_21.jpg", "name": "Solution – pseudocode 2 int strstr(char str1[], char str2[]) { int i; for (i=0; i

Solution – pseudocode 3 int strstr(char str1[], char str2[]) { int i; for (i=0; i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/11/3184229/slides/slide_22.jpg", "name": "Solution – pseudocode 3 int strstr(char str1[], char str2[]) { int i; for (i=0; i

Solution – pseudocode 4 int strstr(char str1[], char str2[]) { int i; for (i=0; i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/11/3184229/slides/slide_23.jpg", "name": "Solution – pseudocode 4 int strstr(char str1[], char str2[]) { int i; for (i=0; i

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; }

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; }

Solution – pseudocode 6 int occurs(char str1[], char str2[], int index) { int i; for (i=0; i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/11/3184229/slides/slide_26.jpg", "name": "Solution – pseudocode 6 int occurs(char str1[], char str2[], int index) { int i; for (i=0; i

Solution – pseudocode 7 int occurs(char str1[], char str2[], int index) { int i; for (i=0; i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/11/3184229/slides/slide_27.jpg", "name": "Solution – pseudocode 7 int occurs(char str1[], char str2[], int index) { int i; for (i=0; i

Solution – pseudocode 8 int occurs(char str1[], char str2[], int index) { int i; for (i=0; i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/11/3184229/slides/slide_28.jpg", "name": "Solution – pseudocode 8 int occurs(char str1[], char str2[], int index) { int i; for (i=0; i

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

solution is_similar.c

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

solution different_nums.c

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

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