03.12.2012 Views

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

2.12. EXERCISES 63<br />

The first line that does not start with % contains the number of rows, the number of columns<br />

and the number of non-zero elements on the sparse matrix.<br />

Use fstream to read the header of a MatrixMarket file and print the number of rows and columns,<br />

and the number of nonzeroes on the screen.<br />

2.12.5 String manipulation programs<br />

There is a type string in the standard library. This type contains a large number of string<br />

operations, such as string concatenation, string comparison, etc. Note the include of the header<br />

file string.<br />

#include <br />

#include <br />

int main()<br />

{<br />

std::string s1 = ”Hello”;<br />

std::string s2 = ”World”;<br />

std::string s3 = s1 + ”, ” + s2 ;<br />

std::cout ≪ s3 ≪ std::endl ;<br />

return 0;<br />

}<br />

In this example we have concatenated the strings s1 and s2 together with a string constants.<br />

Per<strong>for</strong>m the following exercises:<br />

1. Write a function itoa (int i, std::string& b) that constructs a string representation of i in b<br />

and returns b.<br />

2. Write a simple encryption program. It should read the input from cin and write the<br />

encrypted symbols in cout. Use the following simple encryption scheme: the code <strong>for</strong> a<br />

symbol c is c key[i] , where key is a string given as a parameter to a function. The symbols<br />

from key are used in a cyclic way. (After the repeated encryption with a same key key you<br />

should get the source string.)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!