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

  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++ String
AI Thread 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;
}
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Replies
4
Views
6K
Replies
14
Views
34K
Replies
2
Views
5K
Replies
7
Views
7K
Replies
22
Views
3K
Back
Top