 Learn how to form strings using one-dimensional array  String manipulation functions:  strcpy  strrev  strcmp  Program using strings.

Slides:



Advertisements
Similar presentations
Chapter 9 Strings. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 9-2 Learning Objectives An Array Type for Strings C-Strings Character.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
1 Pointers and Strings Section 5.4, , Lecture 12.
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
 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.
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
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.
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.
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'; }
Introduction to Programming Lecture 34. In Today’s Lecture Arrays of objects Arrays of objects Interaction of Arrays with Free Store Interaction of Arrays.
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.
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:
1 CS 105 Lecture 8 Strings; Input Failure Mon, Mar 7, 2011, 3:39 pm.
1 String Library and Stream I/O Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series.
Command-line arguments CS 201 Fundamental Structures of Computer Science.
Friday, January 05, 2007 A few weeks of developing and testing can save a whole afternoon in the library. -Anonymous.
Lab 7 strings as arrays of chars. #include #include #include using namespace std; int printMenu( ); int hexSymbolValue( char c ); int readBinValue( bool.
Character Arrays strlen("hello, world"); /* string constant */ strlen(array); /* char array[100]; */ strlen(ptr); /* char *ptr; */ char pmessage[] = "now.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
CS Nov 2006 C-strings.
String What it is Why it’s useful library routines for handling strings how to input a string from the keyboard.
C-Strings Joe Meehean. C-style Strings String literals (e.g., “foo”) in C++ are stored as const char[] C-style strings characters (e.g., ‘f’) are stored.
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.
Characters & Strings Lesson 2 CS1313 Spring Characters & Strings Lesson 2 Outline 1.Characters & Strings Lesson 2 Outline 2.Character String Declaration.
 Write a program that uses a one dimension to do a table look-up  Learn about parallel arrays.
Strings Sujana Jyothi C++ Workshop Day 4. A String also called character string is a sequence of contiguous characters in memory terminated by the NUL.
In Addition... To the string class from the standard library accessed by #include C++ also has another library of string functions for C strings that can.
Chapter 8 Strings and Vectors (8.1 and 8.2). An Array of characters Defined as: char firstName[20]; char firstName[] = {‘T’, ‘i’, ‘m’}; // an array of.
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
 Review structures  Program to demonstrate a structure containing a pointer.
February 14, 2005 Characters, Strings and the String Class.
M. Taimoor Khan #include void main() { //This is my first C++ Program /* This program will display a string message on.
February 11, 2005 More Pointers Dynamic Memory Allocation.
C++ PROGRAMMING: PROGRAM DESIGN INCLUDING DATA STRUCTURES, FIFTH EDITION Chapter 10: Strings and string type.
9-1 Learning Objectives  An Array Type for Strings  C-Strings.
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
COIT29222-Structured Programming Lecture Week 08  Reading: Textbook (4 th Ed.), Chapter 4 Textbook (6 th Ed.), Chapter 7 Study Guide Book 2, Module 11.
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
C++ Programming Basics
Sahar Mosleh California State University San MarcosPage 1 Character String.
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.
String as Arrays, Array Sorting C++ Programming Technologies.
Data Types Storage Size Domain of all possible values Operations 1.
Strings Jarret Raim C Strings Same as arrays of characters. Use pointers. Require static declarations (compile time). No bounds checking. No easy.
1 Object-Oriented Programming Using C++ A tutorial for pointers.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Strings, Slide Fundamental Programming Strings.
Built-in Functions for NTCAs. strlen char array[10] = “Hello”; int length = strlen(array); cout
The Basics of Arrays Problem: How can the rancher easily catalog all of his cattle?
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
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.
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
for( 起始條件 ; 判斷式 ; 條件運算 ){ // 迴圈內容 } while( 判斷式 ){ // 迴圈內容 } do{ // 迴圈內容 } while( 判斷式 ) ;
Hello Educational presentation.
Command Line Arguments
Random Number Generation
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Introduction to Programming
The Stack.
Programming Strings.
Presentation transcript:

 Learn how to form strings using one-dimensional array  String manipulation functions:  strcpy  strrev  strcmp  Program using strings

char name[3] = {‘A', ‘L', ‘\0’}; ‘A’ ‘L’ ‘\0’ name[0] name[1] name[2]

char word[6] = {'H','e','l','l','o',‘\0'}; char word[6] = {"Hello"}; ‘H’ ‘e’ ‘l’ ‘o’ word[0] word[1] word[2] word[3] word[4] ‘\0’ word[5]

char word[ ] = {“Hello” };

int main() { char hisName[25]; cout > hisName; cout << "Hello " << hisName << endl; char myName[25]; strcpy(myName, "Helen"); strrev(hisName); cout << "My name is " << myName << " Your name reversed is " << hisName << endl; strrev(hisName);

int x = strcmp(hisName, myName); if (x == 0) cout << "Hey, my name is the same as yours!"; else if (x < 0) cout << "Your name comes before mine alphabetically"; else cout << "Your name comes after mine alphabetically"; cout << endl; return 0; }

int main() { char hisName[25]; cout > hisName; cout << "Hello " << hisName << endl;

char myName[25]; strcpy(myName, "Helen"); //cannot write: myName = “Helen”;

‘S’ ‘a’ ‘m’ ‘\0’ hisName[0] hisName[1] hisName[2] hisName[3]... ‘\0’ his Name[24] ‘H’ ‘e’ ‘l’ ‘e’ ‘n’ myName[0] myName[1] myName[2] myName[3] myName[4] ‘\0’ myName[5] myName[24] ‘\0’...

strrev(hisName); cout << "My name is " << myName << " Your name reversed is " << hisName << endl; strrev(hisName ); ‘m’ ‘a’ ‘S’ ‘\0’ hisName[0] hisName[1] hisName[2] hisName[3]... ‘\0’ his Name[24]

if ( hisName == myName) {... }

x = strcmp ( hisName, myName);

int x = strcmp(hisName, myName); if (x == 0) cout << "Hey, my name is the same as yours!"; else if (x < 0) cout << "Your name comes before mine alphabetically"; else cout << "Your name comes after mine alphabetically"; cout << endl; return 0; }

 Learn how to form strings using one-dimensional array  String manipulation functions:  strcpy  strrev  strcmp  Program using strings