Strings
The example program below, with a few notes following, shows how strings work in C++.
Example 1:
#include <iostream> using namespace std; int main()
{
string s="eggplant";
string t="okra";
cout<<s[2]<<endl;
cout<< s.length()<<endl; //prints 8
cout<<s.substr(1,4)<<endl; //prints ggpl...kind of
like a slice, but the second num is the length of the
piece
cout<<s+t<<endl; //concatenates: prints eggplantokra
cout<<s+"a"<<endl;
cout<<s.append("a")<<endl; //prints eggplanta: see
Note 1 below
//cout<<s.append(t[1])<<endl; //an error; see Note 1
cout<<s.append(t.substr(1,1))<<endl; //prints
eggplantak; see Note 1
cout<<s.find("gg")<<endl;
if (s.find("gg")!=-1)
cout<<"found gg"<<endl;
else cout<<"gg not found"<<endl; if
(s.find("xx")!=-1)
cout<<"found xx"<<endl;
else cout<<"xx not found"<<endl;
cout<<s.find('p')<<endl; //prints 3: find will
work with a single character
return 0;
//prints "g"... see Note 0 below
}
Note 0: When you use an index with a string in C++ you do not get a string, but a different thing: a single character. C++ does NOT treat a single character as just a string with one thing in it, but as a different animal.
Note 1: In C++, append() is more like concatenate, in that the thing you append on has to be string, and can't be a single character.
For example, suppose we wanted to add the second character of the string t onto the end of string s. We can't write
s.append(t[1]);
because t[1] isn't a string, it's a single character (see Note 0). Instead we have to write
s.append(t.substr(1,1));
This works because substr() gives us a string of length 1, not a single character.
When find() doesn't find anything, it returns a funny large number, as the printout shows, but fortunately that large number tests as equal to -1. Don't expect that to make sense!
C-strings
C++ actually has two ways of managing collections of characters. We've been discussion "strings" above, and there are also c-strings, which work differently.
Actually, anytime you write something inside double quotes in your programs you're creating a c-string, without knowing it. Strings are actually objects (something you'll learn about soon). C-strings are simpler, just arrays of characters.
C++ has a data type char, for character. A char information holder takes up one byte, and can store one character, such as a letter, a digit, or a punctuation mark. A c-string is just an array of characters, like this, say,
char s[100];
but it is managed in a special, clever way, that it's important to
know about.
The thing about character strings is that you don't want to worry ahead of time about exactly how long they are. If someone is going to type in a file name, you don't want to tell them it has to be exactly so many characters long. Rather, you want to be able to deal with as many characters as they choose to type, up to some limit.
The C++ designers (well, actually it was the C designers, or someone even earlier) came up with a way to do this: they put a special marker character into every c-string to mark its end.
The end marker is the character code 0, which is not a legal code for any character. So there is no risk of cutting off any legitimate c-string when using this as the marker.
The idea is simple. If a c-string is empty, the marker character is sitting right in the first slot. It doesn't matter what's in the rest of the slots, because anybody processing the c-string stops when they see the marker. If the c-string contains the word eggplant, the first eight slots in the array contain the letters e, g, g, p, l, a, n, t, and then the next slot contains the marker character.
When you print out a c-string, as in cout<<s;
the code for << knows to stop when it gets to the marker character, so only the good stuff in s gets printed.
Example 2:
#include <iostream> using namespace std;
int main() {
char cstring[]="eggp ant";
cout << cstring<< endl;
//note that a char array can have a blank in it
int i=0;
while(cstring[i]!=0) //have to use a while, not a for, when don't
know how
//long the array is
{
cout<<cstring[i]<<endl; i=i+1;
}
for(int i=0;i<12;i++) //deliberately going too far in this for to see everything in cstring plus a little extra
cout<<"<"<<cstring[i]<<">"<<int(cstring[i])<<endl; //print the chars and the int codes for them
return 0; }
Running this produces this output (with comments added)
eggp ant //notice that the whole c-string is printed, including
the space e
g
g
p
a
n
t //note that the loop stops when it comes to the end marker,
because of our while()... we don't print any garbage characters,
including the marker
<e>101
<g>103
<g>103
<p>112
< >32
<a>97
<n>110
<t>116
<0>0 //! this is the marker character
<0>0
<0>0 //these other zeros are just left over in memory
<0>0
Pass by Reference
Pass by Value |
Pass by Reference |
Passes an argument by value. |
Passes an argument by reference. |
You are sending a copy of the data to the function. |
You are passing the memory address of the data. |
Changes does not affect the actual value. |
Changes to the value affect the original data. |
Example 3:
#include <iostream> using namespace std;
void printnumbers(int &a, int b) {
a=1500; b=40;
}
int main () {
int a = 5; int b = 10;
cout << "The value of a is: " << a << endl; cout << "The value of b is: " << b << endl;
printnumbers(a, b);
cout << "The value of a is: " << a << endl; cout << "The value of b is: " << b << endl;
return 0; }
Recitation 5 Activity:
This question is related to string operations to find if a given string is Palindrome and Anagram.
Here is the program from which you can identify palindrome string.
#include<iostream>
using namespace std;
int main( )
{
char str[80];
cout<<"Enter string: ";
cin.getline(str, 80);
int l; //length of string
//check length of string
for(l = 0; str[l] != '\0'; l++);
//Comparing first element with last element till middle of
string
int i;
for(i = 0; (i < l/2) && (str[i] == str[l - i - 1]);
i++);
if(i == l/2)
cout << "Palindrome";
else
cout << "Not a palindrome";
return 0;
}
For Anagram string :
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char str1[20],str2[20];
int len,len1,len2,i,j,found=0,not_found=0;
cout<<"Enter first string :";
gets(str1);
cout<<"Enter second string :";
gets(str2);
len1=strlen(str1);
len2=strlen(str2);
if(len1==len2)
{
len=len1;
for(i=0;i<len;i++)
{
found=0;
for(j=0;j<len;j++)
{
if(str1[i]==str2[j])
{
found=1;
break;
}
}
if(found==0)
{
not_found=1;
break;
}
}
if(not_found==1)
{
cout<<"Strings are not Anagram to each other";
}
else
{
cout<<"Strings are Anagram";
}
}
else
{
cout<<"Two string must have
same number of character to be Anagram";
}
getch();
}
Get Answers For Free
Most questions answered within 1 hours.