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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

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