Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 23

CSC 2105 Data Structures

Lecture 2: String
String
• Strings are sequence of characters
representing a piece of text.

• In programming languages a string is


represented as some characters
enclosed by double quotes:

"This is a string”
C++ provides following two types of
string representations:

– The C-style character string.

–The string class type introduced


with Standard C++.

3
The C-style character string
• This string is actually a one-dimensional array of
characters which is terminated by a NULL character '\0'.

• To hold the NULL character at the end of the array, the


size of the character array containing the string is one more
than the number of characters in the word or string.

• The main difference between a simple array of character and


an array of character representing string is the end marker
NULL given at the end of a string.

• The standard library functions can recognize this end marker


as being the end of the string.
String - Declaration & Initialization
• The following declaration and initialization create a
string consisting of the word "Hello".
char greeting [ 6 ] = {'H', 'e', 'l', 'l', 'o', '\0'};
• To hold the NULL character at the end of the
array, the size of the character array containing
the string is one more than the number of
characters in the word "Hello.”
• It can also initialize as follows:
char greeting [ ] = "Hello";
Memory presentation of earlier defined
string in C/C++:

• The C++ compiler automatically places the '\0' at


the end of the string when it initializes the array.
Print earlier-mentioned string:
#include <iostream>
using namespace std;
int main ()
{
char greeting[6]={'H’,'e’,'l’,'l’,'o’,'\0'};
cout << "Greeting message: ";
cout << greeting << endl;
return 0;
}

When the above code is compiled and executed, it


produces result something as follows:

Greeting message: Hello


How to take a string input from keyboard:
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
char greeting[20];
cout << “Enter your string input : ” << endl;
cin >> greeting;
cout << "Greeting message: ";
cout << greeting << endl;
return 0;
}
When the above code is compiled and executed, it
produces result something as follows:
Enter your string input :
Hello
Greeting message: Hello
How to take a string input from keyboard:
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
char greeting[20];
cout << “Enter your string input : ” << endl;
cin >> greeting;
cout << "Greeting message: ";
cout << greeting << endl;
return 0;
}
When the above code is compiled and executed, it
produces result something as follows:
Enter your string input :
Hello World
Greeting message: Hello
How to take a string input from keyboard:
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
char greeting[20];
cout << “Enter your string input : ” << endl;
cin.getline(greeting,20);
cout << "Greeting message: ";
cout << greeting << endl;
return 0;
}
When the above code is compiled and executed, it
produces result something as follows:
Enter your string input :
Hello World
Greeting message: Hello World
Array of character string:
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
char greeting[5][20];
int n=5;
cout << “Enter your string input : ” << endl;
for(i=0; i<n ; i++)
cin.getline(greeting[i],20);

for(i=0; i<n ; i++)


{
cout << ”Name “ << i+1 <<“: ";
cout << greeting[i] << endl;
}
return 0;
}
Enter your string input :
Anika Aziz
Inaaya Rahman
Kazi Lubana
Kazi Areeba
Kazi A Kalpoma
Name 1: Anika Aziz
Name 2: Inaaya Rahman
Name 3: Kazi Lubana
Name 4: Kazi Areeba
Name 5: Kazi A Kalpoma
C++ supports string functions:
S.N. Function & Purpose

1 strcpy(s1, s2); Copies string s2 into string s1.


•strcat(s1, s2); Concatenates string s2 onto the
end of string s1.
3 strlen(s1); Returns the length of string s1.
•strcmp(s1, s2); Returns 0 if s1 and s2 are the same;
less than 0 if s1<s2; greater than 0 if s1>s2.
2strchr(s1, ch); Returns a pointer to the first occurrence
of character ch in string s1.
•strstr(s1, s2); Returns a pointer to the first occurrence
of string s2 in string s1.
#include <iostream>
#include <cstring>
using namespace std;
It produces result something as follows:
int main () strcpy( str3, str1) : Hello
{
char str1[10] = "Hello"; strcat( str1, str2): HelloWorld
char str2[10] = "World";
char str3[10];
strlen(str1) : 10
int len ;
// copy str1 into str3
strcpy( str3, str1);
cout << "strcpy( str3, str1) : " << str3 << endl;
// concatenates str1 and str2
strcat( str1, str2);
cout << "strcat( str1, str2): " << str1 << endl;
// total lenghth of str1 after concatenation
len = strlen(str1);
cout << "strlen(str1) : " << len << endl;
return 0; }
The String Class in C++:

