Week 11 - Structured Programming
Week 11 - Structured Programming
Week 11
Strings
Unlike some other languages, there is no ‘string’ data type in C++.
We can only represent strings of characters in two rather indirect ways:
1. As arrays of type char
2. By pointers of type char
If astring was to contain the string ‘characters’ then the terminating ‘\0’ would be lost.
astring c h a r a c t e r s
Any attempt to display or copy this string would cause problems since the string has an undefined
length, and the program would simply display or copy the contents of all memory locations after the
string until a ‘\0’ was encountered.
To initialize a character array with a string literal, we can use the assignment operator (=) and {}.
E.g.: char astring[]={“hello”};
We cannot assign a string literal to a character array after it has been declared - this can only be
done one character at a time.
Example:
#include <iostream.h>
void main()
{
char astring[4]={"abc"};
char bstring[4];
//bstring={"xyz"} will not compile
bstring[0]='x';
bstring[1]='y';
bstring[2]='z';
bstring[3]='\0';
cout<<astring<<endl;
cout<<bstring<<endl;
}
Output:
abc
xyz
Likewise, if we want to make one array of characters equal to another, then unfortunately the
assignment operator cannot be used.
E.g.: bstring = astring is not allowed
So, to copy the contents of astring into bstring, we can use strcpy(destination, source) and
strncpy(destination, source, num_chars) of the header file string.h
E.g.: strcpy(bstring,astring);
strncpy(bstring,astring,9);
Example:
#include <iostream.h>
#include <string.h>
void main()
{
char astring[20];
char bstring[10];
cout<<"Enter a string (upto 19 characters): ";
cin>>astring;
SLIATE 25 J.Pratheepan
Structured Programming (C++) HNDIT
cout<<astring<<endl;
strncpy(bstring,astring,9);
bstring[9]='\0'; //This is important
cout<<bstring<<endl;
}
Output:
Enter a string (upto 19 characters): Superconductivity
Superconductivity
Supercond
Output:
char array
Output:
spring
SLIATE 26 J.Pratheepan
Structured Programming (C++) HNDIT
summer
winter
winter
winter
fall
The following figure shows what happens when we execute the above program.
s
p
Step1
r
a i
n
b g
\0
Step2
s
u
m
m
e
r
\0
a
Step3 u
c t
u
Step5 m
n
d \0
Step4 w
i
n
Step6 t
e
r
\0
f
a
l
l
\0
Using the assignment operator with char pointers should only be used for short term process.
For safe storage of strings arrays are more reliable.
It is possible to use strcpy or strncpy with character pointers to copy data rather than address.
However, this is not a recommended approach since we may overwrite data in subsequent memory
locations.
SLIATE 27 J.Pratheepan
Structured Programming (C++) HNDIT
It will happen if we declare a char pointer to point to a string of one length and then copy a longer
string to it.
Since char pointer only points to the address of one character, what may be in the subsequent
memory locations is unpredictable.
Example:
#include <iostream.h>
#include <string.h>
void main()
{
char* a="fall";
char* b="winter";
char* c="summer";
cout<<a<<endl;
cout<<b<<endl;
cout<<c<<endl<<endl;
strcpy(b,a);
cout<<a<<endl;
cout<<b<<endl;
cout<<c<<endl<<endl;
strcpy(a,c);
cout<<a<<endl;
cout<<b<<endl;
cout<<c<<endl<<endl;
}
Output:
fall
winter
summer
fall
fall
summer
summer
r
summer
SLIATE 28 J.Pratheepan