12
Strings Sujana Jyothi C++ Workshop Day 4

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

Embed Size (px)

Citation preview

Page 1: 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

Strings

Sujana JyothiC++ Workshop

Day 4

Page 2: 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

A String also called character string is a sequence of contiguous characters in memory terminated by the NUL character ‘\0’.

The C++ header file <string> is included to deal with string manipulations

Initialisation:

char Exforsys[30]= {‘T’,’r’,’a’,’i’,’n’,’i’,’n’,’g’,’\0’};

char Exforsys[30]= “Training”;

char Exforsys[ ]=”Company”;

Example:

#include <iostream>#include <string>void main( ){char examp1[] = “Exforsys”;char examp2[80];for(int i=0; i<strlen(exampl1);i++)examp2[i]=examp1[i];examp2[i]=’\0’;cout<< “The copied string is: “<< examp2;}

Page 3: 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

Dynamic Array of Characters

Example:

#include <iostream>

using namespace std; int main() { char *Country = new char[20]; Country = "Equatorial Guinea"; cout << "Country Name: " << Country; cout << "\n\n"; return 0; }

Passing an array of characters

#include <iostream>

using namespace std; void ShowCountry(const char S[]) { cout << "Country Name: " << S <<"\n\n"; }

int main() { char Country[] = “Republic of Ireland"; ShowCountry(Country); return 0; }

Page 4: 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

Two- dimensional arrays

#include <iostream>

using namespace std; void ShowCountries(char S[][15]) { cout << "Countries Names"; for(int i = 0; i < 3; ++i) cout << "\nCountry " << i + 1 << ": " << S[i]; } int main() { char Country[][15] = { "Afrique du Sud", "Australia", "Zimbabwe", "Sri Lanka", "Yemen", "El Salvador" }; ShowCountries(Country); cout << "\n\n"; return 0; }

String manipulations

Strlen(), Strcat(), Strcpy(), Strcmp()

Try out programs involving these string manipulation functions

Page 5: 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

Cin.getline(Arrayname, Dimension, Delimiter):

#include <iostream> using namespace std; int main () { char name[256], title[256]; cout << "Enter your name: "; cin.getline (name,256); cout << "Enter your favourite movie: "; cin.getline (title,256); cout << name << "'s favourite movie is " << title; return 0; }

cin Member Functions

Read input character-by-character

Char ch; ch=cin.get();

Char ch; cin.get(ch);

Char name[40];

Cin.get(name,40);

Page 6: 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

String class

Declaring a string:

using namespace std; string my_string;

Specifying an initial value for the string in a constructor:

using namespace std; string my_string("starting value");

#include <iostream> #include <string> using namespace std; main() { string str1, str2; const char *s2; char *s3; str1 = "Hello World"; cout << str1.length() << " " << str1 << "\n"; str2 = str1; cout << str2 << "\n"; str1[0] = 'J'; cout << str1 << "\n" << str2 << "\n"; s3 = "Daffy Duck"; str2 = s3; str2[0] = 'T'; cout << s3 << " " << str2 << "\n"; str2 += " "; str2 += str1; cout << str2 << "\n"; }

Page 7: 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

Structures

Sujana JyothiC++ workshop

Day 4

Page 8: 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

From Structures to Objects

Classes in C++ are a natural evolution of the C notion of struct

A typical structure example:

struct Time {

int hour; // 0-23

int minute; // 0-59

int second; // 0-59

};

By exposing weaknesses of this struct example (which are typical of all structures) we will motivate the evolution towards classes

Page 9: 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

Using Structures … a word of warning

The arrow operator requires some care:

timePtr->hour is equivalent to (*timePtr).hour

Parentheses are needed because the dot operator (.) has a higher precedence than the pointer dereferencing operator (*)

Writing *timePtr.hour is a syntax error because the precedence rules define it as *(timePtr.hour) … since with a pointer you must use the arrow operator to refer to a member!

NOTE: pointers are difficult for beginners and experts! (esp. C++)

NOTE: (the concept of) pointers (is)are fundamental to (most) programming

Page 10: 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

Declaring structure variables

Structure_name Structure_variable

Eg: Customer cust1;Example:

#include <iostream>using namespace std;

struct MyStructure{   int n;   float f;   float f2;};

int main(){  MyStructure myMyStructure; MyStructure *ptrMyStructure;

 //set the f of the structure myMyStructure.f = 1000;

 //intialize the pointer ptrMyStructure = &myMyStructure;

 //change the pointers f ptrMyStructure->f = 2000;

 //print out the structures f cout << myMyStructure.f << endl;

 return 0;}

Pointer to a structure

Use -> for structure pointer

#include <iostream>using namespace std;

struct account{   int accountnum;   float balance;   float interestrate;};

int main(){  account myaccount; account *ptraccount;

 ptraccount->balance = 1000;       return 0;}

Page 11: 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

Using Structures … with a function

With the Time structure a typical function would be to print out the time on the screen:

void print (Time t) \\ Just hours and minutes

{ cout << (t.hour < 10 ? “0”: “”) << t.hour <<“:”

<< t.minute <10 ? “0”: “”) << t.minute;

}

Notes:

•structures are normally call-by-value

•could pass a constant reference (or pointer)

•the test? True-do-this: False-do-this construct

Page 12: 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

Recursion

// Recursion - factorial & fibonacci

#include<iostream>

int factorial (int x){if (x<1) return 1; else return x*(factorial (x-1));}

int fibonacci (int x){if (x==1) return 1;if (x==2) return 1; else return (fibonacci(x-2) + fibonacci(x-1));}

int main(){ cout << "factorial 5 =" <<factorial(5)<<endl; cout << "fibonacci 5 =" <<fibonacci(5);}