CS1100 Introduction to Programming

Similar documents
There are functions to handle strings, so we will see the notion of functions itself in little a detail later. (Refer Slide Time: 00:12)

Array Initialization

Memory, Arrays & Pointers

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

Pointers, Arrays, and Strings. CS449 Spring 2016

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University

Procedural Programming & Fundamentals of Programming

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

CS 33. Introduction to C. Part 5. CS33 Intro to Computer Systems V 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

CS1100 Introduction to Programming

Chapter 5. Section 5.4 The Common String Library Functions. CS 50 Hathairat Rattanasook

Computers Programming Course 11. Iulian Năstac

Programming. Pointers, Multi-dimensional Arrays and Memory Management

Computer Programming: Skills & Concepts (CP) Strings

CS1100 Introduction to Programming

C strings. (Reek, Ch. 9) 1 CS 3090: Safety Critical Programming in C

C Programming Language Review and Dissection III

CSCE150A. Introduction. Basics. String Library. Substrings. Line Scanning. Sorting. Command Line Arguments. Misc CSCE150A. Introduction.

Introduction to Programming Block Tutorial C/C++

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 9. Strings. Notes. Notes. Notes. Lecture 07 - Strings

Computer Science & Engineering 150A Problem Solving Using Computers

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

PDS Class Test 2. Room Sections No of students

String constants. /* Demo: string constant */ #include <stdio.h> int main() {

COP 3223 Final Review

Computers Programming Course 10. Iulian Năstac

Fundamentals of Programming & Procedural Programming

CSE 230 Intermediate Programming in C and C++ Arrays, Pointers and Strings

Using Character Arrays. What is a String? Using Character Arrays. Using Strings Life is simpler with strings. #include <stdio.

I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub

Lecture 04 Introduction to pointers

by Pearson Education, Inc. All Rights Reserved.

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Arrays, Pointers, and Strings

Arrays and Strings. CS449 Fall 2017

Iosif Ignat, Marius Joldoș Laboratory Guide 9. Character strings CHARACTER STRINGS

CS 137 Part 6. ASCII, Characters, Strings and Unicode. November 3rd, 2017

Procedural Programming

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

ECE 551D Spring 2018 Midterm Exam

CS 61c: Great Ideas in Computer Architecture

Create a Program in C (Last Class)

Topics so far. Review. scanf/fscanf. How data is read 1/20/2011. All code handin sare at /afs/andrew/course/15/123/handin

Reading Assignment. Strings. K.N. King Chapter 13. K.N. King Sections 23.4, Supplementary reading. Harbison & Steele Chapter 12, 13, 14

COP 3223 Final Review

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

Week 5 9 April 2018 NWEN 241 More on pointers. Alvin Valera. School of Engineering and Computer Science Victoria University of Wellington

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Write a C program using arrays and structure

Grade Distribution. Exam 1 Exam 2. Exams 1 & 2. # of Students. Total: 17. Total: 17. Total: 17

Princeton University Computer Science 217: Introduction to Programming Systems. Modules and Interfaces

CS 449 Lecture 3. Variables in memory. Dr. Jack Lange. int x = 4;

Do not start the test until instructed to do so!

Problem 2 Add the two 2 s complement signed 8-bit values given below, and express your answer in decimal.

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Week 5. Muhao Chen.


mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

ESC101N: Fundamentals of Computing End-sem st semester

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Fundamental of Programming (C)

C: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16

Principles of C and Memory Management

DAY 3. CS3600, Northeastern University. Alan Mislove

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Final Intro to C Review

int Return the number of characters in string s.

Exercise 1.1 Hello world

CSC 2400: Computer Systems. Arrays and Strings in C

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

Programming in C. Session 7. Seema Sirpal Delhi University Computer Centre

Data Structures and Algorithms(4)

Pointers in C/C++ 1 Memory Addresses 2

Instructions: Submit your answers to these questions to the Curator as OQ02 by the posted due date and time. No late submissions will be accepted.

8. Characters, Strings and Files

Contents of Lecture 3

Algorithms & Data Structures

CS 31 Discussion: Week 6. Taylor Caulfield

Strings. Steven R. Bagley

C programming language continued: Recursion, strings and pointer

Arrays and Pointers. CSE 2031 Fall November 11, 2013

CS106L Handout #05 Fall 2007 October 3, C Strings

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

[0569] p 0318 garbage

Goals of this Lecture

