%\marginsize{2cm}{2cm}{0.25cm}{2cm}  % left right top bttom?
%\pagestyle{empty}
\def\semester{Fall~2005}
\def\course{CS~101}
\def\prof{Sharat}
\newcommand{\titleline}[1]{

%  \includegraphics[scale=0.5]{e1.png}\\
% to replaced by a pattern here. - appu
% {\Huge \textbf{\textsf{B1}} \\}

  {\sc #1}\hfill{\sc \prof} \vspace{0.1cm} \hrule }

\newcommand{\exhead}[1]{
  \begin{center} {\bf \course: #1} \\ \end{center} }
  
\documentclass[10pt]{article}
\usepackage{anysize,color,graphicx}
\usepackage{algorithm}
\usepackage{algorithmic}
\usepackage{longtable}
\usepackage{anysize}

\marginsize{2.5cm}{2cm}{0.25cm}{2cm}
% \usepackage{HW}


\begin{document}
\titleline{\semester}
\exhead{Lab Handout No: 6 (Week of September 05, 2005)}
                                                                                                  
\section*{Today's Problem}
\begin{itemize}
\item This assignment DOES carry marks.
\item In this lab, you will do string programming using string class. Using
these concepts, you will solve the problems posted in your assignment
folder when you log into PEACH. Also there might be some optional
problems for you to take a crack at.
\end{itemize}

\section*{C++ String Class.}
\begin{enumerate}
\item We can represent text in C++ using objects of a 'string' class. In order to use this class you should include the following statements:

\begin{verbatim}
  #include <string>
  using namespace std;
\end{verbatim}

\item A string can now be created as follows.
\begin{verbatim}
  string s = "abc def abc"; 
  string s2 = "abcde uvwxyz"; 
\end{verbatim}

\item The following table includes methods for reading string input from the
stdin and commonly used methods with strings. The variable 'c', used below if of type \verb+char+ and the 
variable i is of type \verb+int+.

\begin{longtable}{llp{2.75in}}
\hline
\hline
Input & 
\begin{tabular}{l}
\verb+ cin >> s;+  
\end{tabular}&
{The value of s is read from the stdin. The
value read stops at whitespace. If "charlie brown" is entered in
response to \verb+cin>>s+, only "charlie" gets stored in s. }\\
\hline

Output & 
\begin{tabular}{l}
\verb+ cout <<s;+ 
\end{tabular} &  
Writes the string to the specified output stream. \\
\hline

Line input & 
\begin{tabular}{l}
\verb+ getline(cin, s);+
\end{tabular}& 
Reads everything up to the next newline character and puts the result into the specified string variable. \\
\hline

Assignment &
\begin{tabular}{l} 
\verb+ s = s2;+ \\
\verb+ s = "abc"; +\\
\end{tabular} &
 A string literal or a string variable can be assigned to a string variable.  \\
\hline

Subscript & 
\begin{tabular}{l}
\verb+ s[1] = 'c';+  \\
\verb+ c = s[1]; + 
\end{tabular}& 
Changes s to equal "acc def abc" Sets c to 'b'. The subscript operator returns a char value, not a string value.  \\

\hline

Length & 
\begin{tabular}{l}
\verb+ i = s.length();+
\end{tabular} 
& 
i is set to the current length of the string s \\
\hline

Empty  &
\begin{tabular}{l}
\verb+ if(s.empty()) +\\ 
\verb+   i+++; 
\end{tabular}& 
The example adds 1 to i if string s is empty \\
\hline

Relational operators &
\begin{tabular}{l} 
\verb+ if (s < s2) +\\ 
\verb+  i+++; 
\end{tabular}
 &  
Uses ASCII code to determine which string is smaller. Here the condition is
true because a space comes before letter d \\
\hline

Concatenation & 
\begin{tabular}{l}
\verb+ s2 = s2 ++  \verb+"x";+ \\
\verb+ s2 ++\verb+= "x";+ 
\end{tabular}& 
Both examples add x to the end of s2 \\
\hline


Substring &
\begin{tabular}{l}
\verb+ s = s2.substr(1,4); + \\
\verb+ s = s2.substr(1,50);+
 \end{tabular}& 
The first example starts in position 1 of s2 and takes 4 characters, setting s to "bcde". In the second example, s is set to "bcde uvwxyz". If the length specified is longer than the remaining number of characters, the rest of the
string is used. The first position in a string is position 0. The \verb+substr+ method doesn't change the string object on which it is called. i.e. the s2 here is not 
changed after the \verb+substr+ operation.\\
\hline


Substring replace &
\begin{tabular}{l} 
\verb+ s.replace(4,3,"x"); +
\end{tabular}& 
Replaces the three characters of s beginning in position 4 with the character x. Variable s is set to "abc x abc". The \verb+replace+ method does change the string object on which it is called.\\
\hline

Substring removal &
\begin{tabular}{l} 
\verb+ s.erase(4,5);+ \\
\verb+ s.erase(4); +
\end{tabular}
& 
Removes the five characters starting in position 4 of s. The new value of s is
"abc bc".  Remove from position 4 to end of string. The new value of
s is "abc ". The \verb+erase+ method does change the string object on which it is called.\\
\hline

Pattern matching &
\begin{tabular}{l} 
\verb+ i = s.find("ab",4); +
\end{tabular}
& 
The first example returns the position of the substring "ab" starting the search in position 4. Sets i to 8. Exercise: What is returned if the substring doesn't exist?\\
\hline
\hline
\end{longtable}

\item Sample Example:

\begin{verbatim}
#include <string>
#include <iostream>
using namespace std;

int main()
{
    string s1="calvin";
    cout<<"Length:"<<s1.length()<<endl;
    cout<<s1.empty()<<endl;
    string s2="hobbes";
    string s3=s1+s2;
    cout<<s3.substr(1,4)<<endl;
    cout<<s3<<endl;
    cout<<s3.erase(3,2)<<endl;
    cout<<s3.replace(4,3,"B")<<endl;
    int i=s1.find("lv",2);
    cout<<"i="<<i<<endl;
}
\end{verbatim}

\item Output:
\begin{verbatim}
Length:6
0
alvi
calvinhobbes
calnhobbes
calnBbes
i=2
\end{verbatim}
\end{enumerate}

\end{document}
