Strings …again.

Slides:



Advertisements
Similar presentations
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 1 Overview 8.1 An Array Type for Strings 8.2 The Standard string.
CMPUT 101 Lab # 5 October 22, :00 – 17:00.
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Program Input and the Software Design Process ROBERT REAVES.
Basic Elements of C++ Chapter 2.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library.
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
Lecture 3: Getting Started & Input / Output (I/O) “TRON” Copyright 1982 (Walt Disney Productions)
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
C is a high level language (HLL)
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
Lecture 3: Getting Started & Input / Output (I/O)
Operator Overloading Introduction
Basic concepts of C++ Presented by Prof. Satyajit De
The Second C++ Program Variables, Types, I/O Animation!
Topic 2 Input/Output.
Review 1.
C++ Basic Input and Output (I/O)
Chapter Topics The Basics of a C++ Program Data Types
User-Written Functions
Chapter 7 Pointers and C-Strings
CSE 220 – C Programming C Fundamentals.
Motivation and Overview
© 2016 Pearson Education, Ltd. All rights reserved.
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Basic Elements of C++.
Introduction to C++ October 2, 2017.
Basic Elements of C++ Chapter 2.
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Input/Output Handouts: Quiz 2, Unit 3 practice sheets.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
CS 2308 Exam I Review.
INPUT & OUTPUT scanf & printf.
Functions, Part 1 of 3 Topics Using Predefined Functions
Strings A collection of characters taken as a set:
Functions, Part 2 of 3 Topics Functions That Return a Value
We’re moving on to more recap from other programming languages
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 3: Input/Output
Wednesday 09/23/13.
Chapter 3 Input output.
Chapter 3: Expressions and Interactivity
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Introduction to C Topics Compilation Using the gcc Compiler
Functions, Part 1 of 3 Topics Using Predefined Functions
Functions continued.
Arrays Arrays A few types Structures of related data items
Fundamental Programming
C++ for Engineers and Scientists Second Edition
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Chapter 1 c++ structure C++ Input / Output
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
CPS125.
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

Strings …again

The road so far Up until now we have been using the header file string.h to help us manipulate strings. This has made our lives easier in many ways but it also hides important information from us.

Strings as Arrays Recall that a string is a sequence of characters. As such the natural structure to contains this information is an array. In C / C++ a string is actually an array of characters.

To properly declare a string using an array of characters: char name[10]; Notice that we need to decide the size of the array before the program compiles. This is problematic if we don’t know how large we want the string to be.

Another problem is that we cannot directly assign a string to the array. For example we cannot do the following: char name[10]; name = “bob” This is because the “bob” is not 10 characters long.

We can, however, have the user input the string “bob”. Ex: char name[10]; cout << “please enter your name” <<endl; cin >> name; If the user enters a string that is longer than 10 characters than only the first 10 will be used.

The most important reason why we may not want to use the string variable type is because: Most of the predefined functions in C do not accept them as parameters!

Ex: The fprintf command accepts parameters that include arrays of characters but does not include strings! Ex: string name=“Charles”; fprintf(file,” %s”, name); will not run Ex: char name[10]= “Charles”; fprintf(file,”%s”, name); will run

printf command The printf command is a C command. (But still works in C++) It performs the function as the cout command but with much more versatility. With this added versatility comes a much more complicated syntax.

Printf - Syntax Printf(const * char[], variables) The printf command takes a string a the first argument. The string itself is formated in a special way that uses placeholders to represent variables.

Syntax (cont) Ex: char name[10]=“Charles”; printf(“Hello my name is %s ”, name); The %s is a placeholder for a string. The subsequent arguments represent the variable we want to use to replace the place holders. The output is: “Hello my name is Charles”

Formatting using printf Specifier (placeholder) Description c Character s string d Signed integer f float e Scientific notation

There are many other options! Read the following articles: http://www.cplusplus.com/reference/clibrary/cstdio/printf/ http://www.cplusplus.com/reference/clibrary/cstdio/scanf/ Then practice using these command instead of using cin and cout.