Skip to content

GH-217: String in C++ #224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions concepts/cpp/string/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ In competitive programming, programmers use strings or arrays to store and manip

```c++
#include <iostream>
#include <string>

using namespace std;
```
Expand Down Expand Up @@ -129,6 +130,27 @@ cout << "After : " << s << endl;
After : the cat sat on the mat


**Letters & Numbers**: Check if characters in the string are letters or numbers

You can use

* [`isalpha()`](https://cplusplus.com/reference/cctype/isalpha/) function to check if a character is a letter
* [`isdigit()`](https://cplusplus.com/reference/cctype/isdigit/) function to check if a character is a number
* [`isalnum()`](https://cplusplus.com/reference/cctype/isalnum/) function to check if a character is either a decimal digit or an uppercase or lowercase letter.


```c++
string s = "I have 2 dogs, a poodle named Max and a German Shepherd named 7h3o#d0r.";

for(int i = 0; i < s.length(); i++) {
if(isalnum(s[i])) {
cout << s[i];
}
}
```

Ihave2dogsapoodlenamedMaxandaGermanShepherdnamed7h3od0r

## 🔗 Further Reading

* [String (computer science)](https://en.wikipedia.org/wiki/String_(computer_science)), wikipedia
Expand Down
43 changes: 42 additions & 1 deletion concepts/cpp/string/notebook.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 3,
"metadata": {
"vscode": {
"languageId": "cpp"
Expand All @@ -34,6 +34,7 @@
"outputs": [],
"source": [
"#include <iostream>\n",
"#include <string>\n",
"\n",
"using namespace std;"
]
Expand Down Expand Up @@ -299,6 +300,46 @@
"cout << \"After : \" << s << endl;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Letters & Numbers**: Check if characters in the string are letters or numbers\n",
"\n",
"You can use\n",
"\n",
"* [`isalpha()`](https://cplusplus.com/reference/cctype/isalpha/) function to check if a character is a letter\n",
"* [`isdigit()`](https://cplusplus.com/reference/cctype/isdigit/) function to check if a character is a number \n",
"* [`isalnum()`](https://cplusplus.com/reference/cctype/isalnum/) function to check if a character is either a decimal digit or an uppercase or lowercase letter."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"vscode": {
"languageId": "cpp"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Ihave2dogsapoodlenamedMaxandaGermanShepherdnamed7h3od0r"
]
}
],
"source": [
"string s = \"I have 2 dogs, a poodle named Max and a German Shepherd named 7h3o#d0r.\";\n",
"\n",
"for(int i = 0; i < s.length(); i++) {\n",
" if(isalnum(s[i])) {\n",
" cout << s[i];\n",
" }\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
4 changes: 4 additions & 0 deletions concepts/general/string/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ Here are some basic operations that you can perform on strings:
* Substring: Extract a portion of the string
* Comparison: Compare two strings for equality
* Reversal: Reverse the order of characters in the string

More operations

* Uppercase: Convert all characters in the string to uppercase
* Lowercase: Convert all characters in the string to lowercase
* Letters & Numbers: Check if characters in the string are letters or numbers

## 🔗 Further Reading

Expand Down