String in C++.

Slides:



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

 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.
String in C++. String Series of characters enclosed in double quotes.“Philadelphia University” String can be array of characters ends with null character.
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.
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.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
CS 192 Lecture 11 Winter 2003 December 29-30, 2003 Dr. Shafay Shamail.
String in C++. Using build in library. #include using namespace std; void main () { string str1 = "Hello"; string str2 = "World"; string str3; int len.
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.
Introduction to C programming
One Dimensional Arrays (Part2) Sorting Algorithms Searching Algorithms Character Strings The string Class. 1.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
String functions+ string I.Mona Alshehri. String Functions: Header file:#include Function: Int strlen(char s[n]) Description Calculates the length of.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
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.
 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.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Characters and Strings Functions.
Strings, Slide Fundamental Programming Strings.
CMSC 104, Version 8/061L25Strings.ppt Strings Topics String Libraries String Operations Sample Program Reading Sections
Built-in Functions for NTCAs. strlen char array[10] = “Hello”; int length = strlen(array); cout
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.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
1 Chapter 8 – Character Arrays and Strings Outline 8.1Introduction 8.2Declaring and Initializing String 8.3Input/output of strings 8.4String-handling Functions.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Lecture 2A Data Types Richard Gesick
Strings: C-strings vs. Strings as Objects
Characters, Strings, and the cstring Library
INC 161 , CPE 100 Computer Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Fundamentals of Characters and Strings
Characters, Strings, and the cstring Library
Programming Languages and Paradigms
Programming Paradigms
Strings A string is a sequence of characters treated as a group
«آرايه‌ها و رشته ها».
CS111 Computer Programming
Object Oriented Programming COP3330 / CGS5409
C Stuff CS 2308.
Remark: Data Type of Array Name
Engr 0012 (04-1) LecNotes
INC 161 , CPE 100 Computer Programming
Strings: C-strings vs. Strings as Objects
Pointers and Pointer-Based Strings
Strings A collection of characters taken as a set:
Chapter 9 One-Dimensional Arrays
Lecture 11 Strings.
String in C++.
C-strings In general, a string is a series of characters treated as a unit. Practically all string implementations treat a string as a variable-length.
Strings.
CprE 185: Intro to Problem Solving (using C)
EECE.2160 ECE Application Programming
Strings What is a string? It is an array of characters terminated with
CPS120: Introduction to Computer Science
C Strings Prabhat Kumar Padhy
Engineering Problem Solving with C++, Etter
C++ Programming Lecture 20 Strings
Lecture 19: Working with strings
Strings #include <stdio.h>
Lecture 2 Arrays & Pointers September 7, 2004
Characters and Strings Functions
Characters and Strings
C Characters and Strings
Introduction to Problem Solving and Programming
Presentation transcript:

String in C++

String Series of characters enclosed in double quotes. “Philadelphia University” String can be array of characters ends with null character ‘\0’. char sname[ ] = “Ali Samer”; // 10 locs or char sname[ ] = {‘A’,’l’,’i’,’ ’,‘S’,’a’,’m’,’e’,’r’,’\0’}

String String can be constant pointer that points to the string’s first character. char *sname = “Ali Samer”; Sname points to ‘A’ “Ali Samer’\0’” in Some where in memory

Example void main( ) { char firstName[] = "Ahmad"; char *lastName = "Omar"; cout<<"First Name: "<<firstName<<endl; cout<<"Last Name: "<<lastName<<endl; int i=0; cout<<"FirstName: "; while (firstName[i] != '\0') cout<<firstName[i++]; i=0; cout<<"\nLast Name: "; while (lastName[i] != '\0') cout<<lastName[i++]; }

Reading String char studentName[20]; cin>>studentName; cout<<studentName<<endl; Problem: read characters until the first white space.

Reading String char studentName[20]; cin.getline(studentName,20,'\n'); solution: use cin.getline(array_name, array_size, delimiter) char studentName[20]; cin.getline(studentName,20,'\n'); cout<<studentName<<endl; Remark: Pointer to character string can not be readed

String Library cstring.h or string.h This library include set of built in functions for manipulating the strings. Like: 1- strcpy(s1, s2)  s1 = s2 #include <iostream.h> #include <cstring> void main() { char str1[] ="Omar"; char *str2 = "Ahmad"; strcpy(str1,str2); cout<<str1<<endl; }

2- strncpy(s1, s2)  s1[n] = s2[n] #include <iostream.h> #include <cstring> void main() { char str1[] = ”**********"; char *str2 = “$$$$$$$$$$"; strncpy(str1,str2,5); cout<<str1<<endl; }

3- strcat(s1, s2)  s1 = s1+s2 #include <iostream.h> #include <cstring> void main() { char str1[24] = ”Philadelphia"; char *str2 = “University"; strcat(str1,str2); cout<<str1<<endl; }

4- strncat(s1, s2,n)  s1 = s1+s2[n] #include <iostream.h> #include <cstring> void main() { char str1[24] = ”Philadelphia"; char *str2 = “University of Jordan"; strncat(str1,str2,10); cout<<str1<<endl; }

5- strcmp(s1, s2)  0 if s1 = s2  -1 if s1 < s2  1 if s1 > s2 Symbols < … < numbers < … < capital letters < …. < smal letters. #include <iostream.h> #include <cstring> void main() { char str1[20] ; char str2[20] ; cin.getline(str1,20); cin.getline(str2,20); if (strcmp(str1,str2)) if (strcmp(str1,str2) == 1) cout<<str1<<" > "<<str2<<endl; else cout<<str1<<" < "<<str2<<endl; cout<<str1<<" = "<<str2<<endl; }

6- strncmp(s1, s2,n)  0 if s1[n] = s2[n] #include <iostream.h> #include <cstring> void main() { char str1[20] ; char str2[20] ; cin.getline(str1,20); cin.getline(str2,20); if (strncmp(str1,str2,1)) if (strcmp(str1,str2,1) == 1) cout<<str1<<" > "<<str2<<endl; else cout<<str1<<" < "<<str2<<endl; cout<<str1<<" = "<<str2<<endl; }

7- strlen(s)  How many characters in s #include <iostream.h> #include <cstring> void main() { char s1[] = "Ahamd Ali"; char *s2 = "Amman City"; cout<<s1<<" Consists of "<<strlen(s1)<<" Characters.\n"; cout<<s2<<" Consists of "<<strlen(s2)<<" Characters.\n"; }