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

  • Context: C/C++ 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++ String
Click For Summary

Discussion Overview

The discussion revolves around detecting whether the first character of a user-input string matches a specified character using the C++ substr function. Participants explore the appropriate usage of the substr method and its parameters in this context.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant provides a sample C++ program and indicates a need for an expression to check if the first character of userInput matches firstLetter.
  • Another participant explains the substr method, asking for clarification on the values of pos and len needed to extract the first character.
  • Some participants express confusion about the commands and parameters being discussed, indicating a lack of familiarity with the substr function and its usage.
  • There is a reiteration of the purpose of pos and len as placeholders for the parameters of the substr function, with an example provided to illustrate their use.

Areas of Agreement / Disagreement

Participants show varying levels of understanding regarding the substr function and its parameters. There is no consensus on the correct implementation or understanding of the commands discussed.

Contextual Notes

Some participants appear to have differing levels of familiarity with C++ and the substr function, leading to confusion about its parameters and usage.

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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 14 ·
Replies
14
Views
35K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
12
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K