Need help on getting characters from a string? - c++

I'm currently in an Intro C++ class, and I'm learning about Strings and Member Functions of them.
I have questions that are like this:
Assume that name is a variable of type string that has been assigned a value. Write an expression whose value is a string containing the first character of the value of name . So if the value of name were "Smith" the expression's value would be "S".
or
Assume that name is a variable of type string that has been assigned a value. Write an expression whose value is a string containing the last character of the value of name . So if the value of name were "Smith" the expression's value would be "h".
or
Assume that word is a variable of type string that has been assigned a value. Write an expression whose value is a string consisting of the last three characters of the value of word . So if the value of word were "biggest" the expression's value would be "est".
I know things like name[0] and name[name.length() - 1], but I don't know how to turn those into a string in one expression. I've been looking for a table or list of member functions that can help me do this, but I'm stuck. Any directions or aid would be great. :D

Take a look at the substr method.
http://www.cplusplus.com/reference/string/string/substr/

I would use substring:
//assume that the string in question is held in a variable with the name str
string s1 = str.substr(0,1);
string s2 = str.substr(str.length() - 2, 1);
string s3 = str.substr(str.length() = 4, 3);
Hopefully that helps!

There are quite a few number of ways to do it. For instance, an std::string is also a standard container of char. So you could begin with an empty string, and append one char to it.

Related

How to capitalise and change last special character in a string stored in the variable?

How can I convert this string hello,how,are,you to Hello,How,Are & You.
The text is contained in a variable. I would like to solve it by using regex. I have to use this in tasker.
I'm unclear of your question, however with the assumption this could be in Python, there is an function .title() you can use with strings.
e.g.
stringVar = "hello,world"
stringVar.title()
output:
"Hello,World"

How do I split a word into individual letters in Python 2.7

I have a number that I have converted into a string. I now want to assign each of the digits of this number to a new variable that I want to use later. How do I do it?
For example if
input = "98912817271"
How do I assign the one's digit, 1, to a variable 1 and so on?
I've tried searching for a solution but couldn't find any on StackOverflow.Any help would be much appreciated.
in python, words are already lists, this means, they already have positions asigned,
try: print input[0] and see
if you want to assign a variable the value of any position in your string, just select the position as if it was a list:
foo = input[#]
Try this:
def string_spliter(s):
result = []
for element in s:
result.append(element)
return result
print(string_spliter(string))

matlab regexprep multiple strings with multiple numbers

given a cell array of strings, I want to build one regexprep rule, so that different string types are replaced by a certain number. I.e:
my_cell = {'ok', 'ok', 'bad', 'broken', 'bad', 'broken', 'ok'};
I know how to replace each string type one by one, i.e:
my_cell = regexprep(my_cell,'ok$','1');
but ideally I would like to build one rule, so that ok will be replaced with 1, bad will be replaced with 0 and broken will be replaced with -1.
any hints on how to do this?
How about:
>> my_cell = regexprep(my_cell,{'ok$','bad$','broken$'},{'1','0','-1'});
There's documentation here: http://www.mathworks.co.uk/help/techdoc/ref/regexprep.html
It gives the syntax as: s = regexprep('str', 'expr', 'repstr')
It also says: "If both expr and repstr are cell arrays of strings, then expr and repstr must contain the same number of elements, and regexprep pairs each repstr element with its matching element in expr."
Therefore you could try something like this:
my_cell = regexprep(my_cell, {'^ok$', '^bad$', '^broken$'}, {'1', '0', '-1'});
(Untested)

Part of as string from a string using regular expressions

I have a string of 5 characters out of which the first two characters should be in some list and next three should be in some other list.
How could i validate them with regular expressions?
Example:
List for First two characters {VBNET, CSNET, HTML)}
List for next three characters {BEGINNER, EXPERT, MEDIUM}
My Strings are going to be: VBBEG, CSBEG, etc.
My regular expression should find that the input string first two characters could be either VB, CS, HT and the rest should also be like that.
Would the following expression work for you in a more general case (so that you don't have hardcoded values): (^..)(.*$)
- returns the first two letters in the first group, and the remaining letters in the second group.
something like this:
^(VB|CS|HT)(BEG|EXP|MED)$
This recipe works for me:
^(VB|CS|HT)(BEG|EXP|MED)$
I guess (VB|CS|HT)(BEG|EXP|MED) should do it.
If your strings are as well-defined as this, you don't even need regex - simple string slicing would work.
For example, in Python we might say:
mystring = "HTEXP"
prefix = mystring[0:2]
suffix = mystring[2:5]
if (prefix in ['HT','CS','VB']) AND (suffix in ['BEG','MED','EXP']):
pass # valid!
else:
pass # not valid. :(
Don't use regex where elementary string operations will do.

Finding a substring within a string

In C++ I have a phonebook with many names, such as Sinatra, Frank, and I want the user to be able to input any length of string to scan the file for it. Once I have the user input a string of any desired length, how do I scan an entire string of "Sinatra, Frank" for just "Frank" or "Sinatra" or "atra" and see which name(s) it belongs to?
You can use the std::string::find method:
string s = "Sinatra, Frank";
string::sizetype index = s.find("Frank");
This gets you the index of the match (which in this case is 9).
A question: is your phonebook a flat file with each name on a new line (as in your example with "Sinatra, Frank" in a format like "Lastname, Firstname", etc.), or do you have some structure of this phonebook where each name-string is a node of an array, a linked list, etc?
Note that for strstr():
strstr(const char *s1, const char *s2)
locates the first occurrence of string s2 in s1, which may be sufficient for you.
For your input string, always be sure to check size limits in one way or another; if the user enters a string through some interface it should be explicitly handled to ensure it doesn't exceed your storage for it or contain malevolent characters or code.
Ken's solution produces the position of the substring in the original string (and so long as it's not null that mean's there's a 'hit') but doesn't tell you which entry of the phonebook is the hit; your code will need to track which entry/entries are hits so you can return a meaningful set of results.
You can use strstr() to locate one substring within a string.
If it's a std::string, you can use the .find() method of the "Sinatra,Frank" string