0% found this document useful (0 votes)
3 views33 pages

Topic (4) Programming II - (String)

The document provides an overview of C++ string handling, including initialization, input/output, and string manipulation functions. It covers the differences between string variables and character arrays, as well as common functions like strcpy, strlen, and comparison operators. Additionally, it includes examples and tasks to reinforce understanding of string operations in C++.

Uploaded by

jobestph
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views33 pages

Topic (4) Programming II - (String)

The document provides an overview of C++ string handling, including initialization, input/output, and string manipulation functions. It covers the differences between string variables and character arrays, as well as common functions like strcpy, strlen, and comparison operators. Additionally, it includes examples and tasks to reinforce understanding of string operations in C++.

Uploaded by

jobestph
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JADARA UNIVERSITY

Computer Science Department


T
o
p
i
c
4 Programming II Course
Programming II Course © JADARA © Dr. Ma`en Al-zu`bi
C++ String
What is a string 3

The string type is not a built-in type so when we


want to use all functions of the string type, we must
put the
#include<string> directive.
A string can be initialized as:
The string "Declaration and initialization" variables 4

string variable Arrays of characters

string s1= "AAAAA AA"; char s1[]= "AAAAA AA";


string s2= "BBBBB BB"; char s2[]= "BBBBB BB";
string s3= ""; char s3[]= "";
COUT<<S1;
To print the
variables of strings
COUT<<S2;
COUT<<S3; To Repeat character, using
string s3(60, ‘*’);
//s3 contains 60 asterisks
String I / O 5

cin and cout (<< and >> )

string s1, s2;


cout<<"Enter two strings"<<endl;
cin>> s1>>s2; // whitespace separates strings
cout<< s1 << " "<< s2;
YO U T Y P E J A DA R A J O

s1 s2
You get Jadara Jo
Using String Variables 6

string full_name, adress;


cin>> full_name >> adress;
cout<< full_name << endl;
cout<< adress; String
You enter: ahmed
jordan

full_name Ahmed
THE OUTPUT
adress jordan
Using the arrays of characters C-String 7

char full_name[30], adress[30];


cin>> full_name >> adress;
cout<< full_name <<endl; C-
cout<< adress;
String
You enter: ahmed
jordan

FULL_NAME ahmed
THE OUTPUT
ADRESS jordan
String storing 8

String Variables Arrays of characters Variables

String str1= " Hello " char str2[]= " Hello "
Cout<< str1[0]; Cout<< str2[0];
Cout<< str1[1]; Cout<< str2[1];
Cout<< str1[2]; Cout<< str2[2];
Cout<< str1[3]; Cout<< str2[3];
Cout<< str1[4]; Cout<< str2[4];
Problem, when enter multi string using CIN>> 9

string name1;
char name2[30];
cin>> name1 >> name2; // whitespace separates strings
jadara university
jordan, irbid
cout<< name1 << name2;
THE OUTPUT

name1 JADARA
WHY???
name2 University
Solution, String input with getline( ) 10

The function getline can be used to read an entire line of input


into a string variable.

The getline function has three parameters:


The first specifies the area into which the string is to be read.
The second specifies the maximum number of characters,
including the string delimiter.
The third specifies an optional terminating character. If not
included, getline stops at ‘\n’.
String input with cin vs getline( ) 11

Write a code to different between


getline( ), and [Link]
string s1, s2; char s1[20], s2[20];

getline(cin, s1); [Link](s1,sizeof(s1));


getline(cin, s2); [Link](s2,sizeof(s2));

cout<<s1<<endl; cout<<s1<<endl;
cout<<s2<<endl; cout<<s2<<endl;
Example 1 12

#include <iostream>
#include <string>
using namespace std;
int main(){

string Full_Name;
getline(cin,Full_Name);
//char Full_Name[80];
//[Link](Full_Name, 80);
cout << Full_Name;

return 0;}
Example 2 13

