CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Introduction to C Programming
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
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.
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.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:
Chapter 10.
N-1 University of Washington Computer Programming I Lecture 19: Strings © 2000 UW CSE.
Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld.
Introduction to C Programming CE Lecture 13 Strings in C.
C. About the Crash Course Cover sufficient C for simple programs: variables and statements control functions arrays and strings pointers Slides and captured.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CS 201 String Debzani Deb.
Characters & Strings Lesson 2 CS1313 Spring Characters & Strings Lesson 2 Outline 1.Characters & Strings Lesson 2 Outline 2.Character String Declaration.
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Programming Languages -1 (Introduction to C) strings Instructor: M.Fatih AMASYALI
Introduction to C programming
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
CHAPTER 8 CHARACTER AND STRINGS
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
Programming I Introduction Introduction The only way to learn a new programming language is by writing programs in it. The first program to.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
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.
APS105 Strings. C String storage We have used strings in printf format strings –Ex: printf(“Hello world\n”); “Hello world\n” is a string (of characters)
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
1 This chapter covers both string constants (or literals, as they're called in the C standard) and string variables, which can change during the execution.
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
More Pointers and Arrays Kernighan/Ritchie: Kelley/Pohl: Chapter 5 Chapter 6.
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
 2003 Prentice Hall, Inc. All rights reserved. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
An Array Type For Strings. Two ways to represent strings – i.e. “Hello” cstring An array with base type char Older way of processing strings Null character.
Arrays and Pointers.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Strings, Pointers and Tools
Characters and Strings
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.
In C programming, one of the frequently arising problem is to handle similar types of data. For example: If the user want to store marks of 100 students.
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.
Chapter Nine Strings. Char vs String Literals Size of data types: Size of data types: –sizeof(“hello\n”)7 bytes –sizeof(“hello”)6 bytes –sizeof(“X”)2.
Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal may.
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.
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Computer Organization and Design Pointers, Arrays and Strings in C
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Fundamentals of Characters and Strings
Programming Languages -1 (Introduction to C) strings
Arrays in C.
Chapter 9: Pointers and String
Strings #include <stdio.h>
CS31 Discussion 1H Fall18: week 6
Presentation transcript:

CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4

2 The Data Type char The data type char can be thought of as either a character or an integer. Typically, a char has a value printf( “%c”, ‘a’ ); /* a is printed */ printf( “%d”, ‘a’ ); /* 97 is printed */ printf( “%c”, 97 ); /* a is printed */ ‘a’ == 97, ‘b’ == 98, …, ‘z’ == 112 ‘A’ == 65, ‘B’ == 66, …, ‘Z’ = 90 ‘0’ == 48, ‘1’ == 49, …, ’9’ == 57 ‘&’ == 38, ‘*’ == 42, …

3 Escape codes corresponding to characters For use inside in single-quotes, or double-quotes, for instance in passing a string to printf Exhaustive list in K&R (p. 193) Character Escape Sequence Integer Value Newline \n 10 Backslash (\) \\ 92 Backspace \b 8 Single quote \’ 39 Double quote \” 34 Horizontal tab \t 9 Question Mark \? 63 Null Character \0 0

4 Strings Strings are one-dimensional arrays of type char. Hence, they have type char *. By convention, a string in C is terminated by \0 (null character); thus it needs space equal to the size of string +1 A string constant is treated by the compiler as a pointer; also space in memory is allocated for storing the characters. When allocating char arrays that will hold strings, make sure you allocate enough space! char *p = “abc”; printf( “%s %s\n”, p, p+1 ); /* prints abc bc */ /* “abc”[1], *(“abc” + 2) make sense */ CornellUni\

5 Example: “Double” printing #include void new_print( char *s ) { int i; for( i = 0; s[i] != 0; i++ ) { printf( "%c%c", s[i], s[i] ); } void main() { new_print( “Cornell" ); }

6 Strings are also char pointers #include void new_print( char *s ) { while (*s) { printf( "%c%c", *s, *s ); s++; } void main() { new_print( “Cornell" ); }

7 Some standard string handling functions These are included in library “string.h” char *strcat( char *s1, char *s2 ); –Takes two strings as arguments, concatenates them, and puts the result in s1. The programmer must ensure that s1 points to enough space to hold the result. The string s1 is returned. int strcmp( char *s1, char *s2 ); –Integer is returned that is less than, equal to, or greater than zero, depending on whether s1 is lexicographically less than, equal to, or greater than s2. char *strcpy( char *s1, char *s2 ); –The string s2 is copied into s1. Whatever exists in s1 is overwritten. It is assumed that s1 has enough space to hold the result. The value s1 is returned. Implementation for some exist in K&R, but you try implementing them yourselves!

8 Example: “squeeze” function (from K&R) /* squeeze deletes all instances of c from s */ void squeeze( char s[], int c ) { int i, j; for( i = j = 0; s[i] != ‘\0’; i++ ) { if( s[i] != c ) { s[j] = s[i]; j++; } s[j] = ‘\0’; }

9 Multidimensional Arrays Declaration such as –int b[2][7]; or int c[5][3][2]; In first example, makes 14 ints available for use: –b[i][j] where 0 <= i <= 1, 0 <= j <= 6. #define SIZE 6 void clear_board( int board[SIZE][SIZE] ) { int i, j; for( i = 0; i < SIZE; i++ ) { for( j = 0; j < SIZE; j++ ) { board[i][j] = 0; }

10 Initializations int year[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; float x[4] = {1, }; (or float x[4]={1}; ) /* Initialization of rest to 0’s by default */ char uni[20] = “Cornell University”; char uni[] = “Cornell University”; char *uni = “Cornell University”; What is the difference between the three? float y[2][3] = { {1, 2, 3}, {2, 4, 6}}; float y[2][3] = { 1, 2, 3, 2, 4, 6}; /*same*/ What is the difference between the three? float y[2][3] = { {1}, {2, 4}}; float y[2][3] = { 1, 2, 4}; /*same? NO!*/