Hi guys,
I've been trying for over 3 hours just to know how to compare a char to a letter in a string. This is sure confusing, consider that I am a mechanical eng. student >.< Lets get straight to the point
Problem 1: passing string and char to a function, and display it
#include <iostream>
#include <string>
using namespace std;
void CountLetter(string a, char b)
{
cout << a << "\n";
cout << b << "\n";
}
int main()
{
CountLetter("tttyuttt", 't');
return 0;
}
Problem 2: after I can pass the char and string to that function, I can use the following code to figure out how many times t is repeated in the first argument.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1 ("Test stringeeeeeeeeeeee");
char str2 ('e');
int i;
int count=0;
for (i = 0 ; i < str1.length() ; i++)
{
if (str1[i]==str2)
count++;
}
cout << "total: " << count;
cin >> i;
return 0;
}
So I have two questions:
1. How do you guys pass char and string to a function (the correct way)?
2. Is my code for counting a letter in a string the best way? Is there any better/more efficient way to do it ?
Thank you for your help !