Two question on string access ops c++

  • C/C++
  • Thread starter ineedhelpnow
  • Start date
  • Tags
    C++ String
In summary, string access operations in C++ involve various methods for accessing and manipulating strings, such as reading/writing, finding characters/substrings, and concatenating/comparing. Individual characters can be accessed using square bracket notation, and strings can be modified using functions like "append()" and "erase()". The "find()" method can be used to check for specific characters/substrings in a string, and C++ strings differ from C-style strings in that they are objects with more built-in functions.
  • #1
ineedhelpnow
651
0
Assign the size of userInput to stringSize. Ex: if userInput = "Hello", output is:

Size of userInput: 5
Sample program:

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

int main() {
   string userInput;
   int stringSize = 0;

   userInput = "Hello"; 
   <STUDENT CODE>

   cout << "Size of userInput: " << stringSize << endl;

   return 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;
}
kinda lost. any hints on where to start?
 
Last edited:
Technology news on Phys.org
  • #2
If I recall correctly, C and its variants/derivatives use the [m]strlen()[/m] function to determine the length of a string...where the string variable is the function's parameter.
 
  • #3
MarkFL said:
If I recall correctly, C and its variants/derivatives use the [m]strlen()[/m] function to determine the length of a string...where the string variable is the function's parameter.

I don't think the old C style string functions should be used here.

The length of [m]string[/m] in C++ should be retrieved with either its [m]size()[/m] or its [m]length()[/m] method.
See for instance here for a reference.
 
  • #4
what do i need the string length for?
 
  • #5
ineedhelpnow said:
what do i need the string length for?

Because your problem statement asks you to "Assign the size of userInput to stringSize".
Note that "length" and "size" are interchangeable functions here.
 

FAQ: Two question on string access ops c++

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

String access operations in C++ refer to the various ways in which a programmer can access and manipulate strings, which are sequences of characters. This includes operations such as reading and writing to a string, finding specific characters or substrings within a string, and concatenating or comparing strings.

2. How can I access individual characters in a string in C++?

To access individual characters in a string in C++, you can use the square bracket notation. For example, to access the third character in a string named "str", you would use the expression "str[2]" (since the first character in a string has an index of 0).

3. Can I modify a string in C++?

Yes, you can modify a string in C++ using various string manipulation functions and operations. These include methods such as "append()", "insert()", "replace()", and "erase()". Keep in mind that strings in C++ are immutable, meaning that these operations will create a new string rather than modify the original one.

4. How can I check if a string contains a certain character or substring in C++?

To check if a string contains a certain character or substring in C++, you can use the "find()" method. This method takes in a character or substring as a parameter and returns the index of the first occurrence of that character or substring in the string. If the character or substring is not found, it will return a value of "npos".

5. What is the difference between C-style strings and C++ strings?

C-style strings are arrays of characters that end with a null character '\0', while C++ strings are objects that encapsulate a sequence of characters. C-style strings are typically used in older versions of C++, while C++ strings are more commonly used in modern C++ code. C++ strings also have more built-in functions and methods for string manipulation compared to C-style strings.

Similar threads

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