
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  CSCE 150A -- Problem Solving Using Computers
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\documentclass[pdftex]{beamer}

\input{beamerPreamble}
\usepackage[T1]{fontenc}
%\usepackage{tikz}\text
%\usetikzlibrary{shapes.callout}

\title[CSCE150A]{Computer Science \& Engineering 150A\\
    Problem Solving Using Computers -- Laboratory}
\subtitle{Lecture 14 - String}

\author[~]{Shuai Xie\vskip 1\baselineskip (Adapted from Derrick Stolee, Lin Liu \& Shuai Xie )} %

\date{Spring 2010}
%\usepackage{listings}
\usepackage{multicol}
\begin{document}
%\lstset{language=C}
\begin{frame}
  \titlepage
  \begin{center}
     \vfill
     \email{sxie@cse.unl.edu}
  \end{center}
\end{frame}

\section{Assignment}

\begin{frame} %-----------------------------------------------------------|
  \frametitle{Announcement: CodeLab assignment}
  \framesubtitle{~}

\begin{itemize}
	\item The 7th CodeLab assignment\\
	\vskip 1\baselineskip
	 \qquad "CodeLab Assignment-7"\\
	 \qquad Due:  23:59 Apr 25\\
	      \vskip 1\baselineskip
\end{itemize}

\end{frame} %-------------------------------------------------------------|



\section{As Function Argument}

\begin{frame} %-----------------------------------------------------------|
  \frametitle{Array as Function Argument}
  \framesubtitle{~}

\begin{itemize}
\item Entire arrays as function arguments.
\vskip .5\baselineskip
\texttt{ void func(int array$[\;]$);} \quad $-$$-$ \texttt{Not so good}\\
\vskip .5\baselineskip
\texttt{Put array size as another arguments.}\\
\vskip .5\baselineskip
\texttt{ void func(int array$[\;]$, int size);} \quad $-$$-$ \texttt{Good}\\
\vskip .5\baselineskip
\item Unlike passing values, C sends addresses of arrays.\\
\begin{itemize}
\item Thus, there is no need for a \& in the function call.\\
\item It can change the value of array element. \\

  \item To avoid changing array values: \quad \textbf{const int x[3];}\\

\end{itemize}
\vskip .5\baselineskip
\item In C, it is illegal for a function's return type to be an array;\\

\end{itemize}
\vspace{2in}
\end{frame} %-------------------------------------------------------------|


\begin{frame} %-----------------------------------------------------------|
  \frametitle{Array as Function Argument: Example}
  \framesubtitle{~}

 \small
