Null-Terminated Character Arrays

Slides:



Advertisements
Similar presentations
EC-111 Algorithms & Computing Lecture #8 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Advertisements

Starting Out with C++, 3 rd Edition 1 Chapter 10 – Characters, Strings, and the string Class.
Operator Overloading. Introduction Operator overloading –Enabling C++’s operators to work with class objects –Using traditional operators with user-defined.
LECTURE 17 C++ Strings 18. 2Strings Creating String Objects 18 C-string C++ - string \0 Array of chars that is null terminated (‘\0’). Object.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
M ANIPULATING S TRINGS Resources [1] Object Oriented Programming with C++ (3 rd Edition) E Balagurusamy [2] Teach Yourself C++ (3 rd Edition) H Schildt.
Strings.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
1 Class Vehicle #include #define N 10../.. 2 Class Vehicle class vehicle { public: float speed; char colour[N+1]; char make[N+1];
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
1 Objectives Understand streamed input and output.
Programming Strings. COMP102 Prog. Fundamentals: Strings / Slide 2 Character Strings l A sequence of characters is often referred to as a character “string”.
CS161 Topic #14 1 Today in CS161 Lecture #14 Practicing! Writing Programs to Practice Write a program that counts the number of vowels in a sentence, ended.
 Write a program that uses a one dimension to do a table look-up  Learn about parallel arrays.
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
1 Chapter 9 Additional Control Structures Dale/Weems.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
String and Character Manipulation.
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
Chapter 15 Strings as Character Arrays
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Built-in Functions for NTCAs. strlen char array[10] = “Hello”; int length = strlen(array); cout
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
REEM ALAMER REVISION FOR C LANGUAGE COURSE. OUTPUTS int main (void) { int C1, C2; int *p1, *p2; C1 = 8; p1 = &C1; C2 = *p1 / 2 + 5; p2 = &C2; printf ("C1.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
4.1Introduction Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based arrays (C-like) –Arrays.
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
Data Structures and Algorithms
MT262A Review.
Introduction to Programming
Fundamentals of Characters and Strings
Objectives Identify the built-in data types in C++
CSC 113: Computer Programming (Theory = 03, Lab = 01)
Multi-dimensional Array
C++ Arrays.
CS 1430: Programming in C++.
CS 1430: Programming in C++.
Function Basics.
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
Popping Items Off a Stack Lesson xx
Additional Control Structures
Stack and Queues Stack implementation using Array
Learning Objectives String Class.
Working With Arrays.
Arrays Kingdom of Saudi Arabia
Review for Final Exam.
Strings A collection of characters taken as a set:
The Run-Time Stack and Reference Parameters
Lecture 12 Oct 16, 02.
Sequential input and output Operations in file
String class and its objects
الوحدة الرابعة البرمجة وصياغة حل المسائل البرمجة وأهميتها أهداف الدرس الأول مفهوم البرمجة. الفرق بين المبرمج ومستخدم البرنامج. الحاجة إلى البرامج.
String in C++.
String What it is Why it’s useful
Function Overloading.
CHAPTER 4 File Processing.
Arrays Arrays A few types Structures of related data items
Example 1 Ask the user to type10 integers of an array T1 and 10 integers of an array T2. Put into T3, an array with 20 integers : the sum of the elements.
C++ Programming Lecture 20 Strings
Character Arrays char string1[] = “first”;
Strings …again.
Arrays Prepared By Paritosh Srivastava PGT (CS) KV NHPC Banbasa.
4.1 Introduction Arrays A few types Structures of related data items
Odds and Ends.
Presentation transcript:

Null-Terminated Character Arrays

Character Arrays char my_array[10];

NULL-TERMINATED char array1[10] = {‘c’,’u’,’p’}; char array2[10] = “cup”; [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] c u p ? ? ? ? ? ? ? [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] c u p \0 ? ? ? ? ? ?

NULL-TERMINATED char array1[10] = {‘c’,’u’,’p’}; char array2[10] = “cup”; [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] c u p ? ? ? ? ? ? ? [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] c u p \0 ? ? ? ? ? ?

NULL-TERMINATED char name[10]; cout << “enter name: “; cin >> name; cout << name;

NULL-TERMINATED char name[10]; cout << “enter name: “; cin >> name; cout << name; [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] ? ? ? ? ? ? ? ? ? ?

NULL-TERMINATED char name[10]; cout << “enter name: “; cin >> name; // user inputs Ahmed cout << name; [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] ? ? ? ? ? ? ? ? ? ?

NULL-TERMINATED char name[10]; cout << “enter name: “; cin >> name; // user inputs Ahmed cout << name; [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] A h m e d \0 ? ? ? ?

NULL-TERMINATED char name[10]; cout << “enter name: “; cin >> name; cout << name; //Ahmed is displayed [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] A h m e d \0 ? ? ? ?

NULL-TERMINATED char name[10]; cout << “enter name: “; cin >> name; // user inputs Jo Ann cout << name; [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] ? ? ? ? ? ? ? ? ? ?

NULL-TERMINATED char name[10]; cout << “enter name: “; cin >> name; // user inputs Jo Ann cout << name; [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] J o \0 ? ? ? ? ? ? ?

NULL-TERMINATED char name[10]; cout << “enter name: “; cin >> name; cout << name; //Jo is displayed [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] J o \0 ? ? ? ? ? ? ?

Using the Null-Terminating Character // Pre: char array must have null character at the end of data. long string_length (const char ntca[]) { long length = 0; while (ntca[length] != ‘\0’) length++; return length; } [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] A h m e d \0 ? ? ? ?

Using the Null-Terminating Character // Pre: char array must have null character at the end of data. long string_length (const char ntca[]) { long length = 0; while (ntca[length] != ‘\0’) length++; return length; } length = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] A h m e d \0 ? ? ? ?

Using the Null-Terminating Character // Pre: char array must have null character at the end of data. long string_length (const char ntca[]) { long length = 0; while (ntca[length] != ‘\0’) length++; return length; } length = 0 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] A h m e d \0 ? ? ? ?

Using the Null-Terminating Character // Pre: char array must have null character at the end of data. long string_length (const char ntca[]) { long length = 0; while (ntca[length] != ‘\0’) length++; return length; } length = 1 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] A h m e d \0 ? ? ? ?

Using the Null-Terminating Character // Pre: char array must have null character at the end of data. long string_length (const char ntca[]) { long length = 0; while (ntca[length] != ‘\0’) length++; return length; } length = 1 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] A h m e d \0 ? ? ? ?

Using the Null-Terminating Character // Pre: char array must have null character at the end of data. long string_length (const char ntca[]) { long length = 0; while (ntca[length] != ‘\0’) length++; return length; } length = 2 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] A h m e d \0 ? ? ? ?

Using the Null-Terminating Character // Pre: char array must have null character at the end of data. long string_length (const char ntca[]) { long length = 0; while (ntca[length] != ‘\0’) length++; return length; } length = 2 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] A h m e d \0 ? ? ? ?

Using the Null-Terminating Character // Pre: char array must have null character at the end of data. long string_length (const char ntca[]) { long length = 0; while (ntca[length] != ‘\0’) length++; return length; } length = 3 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] A h m e d \0 ? ? ? ?

Using the Null-Terminating Character // Pre: char array must have null character at the end of data. long string_length (const char ntca[]) { long length = 0; while (ntca[length] != ‘\0’) length++; return length; } length = 3 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] A h m e d \0 ? ? ? ?

Using the Null-Terminating Character // Pre: char array must have null character at the end of data. long string_length (const char ntca[]) { long length = 0; while (ntca[length] != ‘\0’) length++; return length; } length = 4 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] A h m e d \0 ? ? ? ?

Using the Null-Terminating Character // Pre: char array must have null character at the end of data. long string_length (const char ntca[]) { long length = 0; while (ntca[length] != ‘\0’) length++; return length; } length = 4 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] A h m e d \0 ? ? ? ?

Using the Null-Terminating Character // Pre: char array must have null character at the end of data. long string_length (const char ntca[]) { long length = 0; while (ntca[length] != ‘\0’) length++; return length; } length = 5 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] A h m e d \0 ? ? ? ?

Using the Null-Terminating Character // Pre: char array must have null character at the end of data. long string_length (const char ntca[]) { long length = 0; while (ntca[length] != ‘\0’) length++; return length; } length = 5 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] A h m e d \0 ? ? ? ?

Using the Null-Terminating Character // Pre: char array must have null character at the end of data. long string_length (const char ntca[]) { long length = 0; while (ntca[length] != ‘\0’) length++; return length; } length = 5 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] A h m e d \0 ? ? ? ?

Using the Null-Terminating Character // Pre: char array must have null character at the end of data. void print_reverse (const char ntca[]) { long length = string_length(ntca); for (long i = length-1; i >= 0; i--) cout<<ntca[i]; return; } length = i= output buffer= [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] A h m e d \0 ? ? ? ?

Using the Null-Terminating Character // Pre: char array must have null character at the end of data. void print_reverse (const char ntca[]) { long length = string_length(ntca); for (long i = length-1; i >= 0; i--) cout<<ntca[i]; return; } length = 5 i= output buffer= [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] A h m e d \0 ? ? ? ?

Using the Null-Terminating Character // Pre: char array must have null character at the end of data. void print_reverse (const char ntca[]) { long length = string_length(ntca); for (long i = length-1; i >= 0; i--) cout<<ntca[i]; return; } length = 5 i=4 output buffer= [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] A h m e d \0 ? ? ? ?

Using the Null-Terminating Character // Pre: char array must have null character at the end of data. void print_reverse (const char ntca[]) { long length = string_length(ntca); for (long i = length-1; i >= 0; i--) cout<<ntca[i]; return; } length = 5 i=4 output buffer=d [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] A h m e d \0 ? ? ? ?

Using the Null-Terminating Character // Pre: char array must have null character at the end of data. void print_reverse (const char ntca[]) { long length = string_length(ntca); for (long i = length-1; i >= 0; i--) cout<<ntca[i]; return; } length = 5 i=3 output buffer=d [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] A h m e d \0 ? ? ? ?

Using the Null-Terminating Character // Pre: char array must have null character at the end of data. void print_reverse (const char ntca[]) { long length = string_length(ntca); for (long i = length-1; i >= 0; i--) cout<<ntca[i]; return; } length = 5 i=3 output buffer=de [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] A h m e d \0 ? ? ? ?

Using the Null-Terminating Character // Pre: char array must have null character at the end of data. void print_reverse (const char ntca[]) { long length = string_length(ntca); for (long i = length-1; i >= 0; i--) cout<<ntca[i]; return; } length = 5 i=2 output buffer=de [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] A h m e d \0 ? ? ? ?

Using the Null-Terminating Character // Pre: char array must have null character at the end of data. void print_reverse (const char ntca[]) { long length = string_length(ntca); for (long i = length-1; i >= 0; i--) cout<<ntca[i]; return; } length = 5 i=2 output buffer=dem [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] A h m e d \0 ? ? ? ?

Using the Null-Terminating Character // Pre: char array must have null character at the end of data. void print_reverse (const char ntca[]) { long length = string_length(ntca); for (long i = length-1; i >= 0; i--) cout<<ntca[i]; return; } length = 5 i=1 output buffer=dem [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] A h m e d \0 ? ? ? ?

Using the Null-Terminating Character // Pre: char array must have null character at the end of data. void print_reverse (const char ntca[]) { long length = string_length(ntca); for (long i = length-1; i >= 0; i--) cout<<ntca[i]; return; } length = 5 i=1 output buffer=demh [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] A h m e d \0 ? ? ? ?

Using the Null-Terminating Character // Pre: char array must have null character at the end of data. void print_reverse (const char ntca[]) { long length = string_length(ntca); for (long i = length-1; i >= 0; i--) cout<<ntca[i]; return; } length = 5 i=0 output buffer=demh [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] A h m e d \0 ? ? ? ?

Using the Null-Terminating Character // Pre: char array must have null character at the end of data. void print_reverse (const char ntca[]) { long length = string_length(ntca); for (long i = length-1; i >= 0; i--) cout<<ntca[i]; return; } length = 5 i=0 output buffer=demhA [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] A h m e d \0 ? ? ? ?

Using the Null-Terminating Character // Pre: char array must have null character at the end of data. void print_reverse (const char ntca[]) { long length = string_length(ntca); for (long i = length-1; i >= 0; i--) cout<<ntca[i]; return; } length = 5 i=-1 output buffer=demhA [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] A h m e d \0 ? ? ? ?

Forbidden Acts char ntca[10]; ntca = “bob”; char ntca1[10] = “Bob”; char ntca2[10] = “Robert”; if (ntca1 == ntca2)

Forbidden Acts char ntca[10]; ntca = “bob”; char ntca1[10] = “Bob”; char ntca2[10] = “Robert”; if (ntca1 == ntca2)

Forbidden Acts char ntca[10]; ntca = “bob”; char ntca1[10] = “Bob”; char ntca2[10] = “Robert”; if (ntca1 == ntca2)

Forbidden Acts char ntca[10]; ntca = “bob”; char ntca1[10] = “Bob”; char ntca2[10] = “Robert”; if (ntca1 == ntca2)

Forbidden Acts char ntca[10]; ntca = “bob”; char ntca1[10] = “Bob”; char ntca2[10] = “Robert”; if (ntca1 == ntca2)

Forbidden Acts char ntca[10]; ntca = “bob”; char ntca1[10] = “Bob”; char ntca2[10] = “Robert”; if (ntca1 == ntca2)

Forbidden Acts char ntca[10]; ntca = “bob”; char ntca1[10] = “Bob”; char ntca2[10] = “Robert”; if (ntca1 == ntca2)

Forbidden Acts char ntca[10]; ntca = “bob”; char ntca1[10] = “Bob”; char ntca2[10] = “Robert”; if (ntca1 == ntca2)

End of Session