Sol W2 |home |print view |recent changes |changed October 31, 2017 | exact |You are 3.234.208.66 <- set your identity!

Here come your solutions!

When posting code you can leave out scaffolding like the generated code from unit tests or the #includes required. If there is already a solution that is different from yours, just add yours or discuss the difference. Caution: parallel edits might cause data loss (however, versioning happens and you can retrieve an overwritten edit).

### 1. Count non-whitespace Chars^

• Header `charc.h`
```#include <iosfwd>
int charcount(std::istream &in);
```
• Code
```int charcount(std::istream &in){
std::string line{};
std::getline(input, line);
line.erase(remove(line.begin(), line.end(),' '),line.end()); //Erase-Remove Idiom richtig angewendet, leider hier nicht die einfachste Lösung.
return line.size();
// Hat jemand eine bessere Lösung? ...
//T. Corbat: Diese Lösung zählt nur die Zeichen einer einzelnen Zeile und überspringt Talbulatoren nicht
}
```
• Test cases
```// go here
```
• main Program
```#include <charc.h>
#include <iostream>
int main(){
std::cout << charc(std::cin) << '\n';
}
```

### 2. Count all Chars^

• Code
```int allcharcount(std::istream &in){
}

//primitive approach, doesn't work without checking for eof() == false, why?
void allcharc(std::istream & in, std::ostream & out) {

char c { };
int counter { 0 };

while (in.get() >> c && in.eof() == false) {
counter++;
}

out << counter;
}

```
• Tests
```// go here
```

### 3. Count Words^

• Code
```int wordcount(std::istream &in){
}

//primitive approach
void wc(std::istream & in, std::ostream & out) {

std::string word{};
int counter{0};

while (in >> word) {
counter++;
}

out << counter;
}

```
• Tests
```// go here
```

### 4. Count lines^

• Code
```int linecount(std::istream &in){
}

//primitive approach
void lc(std::istream & in, std::ostream & out) {

std::string line{};
int counter{0};

while (!in.eof()) {
std::getline(in, line);
counter++;
}

out << counter;
}

```
• Tests
```// go here
```

### 5. Very Simple Calculator ^

• Code
```int calc(int left, int right, char op){
}
```
• Tests
```// go here
```

### 6. Simple Calculator from stream^

• Code
```int calc(std::istream &in){
}
```
• Tests
```// go here
```

## Drill questions^

Provide the header where defined:

• std::cin
• std::endl
• std::tolower
• std::string
• std::distance
• std::istream_iterator
• std::size_t
• std::vector

 namespace headerfile variable function type description std::cin iostream x Represent standard input stream std::endl ostream x Inserts a newline character into the output sequence os and flushes it as if by calling. std::tolower cctype x Converts given character to lowercase (A -> a). std::string string x Stores and manipulates sequences of char-like objects. std::distance iterator x Returns the number of hops from first to last. std::istream_iterator iterator x Read successive objects of type T from the basic_istream. std::size_t cstddef x Unsigned integer type of the result of the sizeof operator. std::vector vector x Sequence container that encapsulates dynamic size arrays.

//T. Corbat: Korrekte Antworten

 |home |print view |recent changes |changed October 31, 2017 | exact |You are 3.234.208.66 <- set your identity! Sol W2