COP 3275 – Character Strings Instructor: Diego Rivera-Gutierrez.

Slides:



Advertisements
Similar presentations
Lecture 3 Some commonly used C programming tricks. The system command Project No. 1: A warm-up project.
Advertisements

Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Introduction to C Programming
Strings Input/Output scanf and printf sscanf and sprintf gets and puts.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
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
Array_strcpy void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; }
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Kernighan/Ritchie: Kelley/Pohl:
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Chapter 10.
Strings and Dynamic Memory Allocation CS-2301, B-Term Programming Assignment #6 Strings and Dynamic Memory Allocation CS-2301, System Programming.
Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
The preprocessor and the compilation process COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
Week 2 - Monday.  What did we talk about last time?  Software development  Lab 1.
Files COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
Chapter 6 Buffer Overflow. Buffer Overflow occurs when the program overwrites data outside the bounds of allocated memory It was one of the first exploited.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
1 計算機程式設計 Introduction to Computer Programming Lecture01: Introduction and Hello World 9/10/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
C Programming in Linux Jacob Chan. C/C++ and Java  Portable  Code written in one system and works in another  But in C, there are some libraries that.
COP Structures Instructor: Diego Rivera-Gutierrez I’m back baby!
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, Summer 2013 C.
(9-1) Strings I H&K Chapter 8 Instructor - Andrew S. O’Fallon CptS 121 (October 19, 2015) Washington State University.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Functions & Pointers in C Jordan Erenrich
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
1 CSC103: Introduction to Computer and Programming Lecture No 24.
Pointers COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Arrays as pointers and other stuff COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
COP 3275 – Character Strings and Introduction to Pointers Instructor: Diego Rivera-Gutierrez.
ENEE150 – 0102 ANDREW GOFFIN Strings. Project 2 Flight Database 4 options:  Print flight  Print airport  Find non-stop flights  Find one-stop flights.
Today’s Material Strings Definition Representation Initialization
Multi-dimensional Arrays and other Array Oddities Rudra Dutta CSC Spring 2007, Section 001.
Announcements You will receive your scores back for Assignment 2 this week. You will have an opportunity to correct your code and resubmit it for partial.
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
Dr. Sajib Datta Feb 21,  In the last class we discussed: ◦ Bubble sort  How it works  performance.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Week 6 - Friday.  What did we talk about last time?  Pointers  Passing values by reference.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
Dr. Sajib Datta  Ordering elements in some way  For numeric data, ascending order is the most common  Lots of techniques for sorting  These.
COP 3275 – Finishing Loops and Beginning Arrays Instructor: Diego Rivera-Gutierrez.
Week 2 - Wednesday CS 121.
User-Written Functions
Strings CSCI 112: Programming in C.
Overview 4 major memory segments Key differences from Java stack
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
A bit of C programming Lecture 3 Uli Raich.
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
Overview 4 major memory segments Key differences from Java stack
C Stuff CS 2308.
Arrays as pointers and other stuff
Presentation transcript:

COP 3275 – Character Strings Instructor: Diego Rivera-Gutierrez

Administrative stuff Next week is break week! (From M 22 nd to F 26 th ) Hope you guys have a good break! Quiz #5 is posted. Due Monday 29 th before class. LATE POLICY DOES NOT APPLY TO THIS QUIZ If you haven’t submitted by Canvas deadline, you get a 0. No excuses. Homework #4 was fixed (error regarding opening) The solutions of HW #2 and #3 were posted You are free to start HW#4 based on my solution.

O (open a tile) O: Open the tile. If it is an empty space…. All adjacent tiles that are not mines get open too.

Character Strings They store text! In essence they are character arrays. We have been using strings a lot! All of our printfs and scanfs included strings in one way or another.

Character Strings Type? char * ; //pointer char [ ]; //array Literals? "Anything inside double quotation marks" Printf? printf("%s", "this works fine"); char *str = "this works fine"; printf("%s", str); printf(str); //this is a warning, but actually works

Dealing with variable sizes One of the advantages of the pointer (char *)notation: We don’t have to worry about size. If we use array (char []) we can keep the length in a separate int. We can always declare functions that always receive the length. However, it becomes tedious to keep track of length for each string. The trick is “null termination” ('\0'). But… why do we need it?

"Break" char string[5] = {'B','r','e','a','k'}; Break string I need to keep track of the size (5). What happens if I want to store “Case” or “a” in string? What if it was “Summer”?

"Break" char string[6] = {'B','r','e','a','k','\0'}; Break string \0

"Break" char string[6] = {'B','r','e','a','k','\0'}; string = "Case"; Break string \0Creak string \0Caeak string \0Casek string \0Case string \0 No longer need to keep track of the 6, right?

"Break" char string[6] = {'B','r','e','a','k','\0'}; string = "Case"; string = "a"; Case\0 string \0aase string \0a se string \0 No need to clean the rest, as soon as we hit \0, we know it’s the end. What if it was “Summer”? Well that’s why the pointer notation is useful

Null termination Lets us know where a string ends No need to explicitly keep track of size (on most cases) It’s the default if we use the literals: char *string = "Break"; Break string \0 Potentially allocates more spaces than needed (depending on code)

Accessing characters Similar to arrays we can access characters using []. So for the previous example: char *string = "Break"; char c = string[0]; //that accesses the 'B' char d = string[5]; //that accesses the '\0'

Length of a string int length(char *s) { int len = 0; while(s[len] != '\0') { len++; } return len; }

Length of a string int length(char *s) { int len = 0; while(s[len]) { len++; } return len; } Slightly more efficient!

Now… console parameters Have you noticed that we do: $ gcc HW3.c –o HW Turns out gcc, is a compiler written in C! It’s complicated! Take a compilers class or read about bootstrapping if you want to know how that works. How does it read the file name for our code from console? How does it read the output file?

Console parameters We have been purposedly ignoring any parameters that get sent to main! That’s why we write: int main ( void ); I am specific about doing that, because it makes the ignoring of those parameters very explicit. However, main can have parameters! It actually has 2 parameters!

Console parameters The common way to write it is: int main (int argc, char **argv); //more standard int main (int argc, char *argv[]); //easier to understand argc says how many parameters there are argv is an array of parameters. The parameters we get are separated by spaces. So if I type: $ gcc HW3.c –o HW What happens?

Console parameters gcc HW3.c –o HW argc would be ? argv would be: gccHW3.c-oHW argv

Console parameters gcc HW3.c –o HW argc would be 4 argv would be: gccHW3.c-oHW argv