chapter04_String
chapter04_String
3
Using Strings in C++ Programs .. Cont.
Example:
Write a C++ program that reads two initials and the last name of
a person and displays a personalized message to the program
user.
Analysis stage:
- Input:
2 characters for the initials (e.g. first and second)
1 string for the last name (e.g. last)
-Output:
a message to welcome the user
4
Using Strings in C++ Programs .. Cont.
//A program to display a user’s name with a welcome message
#include <iostream>
#include <string>
using namespace std;
void main ( )
{ char first, second; //input and output: first and second initials
string last; //input and output: last name
// Enter letters and print message.
cout<<"Enter 2 initials for your first and second names and last
name: " ;
cin >> first >> second >> last;
cout<< "Hello "<<first<< ". " << second<<". " <<last<< endl;
}
5
.Using build in library
#include <iostream>
#include <string>
using namespace std;
void main ()
{string str1 = "Hello";
string str2 = "World";
string str3;
int len ;
str3 = str1; // copy str1 into str3
cout << "str3 : " << str3 << endl;
// concatenates str1 and str2
str3 = str1 + str2;
cout << "str3 : " << str3 << endl;
len = str3.size();
cout << "str3.size() : " << len <<endl;
}
.Using built-in libraray – Cont
#include<iostream> wholeName.insert(0,"Mr. ");
#include<string> cout<<greeting<<wholeName<<'!'<<endl;
using namespace std; wholeName.insert(9,"Ali ");
void main() cout<<greeting<<wholeName<<'!'<<endl;
{ string first, last, title; wholeName.replace(9,7,"Maher ");
string wholeName;
cout<<greeting<<wholeName<<'!'<<endl;
string greeting= "Hello ";
wholeName.erase(9,3);
cout<<"Enter first name: ";
cout<<greeting<<wholeName<<'!'<<endl;
cin>>first;
title.assign(wholeName,0,20);
cout<<"Enter last name: ";
cout<<greeting<<wholeName<<'!'<<endl;
cin>>last;
wholeName=first+" "+last; cout<<greeting<<title<<'!'<<endl;
cout<<greeting<<wholeName<<'!'<<endl; }
cout<<"You have "<<(wholeName.length()-1)
<<" letters in your name. "<<endl;
cout<<"Your initials are "<<first.at(0)
<<last.at(0)<<endl;
cout<<"position of last
"<<wholeName.find(last)<<endl;
Output :
++Fundamentals of Strings in C
- String can be array of characters ends with null
character ‘\0’.
char color [ ] = “green” ;
-- this creates 6 element char array, color, (last element
is ‘\0’)
0\ n e e r g
• Reading Strings
- Assign input to character array, for example
char word [ 20 ];
cin >> word;
cout<<word<<endl;
-- this reads characters until a space, tab, newline,
or end-of-file is encountered.
-- the string should be less than 19 characters, the
20th is for the null character (‘\0’).
Problem: read characters until the first white space
12
Fundamentals of Strings in C++ .. Cont.
• solution: To read an entire line of text into an array,
C++ uses: getline function as follows:
cin.getline ( array, array size, delimiter character);
- getline will copy input into specified array until
either
-- one less than the size is reached
-- the delimiter character is input
- Example:
char word [20] ;
cin.getline ( word, 20, ‘\n’ );
13
String Manipulation Functions
Description Function
Copies string s2 into the char *strcpy(char *s1, const char *s2);
character array s1. The
value of s1 is returned.
Copies at most n char *strncpy(char *s1, const char *s2, size_t n);
characters of string s2 into
the array s1. The value of
s1 is returned.
Appends string s2 to char *strcat (char *s1, const char *s2);
string s1. The value of s1
is returned.
Appends at most n char *strncat (char *s1, const char *s2, size_t n);
characters of string s2 to
string s1. The value of s1
is returned.
14
String Manipulation Functions .. Cont.
Compares string s1 with int strcmp(const char *s1, const char *s2);
string s2. The function
returns a value of zero,
less than zero or greater
than zero if s1 is equal
to, less than or greater
than s2, respectively.
Compares up to n int strncmp(const char *s1, const char *s2, size_t n);
characters of string s1
with string s2. The
function returns zero,
less than zero or greater
than zero if s1 is equal
to, less than or greater
than s2, respectively.
15
String Manipulation Functions .. Cont.
Determines the length of string s. The Size_t strlen( const char *s);
number of characters preceding the
terminating null character is returned.
16
String Manipulation Functions .. Cont.
#include <iostream>
#include <cstring>
using namespace std;
void main() {
char str1[] ="Omar";
char *str2 = "Ahmad";
strcpy(str1,str2);
cout<<str1<<endl;
}
2- strncpy(s1, s2) s1[n] = s2[n]
#include <iostream>
#include <cstring>
using namespace std;
void main() {
char str1[] = ”**********";
char *str2 = “$$$$$$$$$$";
strncpy(str1,str2,5);
cout<<str1<<endl;
}
3- strcat(s1, s2) s1 = s1+s2
Concatenates string s2 onto the end of string s1.
#include <iostream>
#include <cstring>
using namespace std;
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>
#include <cstring>
using namespace std;
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>
#include <cstring>
using namespace std;
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;
else
cout<<str1<<" = "<<str2<<endl; }
6- strncmp(s1, s2,n) 0 if s1[n] = s2[n]
-1 if s1[n] < s2[n]
#include <iostream>
1 if s1[n] > s2[n]
#include <cstring>
using namespace std;
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;
else
cout<<str1<<" = "<<str2<<endl; }
7- strlen(s) How many characters in s
#include <iostream>
#include <cstring>
using namespace std;
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";
}
strcpy - code
#include<iostream>
using namespace std;
const int stringsize = 40;
void strcpy(char destation[], char source[])
{
int stringindex=0;
while(source[stringindex] !='\0')
{ destation[stringindex]=source[stringindex];
stringindex++;}
destation[stringindex]='\0';
}
void main()
{ char s1[stringsize]; // or you can define it char *s1 = new char[];
strcpy(s1,"we are going to implement strings");
cout<<"S1 = "<< s1<<endl;
strlen- code
#include<iostream>
using namespace std;
const int stringsize =256;
int strlen(char string[])
{ int len=0;
while(string[len]!='\0')
len++;
return len;}
void main()
{ char buffer[stringsize];
char buffer2[] = "this is another expression";
cout<<"enter an expression"<<endl;
cin>>buffer;
cout<<"the length of the string you just typed is "
<<strlen(buffer)<<endl;
cout<<"the length of the string with space is "
<<strlen(buffer2)<<endl;}
strcmp - code
#include<iostream> void main()
using namespace std; { char b1[]="Ahmad";
const int stringsize = 256; char b2[]="Ali";
int strcmp(char s1[], char s2[]) if(strcmp(b1,b2) == -1)
{ cout<<"first string is smaller"<<endl;
int stringindex=0; else if (strcmp(b1,b2) == 1)
while(s1[stringindex] !='\0') cout<<"first string is larger"<<endl;
{ if (s1[stringindex]>s2[stringindex]) else
return 1; cout <<"both strings are the
else if (s1[stringindex]<s2[stringindex]) same"<<endl;}
return -1;
else
stringindex++;}
if (strlen(s1) < strlen(s2))
return -1;
return 0;}
strcat - code
#include<iostream>
using namespace std; void main()
void strcat(char destination[], char source[])
{
{ int len=0;
int stringindex=0;
char s1[265];
// calculates the end of the destination string strcpy(s1,"philadelphia University ");
while(destination[len]!='\0') strcat(s1,"- Faculty of Information Technology");
len++; cout<<" the string is: "<<endl<<s1<<endl;
while(source[stringindex] != '\0') }
{
destination[len+stringindex]=source[stringindex];
stringindex++;
}
destination[len+stringindex]='\0';
}