#include <iostream>
#include <string>
using namespace std;
int main(){
char A_string[20], E_string[20];
cout << "Enter some words in a string:\n";
cin >> A_string;
[Link] (E_string, 20) ;
cout << A_string << "#" << E_string << "\nEND OF OUTPUT\n";
return 0;}
Example 3 14

#include <iostream>
#include <string>
using namespace std;
int main(){

char lastName[30], firstName[30];


cout << "Enter a name <last,first>:\n";

[Link] (lastName, sizeof(lastName), ',');


[Link] (firstName, sizeof(firstName));

cout << "Here is the name you typed:\n\t|" << firstName << " " << lastName << "|\n";

return 0;}
Example 4 15

#include <iostream>
#include <string>
using namespace std;
int main(){
Concatenate
string str1 = "ice“, str2 = "cream“,
strings str3;
str3 = str1 + str2;
cout<< str3;
return 0;}

Access to string,
using index
TASK 1
Write a C++ program to display the following format and
results
Hi! What's your name? Ahmed Mohammad

**********************************
* *
* Welcome, Ahmed Mohammad! *
* *
**********************************
C-String
Variables and Functions
String Copy Function (Create function) 18

Using the String Variables

#include <iostream>
int main()
using namespace std;
{
void strcpy(string dest, string src)
string name1, name2 ;
{
strcpy(name1,"M M ali"); // assign
dest= src;
strcpy(name2,"202320232023"); // assign
cout<<dest<<endl;
strcpy(name2,name1); // no assign ??
cout<<src<<endl;
return 0;
}
}
String Copy Function (Create function) 19

Using the C-String (char of array) Variables

#include <iostream> int main()


