C/C++ Detecting First Letter Match in C++ String with Substr Function

  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++ String
AI Thread Summary
To detect if the first character of `userInput` matches `firstLetter`, the expression should compare the first character of the string directly. The correct approach in C++ involves using indexing to access the first character of `userInput`. The expression can be written as `userInput[0] == firstLetter`. The discussion highlights the confusion around using the `find` method and the `substr` function, clarifying that `find` is not suitable for this task since it searches for a character within the string rather than checking the first character. The `substr` function is also mentioned, but it is unnecessary for this specific comparison. Instead, simply accessing the first character with `userInput[0]` provides a straightforward solution.
ineedhelpnow
Messages
649
Reaction score
0
Write an expression to detect that the first character of userInput matches firstLetter.

Sample program:

Code:
#include <iostream>
#include <string> 
using namespace std;

int main() {
   string userInput;
   char firstLetter = '-';

   userInput = "banana";
   firstLetter = 'b';

   if (<STUDENT CODE>) { 
      cout << "Found match: " << firstLetter << endl;
   }
   else {
      cout << "No match: " << firstLetter << endl;
   }

   return 0;
}
i tried userInput.find(firstLetter, 1) but it only partially passed
 
Technology news on Phys.org
Okay, we know the [m]str.substr(pos,len)[/m] method in C++ will return from the string [m]str[/m], the sub-string beginning at position [m]pos[/m] and spanning length [m]len[/m].

So, we want to see if the first character of the string [m]userInput[/m] matches the value of [m]firstLetter[/m].

First, what values of [m]pos[/m] and [m]len[/m] do we want?
 
um Mark...are you sure this is c++ you're talking about? (Blush) I've never heard of those commands before.
 
ineedhelpnow said:
um Mark...are you sure this is c++ you're talking about? (Blush) I've never heard of those commands before.

You told me that you are familiar with the [m]substr()[/m] function. You will replace [m]str[/m] with [m]userInput[/m] since this is the name of the string from which you wish to extract the first character. I was just giving you the general usage. You will also need to determine what values you need for the parameters [m]pos[/m] and [m]len[/m].
 
ive never heard of pos and lens before i meant.
 
ineedhelpnow said:
ive never heard of pos and lens before i meant.

They are just placeholders for the two parameters that the [m]substr()[/m] function expects. For example, if I have a string [m]str = "Hello!";[/m], then [m]str.substr(1,2)[/m] will return "el" because we start at position 1 (the second character) and are getting 2 characters.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
4
Views
6K
Replies
2
Views
5K
Replies
14
Views
34K
Replies
1
Views
5K
Replies
1
Views
1K
Replies
22
Views
3K
Replies
8
Views
2K
Back
Top