• The standard C++ library provides a string


class type that supports all the operations
mentioned earlier and
• additionally much more functionality.
#include <iostream>
#include <string> It produces as follows:
using namespace std;
int main () Str3 : Hello
{
string str1 = "Hello";
Str1 + str2 : HelloWorld
string str2 = "World"; Str3.size( ) : 10
string str3;
int len ;
// copy str1 into str3
str3 = str1;
cout << "str3 : " << str3 << endl;
// concatenates str1 and str2
str3 = str1 + str2;
cout << "str1 + str2 : " << str3 << endl;
// total lenghth of str3 after concatenation
len = str3.size();
cout << "str3.size() : " << len << endl;
return 0; }
How to take a string input from keyboard:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str;
cout << “Enter your string input : ” << endl;
getline(cin , str);
cout << "Greeting message: ";
cout << str << endl;
return 0;
}
When the above code is compiled and executed, it
produces result something as follows:
Enter your string input :
Hello World
Greeting message: Hello World
Array of string:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str[5];
int n=5;
cout << “Enter your string input : ” << endl;
for(i=0; i<n ; i++)
getline(cin , str[i]);

for(i=0; i<n ; i++)


{
cout << ”Name “ << i+1 <<“: ";
cout << str[i] << endl;
}
return 0;
}
Enter your string input :
Anika Azia
Inaaya Rahman
Kazi Lubana
Kazi Areeba
Kazi A Kalpoma
Name 1: Anika Azia
Name 2: Inaaya Rahman
Name 3: Kazi Lubana
Name 4: Kazi Areeba
Name 5: Kazi A Kalpoma
STRING - ACCESS, INPUT,
OUTPUT…
 Instead of asking for the first name only, if we would have asked for the whole
name, this program might not work properly. That is, if we would have given
input John Rambo instead of only John the output will be as follows –
Please, enter your first name: John Rambo
Hello, John!
 Still it shows John after Hello, not John Rambo. As we know, that scanf
always stops at white spaces during taking input. So, after taking John as
input scanf receives a space indicating the end of input. So it never even goes
for Rambo. To overcome this, there is another function gets() which takes
only a string as input until a newline ('\n') encounters.
 So just writing gets(FirstName); instead of printf("%s",
Question); will give the following output –
Please, enter your first name: John Rambo
Hello, John Rambo!
 Just like gets() for reading from the user, there is also puts() which prints
the string along with a newline ('\n') character at the end of the string.
STRING HANDLING FUNCTIONS
 Strings are often needed to be manipulated by
programmer according to the need of a problem. All string
manipulation can be done manually by the programmer
but, this makes programming complex and large.
 The C/C++ supports a large number of string handling
functions. There are numerous functions defined in
"string.h"/"cstring" header file.
 For all such library functions a NULL character is assumed
to be at the end of the existing string and a NULL
character is always included at the end of the
new/updated/changed string.
HOW TO DETERMINE LENGTH OF
THE STRING
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
int len_str(char str[25]);
int len_str_while(char s[25]);
void main(void)
{
char l[25];
cin >> l;
cout << len_str(l) << endl << len_str_while(l);
}

//function with for loop


int len_str(char s[25])
{
for (int i = 0; s[i] != '\0'; i++);
return i;
}
CONT…
//another method using while loop
int len_str_while(char s[25])
{
int i=0;
while (s[i] != '\0')
{
i++;
}
return i;
}

You might also like