Typical array program in C++ [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to create a program in C++ that arranges inputted words in alphabetical order. Can any one help.
I tried arranging taking first letter using for loop but so far it gives me absurd result. I only want a little hint and will do the rest on own

Yes this is easy. The standard library does all the work for you. The string has an operator< which is compared lexicographically. So really, it's a waste of time. std::sort will call operator< on its parameters.
Warning: by default, lexicographically means it will compare the ASCII values. So the exclamation mark goes before numbers, and numbers go before capital letters, and capital letters go before lowercase letters.
string myWords[10];
for (int i = 0; i < 10; i++)
cin >> myWords[i];
sort(begin(myWords), end(myWords));
If you have a vector, just do v.begin() and v.end().

Related

C++ String individual characters [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm trying to make a function that takes the first and last letters of a string, divides them for the modulus and stores them in an array spot related to the modulus number. However I don't know how I would get the first and last letters of the string.
Assuming std::string str, you can use either:
str.front() and str.back() (C++11 and later only).
str[0] and str[str.size()-1], if str is not empty.
You can have the first letter of your string by just using yourString[0] and to get the last latter you can use yourString[yourString.size()-1]
EDIT
As said in the comments, assuming you're using std::string

C++ compare two sentences [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
randomly came across this:
Develop an algorithm to compare two sentences to see if
they match or not. The key aspect of these sentences is that
the words could be in any order (e.g. "california is hot" and "
hot is california" are two sentences that would match).
any ideas?
Parse each sentence into words, use space as delimiters.
Add all std::string words to a std::vector<std::string>, then sort.
Use the ==operator to compare the two vectors for equality.
Perhaps put words into a std::map<string, int> and count up the element each time you find a word on the one side, and down on the other side, then iterate over the map and check that all entries are zero. [This assumes that "california is hot hot" isn't supposed to be the same as "hot is california", in which case you need a bit more logic, to only count words the first time you see them on each side]
Or put each word in each sentence into a std::vector<string>, then sort each vector and compare the two vectors. Again, strategy changes if the sentence needs to be recognised regardless of the number of times each word is seen.

How to split a long input into multiple items in C++? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm new to C++ and have been trying to code a program where I enter many things at once and it splits them into different strings, ints, etc. depending on what they are but I can't figure out how.
I want to be able to input, for example, "What is 7 plus 9?" and code it so it assigns the first number (7) to int a, the second number (9) to int b, recognize the word "plus" to be the operator. This would then go to a simple calculator program which I have already coded fine and would then output the answer.
How do I code it so it can split the entire input into multiple individual items? I was thinking there must be some function for a format of input in which case I could define input as string1, string2, int a, string 3, int b, and just have it know the start/end of each by the spaces. Anything you have in mind that could do this please let me know.
Thanks
ideally a lexer and parser like antlr or lex/yacc would be great but the learning curve is steep

Count char occurs in string with length 10^200 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a string on Linux standard terminal input. Its maximal length is 10^200 (as said in program specification). I have to count, how many "3" characters are inside it (occur in this string). I couldn't do that by for loop, because there is no so big variable type, which can be used as iterator. Is there any way to analyze so big strings?
Is there any way to analyze so big strings?
Not in this universe there is not. Such an entity cannot exist in this universe1 and that which does not exist, cannot be analyzed.
1 Current estimates of this universe's total number of particles particles are in the region of 1080.
As from your comment
The data source is standard terminal input.
Then you'll need a lot of monkeys to type this in.
Though you don't need to read in what's typed at once into a big string, but you can simply analyze char by char as typed. The std::istream::get(char_type& ch) method is suitable for doing so.

How to select a word from a dictionary which has the highest score and print it [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Hey I'm trying to make a function, the function its self finds the word with the highest score and then list that word, I'm currently using a vector to find the word with the highest score but I wish to get the word from my dictionary.
The vector you store your words in is a vector of std::string, and not a vector of Word pointers. So you can't use compare_by_score as it expects the latter kind of arguments and not the first.
Either store Word pointers in the vector, or change the compare_by_score function to take const std::string& arguments and then in the function convert it to Word objects.