C/C++ Two question on string access ops c++

  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++ String
AI Thread Summary
To assign the size of a string in C++, the correct approach is to use the `size()` or `length()` method of the string object. For example, if `userInput` is initialized with "Hello", the size can be assigned to `stringSize` using `stringSize = userInput.size();`. In the second part of the discussion, to check if the first character of `userInput` matches `firstLetter`, an if statement can be used with `userInput[0] == firstLetter`. The distinction between `size()` and `length()` is clarified, as they are interchangeable in this context. Understanding these methods is essential for proper 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: 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
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.
 
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.
 
what do i need the string length for?
 
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.
 
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 have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
1
Views
5K
Replies
14
Views
34K
Replies
2
Views
5K
Replies
7
Views
7K
Replies
22
Views
3K
Replies
8
Views
2K
Back
Top