Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

Arrays and Strings

Chapter (7)

1 Part II
Contents
C-Strings
C-String Variables
String stored in string variable
Avoiding Buffer Overflow
String Constants
Reading Embedded Blanks
Reading Multiple Lines
Copying a String the Hard Way
Copying a String the Easy Way
Arrays of Strings
Library Functions
2
C-Strings
 two kinds of strings are commonly used in C++:
1.C-strings
2.strings that are objects of the string class.
 C-strings are arrays of type char.
 C-strings are also called C-style strings.
 C-strings are still important for a variety of reasons.
1.First, they are used in many C library functions.
2.Second, they will continue to appear in legacy code for years to
come.
3.And third, for students of C++, C-strings are more primitive and
therefore easier to understand on a fundamental level.

3
C-String Variables
As with other data types, strings can be variables or constants.
The definition of the string variable str looks like (and is) the
definition of an array of type char: char str[MAX];
Each character occupies 1 byte of memory.
An important aspect of C-strings is that they must terminate with a
byte containing 0.
This is often represented by the character constant ‘\0’, which is a
character with an ASCII value of 0.
This terminating zero is called the null character.

4
String stored in string variable
// stringin.cpp
// simple string variable
#include<iostream>
using namespace std;
int main()
{//max characters in string
const int MAX = 80;
char str[MAX]; //string variable str
cout << "Enter a string: ";
cin >> str; //put string in str
//display string from str
cout << "You entered: " << str << endl;
return 0;
}

5
Avoiding Buffer Overflow
// avoids buffer overflow with cin.width What happens if the user enters a
#include<iostream> string that is longer than the array
#include<iomanip> used to hold it?
using namespace std;
int main()  This program uses the setw
{ manipulator to specify the
const int MAX = 20; maximum number of characters
char str[MAX]; //string variable str the input buffer can accept.
 The user may type more
cout << "\nEnter a string: ";
characters, but the >> operator
cin >> setw(MAX) >> str; //put string in str,
won’t insert them into the array.
// no more than MAX chars  One character fewer than the
cout << "You entered: " << str << endl; number specified is inserted, so
return 0; there is room in the buffer for the
} terminating null character.

6
String Constants

#include<iostream>
using namespace std;
int main()
{ You can initialize a string
to a constant value when
char str[] = "We are the champions."; you define it.
cout << str << endl;
return 0;
}

7
Reading Embedded Blanks
// blanksin.cpp
// reads string with embedded blanks
#include<iostream>
using namespace std;
int main()
{ Using this function, the input
string is now stored in its entirety.
const int MAX = 80; //max characters in
string
Enter a string: No Pain, No Gain.
char str[MAX]; //string variable str
You entered: No Pain, No Gain.
cout << "\nEnter a string: ";
cin.get(str, MAX); //put string in str
cout << "You entered: " << str << endl;
return 0;
}

8
Reading Multiple Lines
// linesin.cpp
// reads multiple lines, terminates on ‘$’ character
#include<iostream>
using namespace std;
const int MAX = 2000; //max characters in string
char str[MAX]; //string variable str
int main( )
{
cout << "\nEnter a string:\n";
cin.get(str, MAX, '$'); //terminate with $
cout << "You entered:\n" << str << endl;
return 0;
}

9
Copying a String the Hard Way
// strcopy1.cpp
// copies a string using a for loop 0 1 2 3 4 5 6 7 8 9 10
#include<iostream> str1
H a p p y b o y ! ‘\0’
#include<cstring> //for strlen()
using namespace std; 0 1 2 3 4 5 6 7 8 9 10 11 12
int main() str 2 H a p p y b o y ! ‘\0’
{
char str1[] = "Happy boy!";
const int MAX = 80; //size of str2 buffer
char str2[MAX]; //empty string
int j;
for(j=0; j<strlen(str1); j++) //copy strlen characters
str2[j] = str1[j]; // from str1 to str2
str2[j] = '\0'; //insert NULL at end
cout << str2 << endl; //display str2
return 0;
}
10
Copying a String the Easy Way
// strcopy2.cpp
// copies a string using strcpy() function
#include<iostream>
#include<cstring> //for strcpy()
using namespace std;
int main()
{
char str1[] = "Happy New Year.";
const int MAX = 80; //size of str2 buffer
char str2[MAX]; //empty string
strcpy(str2, str1); //copy str1 to str2
cout << str2 << endl; //display str2
return 0;
}

11
Arrays of Strings
// straray.cpp
// array of strings
#include<iostream>
using namespace std;
int main()
{
const int DAYS = 7; //number of strings in array
const int MAX = 10; //maximum size of each string
//array of strings
char star[DAYS][MAX] = { "Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday" };
for(int j=0; j<DAYS; j++) //display every string
cout << star[j] << endl;
return 0;
}

12
Library Functions

Sr.No Function Purpose


1. strcat(s1, s2); Concatenates string s2 onto the end of string s1
2 strcmp(s1, s2); Returns 0 if s1 and s2 are the same; less than 0 if
s1<s2; greater than 0 if s1>s2.
3. strchr(s1, ch); Returns a pointer to the first occurrence of character
ch in string s1.
4. strstr(s1, s2); Returns a pointer to the first occurrence of string s2 in
string s1.
5. toupper(ch) The toupper() function converts ch to its uppercase
version if it exists.
6. tolower(ch) The tolower() function converts ch to its lowercase
version if it exists.

13
Exercises
1. Write a C++ program to find the length of a string without using
library functions.

2. Write a C++ program to enter the input string and then separate as
the individual characters from a string. The interaction with the user
might look like this:
Enter the input string: Hello UCSY
The characters of the string are : H e l l o U C S Y

3. Write a C++ program to count the total number of words in a


string.

14
THANK YOU.

15

You might also like