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

  • C/C++
  • Thread starter ineedhelpnow
  • Start date
  • Tags
    C++ String
In summary: So for our case, we want to start at position 0 (the first character) and only get 1 character. This will return "b", which is the first character of the string userInput. Therefore, our expression to detect if the first character of userInput matches firstLetter would be userInput.substr(0,1) == firstLetter. In summary, the expression to detect that the first character of userInput matches firstLetter is userInput.substr(0,1) == firstLetter.
  • #1
ineedhelpnow
651
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
  • #2
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?
 
  • #3
um Mark...are you sure this is c++ you're talking about? (Blush) I've never heard of those commands before.
 
  • #4
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].
 
  • #5
ive never heard of pos and lens before i meant.
 
  • #6
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.
 

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

1. What are string access operations in C++?

String access operations in C++ refer to the methods used to access individual characters or substrings within a string. These operations are important for manipulating and working with strings in C++.

2. How do I access a specific character within a string in C++?

To access a specific character within a string in C++, you can use the square bracket notation with the index of the desired character. For example, to access the 3rd character in a string named "str", you would use str[2].

3. Can I change the value of a character in a string using string access operations?

Yes, you can change the value of a character in a string using string access operations. Simply use the square bracket notation to access the desired character and assign a new value to it. For example, str[2] = 'a' would change the 3rd character in the string "str" to 'a'.

4. How can I extract a substring from a string in C++?

To extract a substring from a string in C++, you can use the substr() function. This function takes in two parameters - the starting index and the length of the desired substring. For example, str.substr(2, 4) would return a substring starting at the 3rd character and with a length of 4 characters.

5. Are there any other string access operations in C++?

Yes, there are other string access operations in C++ such as the at() function, which also allows you to access a specific character using an index, and the c_str() function, which returns a pointer to a null-terminated character array with the same content as the string. These and other string access operations can be useful for different purposes and can be explored further in the C++ documentation.

Similar threads

  • Programming and Computer Science
Replies
4
Views
6K
  • Programming and Computer Science
Replies
14
Views
31K
  • Programming and Computer Science
Replies
2
Views
5K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
1
Views
4K
  • Programming and Computer Science
Replies
1
Views
970
  • Programming and Computer Science
Replies
18
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top