Arrays and Pointers in C. Alan L. Cox

Arrays and Pointers in C & C++

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Pointers, Dynamic Data, and Reference Types

ECE 15B COMPUTER ORGANIZATION

EM108 Software Development for Engineers

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

This is CS50. Harvard University Fall Quiz 0 Answer Key

Character Array. C Programming. String. A sequence of characters The last character should be \0 that indicates the end of string 5 \0

ECE551 Midterm Version 1

Transcription:

CS1100 Introduction to Programming Sorting Strings and Pointers Madhu Mutyam Department of Computer Science and Engineering Indian Institute of Technology Madras Lexicographic (Dictionary) Ordering Badri < Devendra Janak < Janaki Shiva < Shivendra Seeta < Sita Based on the ordering of characters A < B < Y < Z < a < b < c <... < y < z upper case before lower case Course Material SD, SB, PSK, NSN, DK, TAG CS&E, IIT M 1 2 Lexicographic Ordering What about blanks? Bill Clinton < Bill Gates Ram Subramanian < Ram Subramanium Ram Subramanian < Rama Awasthi In ASCII the blank (code = 32) comes before all other characters. The above cases are taken care of automatically. Exercise: Look up ASCII codes on the web. 3 Lexicographic Ordering What if two names are identical? There is a danger that the character arrays may contain some unknown values beyond \0 Solutions One could begin by initializing the arrays to blanks before we begin. One could explicitly look for the null character \0 When the two names are equal it may not matter if either one is reported before the other. Though in stable sorting there is a requirement that equal elements should remain in the original order. 4 Comparing Strings (char Arrays) Given two strings A[i][] and A[j][] of length n, return the index of the string that comes earlier in the lexicographic order int strcompare(char A[ ][MAX_SIZE], int i, int j, int MAX_SIZE){ Skip common characters if any int k=0; while ((A[i][k] == A[j][k]) && k<max_size) k++; if (A[i][k] == \0 ) return i; if (A[j][k] == \0 ) return j; If one string is prefix if (A[i][k] < A[j][k]) return i; of the other return that else return j; Built-in String Comparison Pointers - address of char array #include <string.h> we will look at them later int strcmp(const char *s1, const char *s2); int strncmp(const char *s1, const char *s2, size_t n); int strcmp(char*, char*) compares two strings (char arrays) The return values are: 0 If both strings are equal 1 If first string is lexicographically greater than second -1 If second string is lexicographically greater than first 5 6 1

Other Built-in String Functions char* strcat(char* dest, char* src) Strcat combines two strings and returns a pointer to the destination string. In order for this function to work (and not seg fault), you must have enough room in the destination for both strings. char* strcpy(char* dest, char* src) Strcpy copies one string to another. The destination must be large enough to accept the contents of the source string. int strlen(const char* s) Strlen returns the length of a string, excluding \0 7 ArrayCopy Copies content of i th row of array into the j th row array[i][ ] array[j][ ] void arraycopy (char array[ ][MAX_SIZE], int i, int j, int MAX_SIZE) { int k; for (k =0; k < MAX_SIZE; k++) array[j][k] = array[i][k]; 8 ArraySwap array[i][ ] array[j][ ] void arrayswap (char array[ ][MAX_SIZE], int i, int j, int MAX_SIZE){ for (k =0; k < MAX_SIZE; k++) swap(array, i, j, k); Note: We exchange the entire arrays. If we knew the length of the longer string, we could have a different end condition. 9 Sorting String Arrays Modify InsertionSort to sort array names[ ] of names Use strcompare to compare names Use arraycopy to move names In the exercise where names[ ] and marks[ ] have to be sorted in concert, modify the sorting algorithm to compare in one array names[ ] for alphabetic order marks[ ] for decreasing marks order move elements of both Compact structures to hold both to be explored later 10 Printing a Reversed String main( ) { int i = 4; do{ c = "hello"[i]; printf("%c",c); i --; while(i >= 0); printf("\n"); 11 Palindromes Strings/sequences that read the same left to right or right to left string == reversed string malayalam god live evil dog able was I ere I saw elba don t nod never odd or even notice that we ignore blanks (4, 5) and other characters (4) preprocess the string to remove them 12 2

Reversing an Array Swap the first element with last a(0) with a(n 1) second with second last a(1) with a(n 2) a(i) with a((n 1) i) How about the following code? for (i=0; i<n; i++) swap (a, i, n-1-i); void swap (char a[ ], int i, int j){ c = a[i]; a[i]=a[j]; a[j]=c; Limits for Iteration reverse S N A K E E N A K S 13 i=0 i=1 i=2 i=3 i=4 E K A N S E K A N S E N A K S S N A K E for (i=0; i<n; i++) swap (a, int i, int n 1 i); Job done! Stop at halfway mark! for(i=0; i<n/2; i++) void swap (char a[ ], int i, int j){ c = a[i]; a[i]=a[j]; a[j]=c; 14 Exercise Compute the transpose of a matrix Compute in place transpose of a square matrix T A B L E A C E D O S T E E P T A S A C T B E E L D E E O P Palindrome Squares Write a program to check if a square matrix contains a palindrome table. Two examples are given below from http://www.fun-with-words.com/palin_example.html S T E P T I M E E M I T P E T S R A T S A B U T T U B A S T A R 15 16 Concatenating Two Strings S t r i n g i n C \0... A n o t h e r s t r i n g i n C \0... /* Arrays of strings */ #include <stdio.h> void main() { char str[ ][40] = {"String in C, "Another string in C ; int count1 = 0; /* Length of first string */ int count2 = 0; /* Length of second string */ /* find the length of the strings */ while (str[0][count1]!= '\0') count1++; /* 11 */ while (str[1][count2]!= '\0') count2++; /* 19 */ 17 Concatenating Two Strings S t r i n g i n C \0... A n o t h e r s t r i n g i n C \0... /* Check that we have enough space for both strings */ if (sizeof str[0] < count1 + count2 + 1) printf("\n Not enough space for both strings."); else { /* Copy 2nd string to first */ int i = count1, j = 0; while((str[0][i++] = str[1][j++])!= '\0'); printf("\n%s", str[0]); /* Output combined string */ 18 3

What is a Pointer? Recap: a variable int k Names a memory location that can hold one value at a time Memory is allocated statically at compile time One name one value A pointer variable int *p Contains the address of a memory location that contains the actual value Memory can be allocated at runtime One name many values k 38 100 *p p 100 250 200 *p m 84 Addr 250 19 l-value and r-value Given a variable k Its l-value refers to the address of the memory location l-value is used on the left side of an assignment Ex. k = expression Its r-value refers to the value stored in the memory location r-value is used in the right hand side of an assignment Ex. var = k + Pointers allow one to manipulate the l-value! 20 Pointer Variables Pointer variables are variables that store the address of a memory location Memory required by a pointer variable depends upon the size of the memory in the machine one byte could address a memory of 256 locations two bytes can address a memory of 64K locations four bytes can address a memory of 4G locations modern machines have RAM of 1GB or more The task of allocating this memory is best left to the system 21 Declaring Pointers Pointer variable precede its name with an asterisk Pointer type - the type of data stored at the address For example, int *p; p is the name of the variable. The * informs the compiler that p is a pointer variable The int says that p is used to point to an integer value Ted Jenson s tutorial on pointers http://pweb.netcom.com/~tjensen//cpoint.htm 22 Contents of Pointer Variables In ANSI C, if a pointer is declared outside any function, it is initialized to a null pointer For example, int k; int *p; p = &k; //assigns the address of int k to p if (p == NULL) //tests for a null pointer p = malloc(sizeof(int)); //dynamic allocation, //creates an anonymous int // in memory at runtime Dereferencing Operator The asterisk symbol is the "dereferencing operator" and it is used as follows * = 7; Will copy 7 to the address pointed to by Thus if "points to" (contains the address of) k, the above statement will set the value of k to 7 Using '*' is a way of referring to the value in the location which is pointing to, but not the value of the pointer itself printf("%d\n",*); --- prints the number 7 23 24 4

short int Pointer short *; says that is the address of a short integer type short allocates two bytes of memory 100 101 102 103 104 105 * = 20; //store the value 20 in the above two bytes if we had said int * it would have allocated 4 bytes of memory Pointer Arithmetic = +1; says to point to the next data item after this one Makes sense only for same type data eg. an array of integers 25 26 Memory Needed for a Pointer A pointer requires two chunks of memory to be allocated: Memory to hold the pointer (address) Allocated statically by the pointer declaration Memory to hold the value pointed to Allocated statically by a variable declaration OR allocated dynamically by malloc( ) One variable or pointer declaration à allocation of one chunk of memory 27 5