C++ Tutorial 5 : Strings & Math

preview_player
Показать описание

In this part of my C++ tutorial we'll cover numerous ways to interact with Strings, numerous common Math functions and we'll solve a rather complex 2 part problem. The goal of this series is to teach the syntax of the C++ language while actually teaching how to solve problems using C++. Each tutorial includes increasingly more complex problems for you to solve.
Рекомендации по теме
Комментарии
Автор

FYI -- To get reverse working on mingw, need both #include <algorithm> and reference it as std::reverse, per helpful commenters below.

scramjet
Автор

Your Java tutorials got me interested in Java. I followed your first 40 tutorials orso and passed Java OCA 2 weeks ago. Now I've got myself a job as a developer. Thank you

TimonNetherlands
Автор

Derek you just keep giving! You really need more recognition for this amazing stuff!!

chukwuka-steveorefo
Автор

So far, only 1 Person solved the problem (ozbity). Can you solve this videos problem with fewer lines of code? If so, share it in the comments below. Thank you for watching :)

derekbanas
Автор

Just in case anyone is wondering; the length of a string array is found by dividing the total size in memory by the value of each string character in memory i.e.
However, the length of the cString can be derived simply with sizeof(cString). This is because each character is stored in memory as 1 byte. Dividing the total size by 1 gives the same number.

mrturnables
Автор

Derek, this is great. I haven't touched C++ since 1998 and this series got me back up to speed quickly after using C# for a number of years. Thanks for doing this.

melsilva
Автор

Regarding Reverse String example at 11:03 into video:
The reverse function requires a std:: in order for it to work. Hope this helps somebody :)
std::reverse(str.begin(), str.end());

techwithdave
Автор

If anyone is having issues with Transform function in Visual Studio. Make sure you are including the #include <algorithm>. Hope this helps somebody.

Conorkc
Автор

Be careful with multiple variable initialization on declaration (16:32) ! This doesn't initialize the first variable - this can result in unknown behaviour especially when using other data types.

janaebert
Автор

For the problem i saved each char into a vector and i was able to convert back and forth without subtracting 23

techwithjordan
Автор

For the uppercase probelm I just did:
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <numeric>
#include <sstream>
#include <math.h>
#include <algorithm>

using namespace std;
int main(){
int i = 0;
string sentencer;
cout << "Enter your sentence : " << "\n";
getline(cin, sentencer);
cout << sentencer << "\n";
for(i = 0; i < sentencer.length(); i++){
if(int(sentencer[i]) >= 65 && int(sentencer[i]) <= 90){
sentencer[i] = char(sentencer[i]+32);
cout << sentencer[i];

}
else if(int(sentencer[i]) >= 97 && int(sentencer[i]) <= 122){
sentencer[i] = char(sentencer[i]-32);
cout << sentencer[i];

}
else{
cout << sentencer[i];
}
}
}


This also can change full sentences and changes lowercase to uppercase, also does not transform symbols.

TravelingMooseMedia
Автор

Again, another topic I thought I knew a lot about, and I get shown just a bunch of new things as well, you're an excellent teacher Mr.Banas!
The last example for using both lower case and upper case letters was such an easy solution that I just didn't think about it at all! I also tried to use the example to take in full sentences with getline, but I soon found out the simple solution wouldn't work with it. As it takes in 2 numbers at a time and the blank space ASCII is 32 and we're subtracting 23 to take in both cases, you can see the issue I ran into with it as the rest of the sentence after the first blank space was all jumbled up!
As always, I can't wait to check out the next video on the playlist!

ryantripi
Автор

For the transform function you need to add #include <algorithm>

mcalex
Автор

on windows visual studios #include <algorithm> is required to get the transform to work. Just putting it in the comments for anyone else who runs into the same problem i did :)

matthewquinn
Автор

1:32 Can't i just use char cString = "something"; instead of having to put every char? I've seen many people do char cString[] = "text here"; when coding in C.

Another question, why do you do std::string str("im a string"); instead of doing std::string str = "im a string";?

TERIHAX
Автор

I'm so happy I found you back when I needed help with assembly. You're such a good teacher, keeping it simple and underatandable. Thank you for your vids!

lowochi
Автор

A comprehensive tutorial on Strings. thanks Derek

masoud
Автор

If I had this skill from the previous episode, my tree problem would have been much easier!!! LOL

mrturnables
Автор

@Derek Banas - have you already covered anything about casting up to this point in the series? I am not familiar with what you did in the std::to_string((int)a) at 17:40.
Here is my solution which I think gets the same results:

```
void StringToAscii(std::string inputString) {
// Convert each letter in a string to its ascii code and print it
std::string outputString = "";
std::cout << "The input string: " << inputString << "\n";

for (char S: inputString) {
int AsciiRepresentation = S;
std::cout << S << " becomes " << AsciiRepresentation << std::endl;
outputString +=
}
std::cout << "ASCII string: " << outputString << std::endl;
}
```

DDeathdealer
Автор

My solution for the first problem that uses vector for ASCI codes:

#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>

int main(){
std::string normalStr = "";
std::vector<int> nString;

std::cin >> normalStr;
int nLength = normalStr.length();

char cTemp;
for (int i = 0; i < nLength; ++i){
cTemp = normalStr[i];

}

for(auto y : nString)
printf("%d ", y);

return 0;
}

alaksiejhuchau
welcome to shbcf.ru