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.

Slides:



Advertisements
Similar presentations
Problem Solving & Program Design in C
Advertisements

Introduction to C Programming
8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
C Programming lecture 2 Beautiful programs Greek algorithms
 A string is an array of characters.  Strings must have a 0 or null character after the last character to show where the string ends.  The null character.
Strings string.h library. String Library Functions Dr. Sadık EşmelioğluCENG 1142 NameDescription strlen return the length of string not counting \0 strcopy.
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 09 Strings, IDEs. METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 29, 2002.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Strings CS240 Dick Steflik. What is a string A null terminated array of characters: char thisIsAString[10]; \0 The “\0” (null character)
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
Chapter Fourteen Strings Revisited. Strings A string is an array of characters A string is a pointer to a sequence of characters A string is a complete.
Array_strcpy void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; }
Sort the given string, without using string handling functions.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
1 Strings ( מחרוזות ). 2 Agenda Definition and initialization Termination Input / Output String library.
Programming Summary. Exercise – Remove Duplicates Download remove_duplicates_ex.c and implement the function void remove_duplicates(Node *head); remove_duplicates()
Arrays Ethan Cerami New York University Today n Array Basics (Review) n Random Number Example n Passing Arrays to Functions n Strings.
Exercise 10 Review: pointers, strings and recursion.
Tutorial #8 Summer strings #include int main() { char str1[] = {‘h’,’e’,’l’,’l’,’o’}; char str[] = {‘h’,’e’,’l’,’l’,’o’,’\0’}; char p[] = ”hello”;
C Programming Strings. Array of characters – most common type of array in C  Let’s make them easier for use Denote the end of array using a special character.
Strings. Sentences in English are implemented as strings in the C language. Computations involving strings are very common. E.g. – Is string_1 the same.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
Strlen() implementation /* strlen : return length of string s */ int strlen(char *s) { int n; for (n = 0 ; s[n] != ‘\0’ ; n++) ; return n; } /* strlen.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
Recursion A method is recursive if it makes a call to itself. A method is recursive if it makes a call to itself. For example: For example: public void.
Recursion.
CPT: Strings/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship.
CS 162 Introduction to Computer Science Chapter 17 C++ String Objects Herbert G. Mayer, PSU (Copied from Prof. Phillip Wong at PSU) Status 11/30/2014.
STARTING OUT WITH STARTING OUT WITH Class 9 Honors.
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
String functions+ string I.Mona Alshehri. String Functions: Header file:#include Function: Int strlen(char s[n]) Description Calculates the length of.
EXERCISE Arrays, structs and file processing. Question You own a pet store. You want to keep an inventory of all the pets that you have. Pets available.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
String Array (Multidimensional Arrays) 1. A string array is a multidimensional array of strings. It is declared in the following syntax: char variable_name[No_of_strings][size_of_each_string];
String operations. About strings To handle strings more easily, we need to include a library> #include To see what the library allows us to do, look here:
Slides from Shane Griffith (TA and guest instructor in Fall 2008) CprE 185: Intro to Problem Solving.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Characters and Strings Functions.
Pointers and Arrays An array's name is a constant whose value is the address of the array's first element. For this reason, the value of an array's name.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
 Learn how to form strings using one-dimensional array  String manipulation functions:  strcpy  strrev  strcmp  Program using strings.
Computer Programming Arrays 1. Question #1 2 Question Choose the correct answer..
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
Topic 6 Recursion.
Lecture 8 String 1. Concept of strings String and pointers
Strings (מחרוזות).
Tutorial 8 Pointers and Strings
CS111 Computer Programming
CS 106A, Lecture 9 Problem-Solving with Strings
INC 161 , CPE 100 Computer Programming
Lecture 11 Strings.
Class Examples.
EECE.2160 ECE Application Programming
Chapter 8 Character Arrays and Strings
EECE.2160 ECE Application Programming
Strings #include <stdio.h>
Programming Strings.
CS-161 Computer Programming Lecture 15 & 16: Arrays II
Characters and Strings Functions
Lecture 20 – Practice Exercises 4
Introduction to Problem Solving and Programming
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

Solution palindrome.c advanced_pal.c

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<strlen(str1); i++) { if str2 occurs in str1 starting in i return i; } if we didn’t find str1 anywhere return -1;

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;

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;

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

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

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

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