Skip to content

Commit

Permalink
Add reference to std::string (#686)
Browse files Browse the repository at this point in the history
* Add reference to std::string

Added reference to std::string in about.md, introduction.md and hints.md
add hints

* Resolve suggestions of #686

- Change the mention of 'string' to 'std::string'
- Add reference to 'std::string'
- improved the wordings

* [hints] change integer to number

Closes #643
  • Loading branch information
StarOne01 authored Jul 18, 2023
1 parent d883671 commit 83295d5
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 3 deletions.
64 changes: 63 additions & 1 deletion concepts/strings/about.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,65 @@
# About

TODO: add content from introduction.md, when finished.
A `string` in C++ is a mutable object that represents text as a sequence of characters (letters, digits, punctuation, etc.).
Strings are manipulated by calling the string's methods.

## Strings Library

In C++ the string type and the associated functions have to be included from the strings library before usage.
You can do so by adding `#include <string>` to the top of your file.
They will then populate the `std` namespace.
The string literal uses the double quote character: `"`.

```cpp
#include <string>
std::string w_berry_quote{"A well-made sentence, I think, is a thing of beauty."};
```
## Common String Operations
You can use the `+` operator to concatenate strings:
```cpp
std::string original_title{"The School of Rock"};
std::string sequel_indicator{"Electric Boogaloo"};
std::string next_movie_title = original_title + " 2: " + sequel_indicator;
```

To use the strings library, you need to know that it is possible to call a function that belongs to an object.
These are called member functions.
Later in the syllabus, you will learn more about member functions and the connected class concept.

```cpp
std::string qualification{"awesome"};
// 1st argument: from the index to the end of the string:
std::string who_is_awesome = qualification.substr(5);
// => "me"

// 2nd optional argument for the length:
std::string material{"haunted books"};
std::string ghost = material.substr(8, 3);
// => "boo"
```
The `find` function is also very useful.
It is called as a member function on the string and takes a string as the argument.
`find` returns the zero-indexed position of the first occurrence in the string.
```cpp
std::string new_release{"apple released a new app!"};
new_release.find("app");
// => 0
new_release.find("!");
// => 24
```

There is also the `to_string` function, which can be used to convert integers and float/double values to string.
`to_string` returns a string.

```cpp
int num{92};
double exponent{0.92e2};
std::string msg{ std::to_string(num) + " can also be written as " + std::to_string(exponent)};
// => 92 can also be written as 92.0000000
```
9 changes: 9 additions & 0 deletions concepts/strings/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,12 @@ new_release.find("app");
new_release.find("e");
// => 4
```

There is also the `std::to_string` function, which can be used to convert integers and float/double values to string.

```cpp
int num{92};
double exponent{0.92e2};
std::string msg{ std::to_string(num) + " can also be written as " + std::to_string(exponent)};
// => 92 can also be written as 92.0000000
```
8 changes: 6 additions & 2 deletions exercises/concept/making-the-grade/.docs/hints.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
- `while` loops are used for _indefinite_ (uncounted) iteration
- `for` loops are used for _definite_, (counted) iteration.
- The keywords `break` and `continue` help customize loop behavior.
- `std::to_string()` from `string` library can be used to convert a integer to string

Also being familiar with the following can help with completing the tasks:

- [`vectors`][vectors]: indexing, size, [`<vector>.emplace_back`][emplace], [`<vector>.pop_back()`][pop].
- [`string`][string]: using the `+` to concatenate strings.
- [`string`][string]: using the `+` to concatenate strings, integer to string conversion, [`to_string`][to\_string].

## 1. Rounding Scores

Expand All @@ -34,6 +35,8 @@ Also being familiar with the following can help with completing the tasks:
## 5. Matching Names to Scores

- If both containers are the same length and sorted the same way, could you use the `index` from one to retrieve a `value` from the other?
- `std::to_string(int)` can be used to convert a number to string.
- Don't forget the follow the format of the example's output.

## 6. A "Perfect" Score

Expand All @@ -42,5 +45,6 @@ Also being familiar with the following can help with completing the tasks:

[vectors]: https://en.cppreference.com/w/cpp/container/vector
[string]: https://en.cppreference.com/w/cpp/string/basic_string
[to\_string]: https://en.cppreference.com/w/cpp/string/basic_string/to_string
[emplace]: https://en.cppreference.com/w/cpp/container/vector/emplace_back
[pop]: https://en.cppreference.com/w/cpp/container/vector/pop_back
[pop]: https://en.cppreference.com/w/cpp/container/vector/pop_back
1 change: 1 addition & 0 deletions exercises/concept/making-the-grade/.meta/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ This concept exercise should also teach how to interrupt or change the normal fl

- [vectors](https://en.cppreference.com/w/cpp/container/vector-)
- [strings](https://en.cppreference.com/w/cpp/string/basic_string)
- [to\_string](https://en.cppreference.com/w/cpp/string/basic_string/to_string)
- [for](https://en.cppreference.com/w/cpp/language/for)
- [while](https://en.cppreference.com/w/cpp/language/while)

0 comments on commit 83295d5

Please sign in to comment.