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;
}
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

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