class24-25 strings
TRANSCRIPT
-
8/7/2019 class24-25 Strings
1/35
Dept of C.S.E., M.I.T., Manipal
Character Sequences
or strings
-
8/7/2019 class24-25 Strings
2/35
Dept of C.S.E., M.I.T., Manipal
Definition
A string is an array of characters. Any group of characters (except double quote sign)
defined between double quotation marks is a
constant string.
Character strings are often used to build meaningfuland readable programs.
The common operations performed on strings are
Reading and writing strings
Combining strings together Copying one string to another
Comparing strings to another
Extracting a portion of a string ..etc.
-
8/7/2019 class24-25 Strings
3/35
Dept of C.S.E., M.I.T., Manipal
Declaration and initialization.
char string_name[size];The size determines the number of characters in the
string_name.
For example, consider the following array:char jenny [20]; is an array that can store up to 20
elements of type char. It can be represented as:
-
8/7/2019 class24-25 Strings
4/35
Dept of C.S.E., M.I.T., Manipal
In the above array, we can store sequences of
characters up to 20 characters long.
But we can also store shorter sequences.
For example,An array jenny could store at some point in a program
either the sequence "Hello" or the sequence "Merry
Christmas", since both are shorter than 20 characters.
-
8/7/2019 class24-25 Strings
5/35
Dept of C.S.E., M.I.T., Manipal
The characters sequences "Hello"
and "Merry Christmas represented in an array jenny as
follows :
-
8/7/2019 class24-25 Strings
6/35
Dept of C.S.E., M.I.T., Manipal
Initialization of null-terminated
character sequences arrays of characters or strings are ordinaryarrays they follow the same rules of arrays.
For example, if we want to initialize an array of
characters with some predetermined sequence of
characters we can just do it like any other array:
char myword[ ] = { 'H', 'e', 'l', 'l', 'o', '\0' };
-
8/7/2019 class24-25 Strings
7/35
Dept of C.S.E., M.I.T., Manipal
arrays of char elements have an additional method to
initialize their values: using string literals.
"the result is: " is a constant string literal.
For example,
char result[14] =the result is
Double quoted strings (") are literal constants whose
type is in fact a null-terminated array of characters. So
string literals enclosed between double quotes always
have a null character ('\0') automatically appended at the
end.
-
8/7/2019 class24-25 Strings
8/35
Dept of C.S.E., M.I.T., Manipal
we can initialize the array of char elements called
myword with a null-terminated sequence of
characters by either one of these two methods:char myword [ ] = { 'H', 'e', 'l', 'l', 'o', '\0' };
char myword [ ] = "Hello";
In both cases the array of characters myword is
declared with a size of 6 elements of type char: the 5
characters that compose the word "Hello" plus a final
null character ('\0') which specifies the end of the
sequence and that, in the second case, when using
double quotes (") null character ('\0') is appendedautomatically.
mystext = "Hello"; mystext[ ] = "Hello"; not validSTATEMENTS.
-
8/7/2019 class24-25 Strings
9/35
Dept of C.S.E., M.I.T., Manipal
Using null-terminated sequences of
characters#include void main ()
{
char question[] = "Please, enter your first name: ";char greeting[] = "Hello, ";
char yourname [80];
cout > yourname;
cout
-
8/7/2019 class24-25 Strings
10/35
Dept of C.S.E., M.I.T., Manipal
//include necessary header files.
void main()
{ const int MAX = 80; //max characters in string
char str[MAX]; //string variable str
cout > str; //put string in str
cout
-
8/7/2019 class24-25 Strings
11/35
Dept of C.S.E., M.I.T., Manipal
Reading Embedded Blanks
To read text containing blanks we use function,
cin :: get( ) // in C++
i.e cin.get(array_name, size)
The first argument to cin.get() is the array address
where the string being input will be placed.
The second argument specifies the maximum size of
the array, thus automatically avoiding buffer overrun.
or
in C Language we usegets(string) to read everything that you enter from
the key board until the ENTER key is pressed.
-
8/7/2019 class24-25 Strings
12/35
Dept of C.S.E., M.I.T., Manipal
//include necessary header files.
void main(){ const int MAX = 80; //max characters in string
char str[MAX]; //string variable str
cout
-
8/7/2019 class24-25 Strings
13/35
Dept of C.S.E., M.I.T., Manipal
#include#include//#include //for gets and puts
//Count the number of characters in a stringvoid main(){const int Max = 100;char sent[Max];
int i=0,count=0;clrscr();cout
-
8/7/2019 class24-25 Strings
14/35
Dept of C.S.E., M.I.T., Manipal
#include#include
//#include//Count the number of words in a sentencevoid main(){const int MAX = 100;char sent[MAX];
int i=0,count=1;clrscr();cout
-
8/7/2019 class24-25 Strings
15/35
Dept of C.S.E., M.I.T., Manipal
Reading Multiple Lines
we use third argument in cin::get() function.
cin.get(array_name, size, stop_char)
The argument stop_char specifies the character that tells
the function to stop reading. The default value for this
argument is the newline (\n) character, but if you call the
function with some other character for this argument, the
default will be overridden by the specified character.
-
8/7/2019 class24-25 Strings
16/35
Dept of C.S.E., M.I.T., Manipal
//include necessary header files
void main()
{
const int MAX = 200; //max characters in stringchar str[MAX]; //string variable str
cout
-
8/7/2019 class24-25 Strings
17/35
Dept of C.S.E., M.I.T., Manipal
String length can be obtained by using thefollowing functionn=strlen(string)This function counts and returns the numberof characters in a string.where n is an integer variable which receives
the value of the length of thestring. The argument may be a stringconstant.
Copying a String the Hard WayThe best way to understand the true nature of
strings is to deal with them character bycharacter. The following program does this.
-
8/7/2019 class24-25 Strings
18/35
Dept of C.S.E., M.I.T., Manipal
// copies a string using a for loop
#include
#include
void main()
{
char str1[ ] = Oh, Captain, my Captain! our fearful tripis done;
const int MAX = 80; //size of str2 bufferchar str2[MAX]; //empty string
for(int j=0 ; j
-
8/7/2019 class24-25 Strings
19/35
Dept of C.S.E., M.I.T., Manipal
The copying is done one character at a time, in the
Statement str2[j] = str1[j];
The copied version of the string must be terminated with anull. However, the string length returned by strlen() does
not include the null. We could copy one additional
character, but its safer to insert the null explicitly. We do
this with the line str2[j] = \0;If you dont insert this character, youll find that the string
printed by the program includes all sorts of weird
characters following the string you want. The
-
8/7/2019 class24-25 Strings
20/35
Dept of C.S.E., M.I.T., Manipal
Copying a String the Easy Way
strcpy(destination, source)
The strcpy function works almost like a stringassignment operator and assigns the contents of sourceto destination.
source may be a character array variable or a stringconstant.
e.g., strcpy(city,DELHI); will assign the string DELHI to the string variable city.
Similarly, the statement strcpy(city1,city2);
will assign the contents of the string variable city2 to the
string variable city1.The size of the array city1 should be large enough toreceive the contents of city2.
-
8/7/2019 class24-25 Strings
21/35
Dept of C.S.E., M.I.T., Manipal
//include necessary header filesvoid main()
{
char str1[ ] = Tiger, tiger, burning bright\n;
const int MAX = 80; //size of str2 buffer
char str2[MAX]; //empty stringstrcpy(str2, str1); //copy str1 to str2
cout
-
8/7/2019 class24-25 Strings
22/35
Dept of C.S.E., M.I.T., Manipal
Arrays of Strings
Heres an example, that puts the names of the days of the
week in an array:
// array of strings
void 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
-
8/7/2019 class24-25 Strings
23/35
Dept of C.S.E., M.I.T., Manipal
strcmp( ) Function :
The strcmp function compares two strings identified bythe arguments and has a value 0 if they are equal.
If they are not, it has the numeric difference betweenthe first non matching characters in the strings.It takes the form strcmp(string1,string2);string1 and string2 may be string variables or stringconstants.
Our major concern is to determine whether the stringsare equal; if not, which is alphabetically above. Thevalue of the mismatch is rarely important.e.g., strcmp(their,there); will return a value of 9which is the numeric difference between ASCII i andASCII r. That is, i minus r in ASCII code is 9.If the value is negative, string1 is alphabetically abovestring2.
-
8/7/2019 class24-25 Strings
24/35
Dept of C.S.E., M.I.T., Manipal
strcat( ) Function :
The strcat function joins two strings together.
It takes the following form:
strcat(string1,string2);string1 and string2 are character arrays.
When the function strcat is executed, string2 isappended to a string1. It does so by removing thenull character at the end of string1 and placingstring2 from there. The string at string2 remains
unchanged.
-
8/7/2019 class24-25 Strings
25/35
Dept of C.S.E., M.I.T., Manipal
#include#include
#include#includevoid main(){
char s1[40], s2[50];cout
-
8/7/2019 class24-25 Strings
26/35
Dept of C.S.E., M.I.T., Manipal
Two Dimensional Character Arrays
These Are arrays of arrays. 2-D character array consistsof strings as its individual elements.
Declaration :-
char a[10][10];
Initialization:-
char a[10][20]={aaa ,
bbb,
ccc
};
-
8/7/2019 class24-25 Strings
27/35
Dept of C.S.E., M.I.T., Manipal
void main(){
char string[30][30];int no,i;coutno;cout
-
8/7/2019 class24-25 Strings
28/35
Dept of C.S.E., M.I.T., Manipal
void main(){char string[30][30],temp[30];int no,i,j;coutno;cout
-
8/7/2019 class24-25 Strings
29/35
Dept of C.S.E., M.I.T., Manipal
for(i=0;i
-
8/7/2019 class24-25 Strings
30/35
Dept of C.S.E., M.I.T., Manipal
void main()
{char str[30];int i,n,j,flag=1;cout
-
8/7/2019 class24-25 Strings
31/35
Dept of C.S.E., M.I.T., Manipal
void main(){
char str[70];char temp;int i,n=0;cout
-
8/7/2019 class24-25 Strings
32/35
Dept of C.S.E., M.I.T., Manipal
void main(){
char c;
clrscr();
cout
-
8/7/2019 class24-25 Strings
33/35
Dept of C.S.E., M.I.T., Manipal
void main(){char string[30];int i,n=0;cout
-
8/7/2019 class24-25 Strings
34/35
Dept of C.S.E., M.I.T., Manipal
#include
#include
#include
//Checking for given substring in a string
char name[10], sub[10],flag=0,pos;
int i, j,k,l;
coutname;
l=strlen(name);coutsub;
int m=strlen(sub);
j=0,i=0;
while(sub[j]!='\0')
{ flag=0;
-
8/7/2019 class24-25 Strings
35/35
Dept of C.S.E., M.I.T., Manipal
if(sub[j]!=name[i])
{i++; continue;}
else pos=i;
if ((i+m)