using namespace std; {

void strcpy(char dest[], const char src[]) char name1[16] , name2[16] ;


{ strcpy(name1,“M M ali"); // assign
for(int i=0;i<16;i++) strcpy(name2,“202320232023"); // assign
{ dest[i] = src[i]; } strcpy(name2,name1); // assign
cout<<dest<<endl;
cout<<src<<endl; return 0;
} }
Predefined Function strcpy () with <cstring> 20

#include <iostream>
#include <cstring>
The strcpy() using namespace std; strcpy (DEST , SRC)
function in C++ int main()
copies a {
char string_1[6] = "Short";
character string char string_2[16] = "Have Nice Day";
from source to
strcpy(string_1, string_2); // incorrect
destination. It is cout<< "string_1 " << string_1 << endl;
defined in the
cstring header strcpy(string_2, string_1); // correct
cout<< "string_2 " << string_2 << endl;
file.
return 0;
}
Example strcpy () with <cstring> 21

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char string_1[] = "Short"; // 6
int size1 = sizeof(string_1);
char string_2[] = "Have a Nice Day!"; //17
int size2 = sizeof(string_2);

cout<< "string_1 " << string_1 << " Size " << size1 <<endl;
strcpy (DEST , SRC)
cout<< "string_2 " << string_2 << " Size " << size2 <<endl;

strcpy(string_2, string_1); // correct


cout<< "string_1 " << string_1 << endl;
cout<< "string_2 " << string_2 << endl;

cout<< "string_1 " << string_1 << " Size " << size1 <<endl;
cout<< "string_2 " << string_2 << " Size " << size2 <<endl;

return 0;
}
Predefined Function strlen() with <cstring> 22

#include <iostream>
The strlen() #include <cstring> strlen (SRC)
function in C++ using namespace std;
int main()
returns the length {
of the given // initialize C-string
C-string. char ch[] = "We Will Win!";
// print the length of the string
It is defined in the cout << strlen(ch);
cstring header return 0;
file. }
Predefined Function strncpy() with <cstring> 23

#include <iostream>
#include <cstring>
using namespace std; strncpy (DEST , SRC, length)
int main()
{
char string_1[5] = "ABCD", string_2[10]="123456789";
cout << "String 1 = " << string_1 << endl;
cout << "String 2 = " << string_2 << endl;
strncpy(string_1,string_2,strlen(string_1));
cout << "After copying, string 1 = " << string_1<<endl;
cout<< sizeof(string_1);
}
Example 1: String Copy and String Length Check 24

void string_copy(char target[], char source[], int target_size)


{
int index; int main()
int new_length = strlen(source); {
if (new_length > (target_size))
char short_string[11];
new_length = target_size ;
char long_string[] = "This is rather long.";
for (index = 0; index < new_length; index++) cout << long_string << " \n";
target[index] = source[index];
string_copy(short_string, "Hello", 10);
target[index] = '\0'; cout << short_string << "\n";
}
string_copy(short_string, long_string, 10);
cout << short_string << " \n";

}
Example2: String Comparison 25

you can compare two strings . All the operators designed to


compare data are at your disposal: > , < , >= , <= , !=.
C-String Comparison – strcmp() and strncmp() 26

Strcmp (src , dest)


char str1[10] , str2[10] ;

Using the compare function for c-string variables str1 str2 return reason
strcmp(str1,str2) == 0 when str1 == str2 value
strcmp(str1,str2) == 1 when str1 > str2 “AAAA “ABCD” <0 ‘A’ <‘B’
strcmp(str1,str2) == -1 when str1 < str2 ”
“B123” “A089” >0 ‘B’ > ‘A’
“127” “409” <0 ‘1’ < ‘4’
Strncmp (src , dest, size)
“abc88 “abc888” =0 equal string
char str1[10] , str2[10] ; 8”
“abc” “abcde” <0 str1 is a sub string of str2
Using the compare function for c-string variables
“3” “12345” >0 ‘3’ > ‘1’
strcmp(str1,str2, n) == 0 when str1 == str2
strcmp(str1,str2, n) == 1 when str1 > str2
strcmp(str1,str2, n) == -1 when str1 < str2
Example1: String Comparison 27

#include <iostream> #include <iostream>


#include <cstring> #include <cstring>
using namespace std; using namespace std;

int strncmp(char s1[], char s2[], int limit) int strncmp(char s1[], char s2[], int limit)
{ {
for (int i=0; i < limit; i++){ for (int i=0; i < limit; i++){
if (s1[i] < s2[i]) if (s1[i] < s2[i])
return -1; return -1;
if (s1[i] > s2[i]) if (s1[i] > s2[i])
return 1; return 1;
} }
return 0; return 0;
} }

int main() int main()


{ {
char s1[10] = "aaaaaa", s2[10] = "bbbbbb", limit = 5; char s1[10] = “bbbbbb", s2[10] = “aaaaaa", limit = 5;
cout << strncmp(s1, s2,limit); cout << strncmp(s1, s2,limit);
} }
Example2: String Comparison 28
Some Common Errors 29

It is illegal to assign a value to a string variable (except


at declaration).

char A_string[10];
//A_string = "Hello"; // illegal assignment
char B_string[10] = "Hello";
strcpy (B_string, "Hello");
cout<< B_string;
Some Common Errors 30

int main()
{
char string1[10] = "Hello";
char string2[10] = "Hello";

if (string1 == string2) //wrong


cout << "Yes!"; // illegal comparison
if (!strcmp(string1,string2))
cout << "Yes they are same!";
}
Creating Space in Output 31

#include<iomanip>
THE SETW FUNCTION SPECIFIES THE NUMBER OF
SPACES FOR THE NEXT ITEM
Applies only to the next item of output
EXAMPLE: TO PRINT THE DIGIT 7 IN FOUR SPACES USE

COUT<<SETW(4) << 7 << ENDL ;


Three of the spaces will be blank

7 7

<<left<<
TASK 2
(“salty ” < “sweet”) t/f
(“aardvark” == “aardvark”) t/f
(“john” > “john”) t/f

what does full hold?


string last= “woods”, first = “tiger ”, full;
a) full = first + last; _______________
b) full = first + “ “ + last; _______________
c) full = last + “, ” + first; _______________
what does k hold?
int k = [Link]( ); _______________
JADARA UNIVERSITY
Computer Science Department

Programming II Course © JADARA © Dr. Ma`en Al-zu`bi

You might also like