\texttt{\#include <stdio.h>}\\
\texttt{void bar(int temp[], int size);}\\
\vskip .5\baselineskip
\texttt{int main(void) }\\
 $\{$ \\
 \quad  \texttt{int foo[] = $\{$1,2,3,4,5,6,7,8,9,10$\}$, i;}\\
  \quad  \texttt{bar(foo,10);}\\
  \quad  \texttt{for (i = 0; i < 10; i++) }\\
   \qquad \texttt{ printf("\%d$\setminus$n", foo[i]); }\\
  \quad  \texttt{return 0;}\\
 $\}$ \\
\texttt{void bar(int temp[], int size)  $\{$ }\\
  \quad  \texttt{int i;}\\
  \quad  \texttt{for (i = 0; i < size; i++)}\\
   \qquad  \texttt{temp[i] *= temp[i]; }\\
 $\}$ \\
\normalsize

\vspace{2in}
\end{frame} %-----------------------------------------------------------|

\section{Multidimensional Array}

\begin{frame} %-----------------------------------------------------------|
  \frametitle{Multidimensional Array declaration and initialization}
  \framesubtitle{~}


\begin{itemize}
	\item \texttt{Example: int x[10][10], y[3][2][4];}
	\vskip .5\baselineskip
  \item \texttt{Initialization}\\
	\texttt {int x[][3]=$\{$ $\{$1,2,3$\}$,$\{$4,5,6$\}$,$\{$7,8,9$\}$ $\}$ };
	\vskip .5\baselineskip
	\item \texttt{Accessing:  x[0][1], y[2][1][0];}
	\vskip .5\baselineskip
	\item Nested for loop to acess the array.
	\vskip .5\baselineskip
	\item Iterating:
		

\quad for ( i = 0; i < sizeX; i++ ) \\
\quad \quad for ( j = 0; i < sizeY; j++ ) $\{$\\

 \qquad    name[i][j] = 0;\\$\}$\\

\end{itemize}
\vspace{2in}

\end{frame} %-----------------------------------------------------------|



\begin{frame}

\begin{itemize}
	\item \texttt{Unfortunately C language doesn't support directly use two-dimensional array as function argument}\\
\vskip .4\baselineskip
	\texttt{int func(int arr[][],int x, int y);} \\
	 \textbf{Wrong!}\\
	 \vskip 1\baselineskip
	\item \texttt{You must give out all the dimension size except the first dimension}\\
\vskip .4\baselineskip
	\texttt{int func(int arr[][10],int x);}\\
	\textbf{OK!}\\
	\texttt{Here,x is the size of the first dimension}\\
	
\end{itemize}
\vspace{2in}

\end{frame} %-----------------------------------------------------------|

\begin{frame} %-----------------------------------------------------------|
  \frametitle{Redirect input}
  \framesubtitle{~}


\begin{itemize}
  \item Try unix command \textbf{ ls -a $>$ 1.txt}
	\item \texttt{To test our codes easily or other reason, we can use redirect input.}\\
	\texttt{Store all the input data into a file. } \\
	 \texttt{Use unix redirection command "$<$"}\\
	 \item \texttt{Example:} \textbf{ ./GetAverage $<$ Score.txt}\\
	 \vskip 1\baselineskip
	\item \texttt{For redirect input, your program will use scanf as normal, but the program will get input data from the file instead of keyboard, save time for programmer to input data}\\
	
\end{itemize}
\vspace{2in}

\end{frame} %-----------------------------------------------------------|






\section{String}

\frame{
\frametitle{Introduction}
\begin{itemize}
	\item A list of characters is a \emph{string}.
	\item We use arrays of type {\tt char}:
\end{itemize}
	\begin{center}
		{\tt char s[] = "My string!";}
		
		\begin{tabular}[h]{|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|}
			\hline
			`M'& `y' & ` ' & `s' & `t' & `r' & `i' & `n' & `g' & `!' & `$\setminus$0'\\
			\hline
			77 & 121 & 32 & 115 & 116 & 114 & 105 & 110 & 103 & 33 & 0\\
			\hline
		\end{tabular}
	\end{center}

\begin{itemize}
	\item The final `$\setminus$0' character signifies the end of the string.
	\item \emph{Note: there may be more entries in the array, but they will be ignored!}
\end{itemize}
\vspace{2in}
} %frame

\frame[containsverbatim]{
\frametitle{Dealing with Character Values}
\begin{itemize}
\item Each {\tt char} is essentially a number, but gets translated into a letter with the ASCII table.
\item {\tt http://www.asciitable.com }
\item String examples\\
\qquad \texttt{"UNL", "C Program", "15.21"}
\end{itemize}
} %frame


\frame[containsverbatim]{
\frametitle{Dealing with Character Values}
\begin{itemize}
\item Upper-case letters {\tt 'A'} through {\tt 'Z'} appear as contiguous numbers, in order.
\item Lower-case letters {\tt 'a'} through {\tt 'z'} appear as contiguous numbers, in order.	
\item You can treat a {\tt char} type (including string index) and {\tt char} literal (such as {\tt 'a'}) as numbers!
	\begin{itemize}
		\item You can use comparisons: {\tt == \quad != \quad <= \quad >= }
		\item You can use operations: {\tt + \quad - \quad / \quad * }
	\end{itemize}
\vspace{2in}
\end{itemize}
} %frame


%Chapter 8 Section 1
\frame[containsverbatim]{
\frametitle{Dealing with Literal Strings}
\begin{itemize}
\item We have seen string literals before, using double quotes (")
\item Examples:
	\begin{itemize}
		\item {\tt printf("Hello world!");}
%		\item {\tt scanf("\%lf", val);}
	\end{itemize}
\item These strings cannot be changed after compile time, since they
	are never stored into an array.
	
\vspace{2in}
\end{itemize}
} %frame

\frame[containsverbatim]{
\frametitle{Storing Strings}
\begin{itemize}
\item You can use static arrays to store strings.

\end{itemize}
\begin{verbatim}
char string1[] = "Length determined by string";
char string2[100] = "Length determined by 100";
\end{verbatim}

\begin{itemize}
\item Then, you can output strings with {\tt printf}:

	{\tt printf("My string is: \%s$\setminus$n",string1);}
	
	{\tt printf("My string is: \%s$\setminus$n",string2);}
\end{itemize}

\vspace{2in}
} %frame

\section{Manipulating Strings}
\frame[containsverbatim]{
\frametitle{Pulling Characters}
\begin{itemize}
\item We can access individual characters by treating the string as an array.
\begin{verbatim}
char string[] = "What is the word?";
printf("Starts with %c\n", string[0]);
\end{verbatim}
\end{itemize}
\vspace{2in}
} %frame

\frame[containsverbatim]{
\frametitle{Using String Library}
\begin{itemize}
\item There are a lot of built-in functions in {\tt string.h}\\
{\tt \#include <string.h>}
\item Key pattern: first parameter will store the result.

    {\tt strlen(char*)}
	
		{\tt strcpy(char*, char*)}
		
		{\tt strcat(char*, char*)}
		
		{\tt strncpy(char*, char*, int)}
		
		{\tt strncat(char*, char*, int)}

		{\tt strncmp(char*, char*)}
		
		

\item More available in textbook(Page 455).
\end{itemize}
\vspace{2in}
} %frame\frame[containsverbatim]{



\end{document}
