C/C++ C++ String Access Ops: Assign Size of UserInput

  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++ String
Click For Summary
To determine the size of a string in C++, the `size()` function is utilized. In the provided sample program, the goal is to assign the size of the `userInput` string to the variable `stringSize`. The correct implementation involves calling `userInput.size()` to get the length of the string. The example shows that if `userInput` is set to "Hello", the output will be "Size of userInput: 5". The discussion emphasizes the importance of understanding how to use the `size()` method correctly within the context of string manipulation in C++.
ineedhelpnow
Messages
649
Reaction score
0
Assign the size of userInput to stringSize. Ex: if userInput = "Hello", output is:
Size of userInput: 5Sample 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;
}

help! i figured i have to use the function size() but not sure how in this case.
 
Technology news on Phys.org
Here is a sample program showing how to use the [m]size()[/m] function:

Code:
// string::size
#include <iostream>
#include <string>

int main ()
{
  std::string str ("Test string");
  std::cout << "The size of str is " << str.size() << " bytes.\n";
  return 0;
}
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 14 ·
Replies
14
Views
34K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 5 ·
Replies
5
Views
7K
  • · Replies 7 ·
Replies
7
Views
7K
  • · Replies 3 ·
Replies
3
Views
4K
Replies
12
Views
3K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 22 ·
Replies
22
Views
3K