ok, just 1 problem, wat do u mean by character of message at position i??
wont i have to use arrays?
this is wat i have
for (n = 0; n > 40; n++)
{
if ( (here is my problem) ) - how do i check the position?
{
sum++;
}
return sum;
}
The loop you have here will check the first 40 characters of a string - all well and good if you have a string of length 40. However, this is unlikely. The first thing I'd suggest is using the string's length (by using the stringVariable
.length() method) to control
n.
- for (int n = 0; n < stringVariable.length(); n++) {
Next, the comparison.
n will loop through position 0 to stringVariable.length() - 1; in other words, every position of a character in the string. Consider the following statement:
- char ch = stringVariable[n];
This stores the character at position
n into
ch - you can then compare
ch to 'e' or 'E' with the following if statement:
- if (ch == 'E' || ch == 'e') {
The || means "OR" to C++; in other words, the if statement will be true if
ch is E OR if
ch is e.
The character of
message at position
i means the following:
Suppose I have the following statements:
- ...
-
string message;
-
message = "This is a sample string.";
-
...
Suppose I then declare a char variable named
ch, and set
ch to message[10]. Then
ch is now 's' - the s starting the word sample. A string is actually an array of characters - the string above,
message, is actually an array of type char with length 24 (having values at
message[0]...
message[23]). Thus, you can access any character in
message by accessing the position of that character in the string as you would a normal array. The g in that string, therefore, is
message[22], and so on.