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 12 months ago.
Improve this question
INPUT STRING
ABCE
INPUT CHAR
D
OUTPUT STRING
ABCDE
It should run for all sizes , and it should be a standard procedure ie run for all the cases.
Can anyone Help me with this? Any help would be appreciated.
You should try the std::string::insert(..). Check out the documentation
#include <iostream>
#include <string>
int main() {
std::string str("ABCE");
std::cout << str << std::endl; // ABCE
str.insert(3, 1, 'D');
std::cout << str << std::endl; // ABCDE
return -1;
}
Related
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 2 years ago.
Improve this question
I want to find index of '-' in string "book-Buch". What should I do? I mustn't use this function or any others. Any ideas?
int indexOf(int ch, int fromIndex)
#include <iostream>
#include <string>
int main() {
std::string word("book-Buch");
// The easy way
std::cout << word.find("-", 0) << '\n';
// The manual way
for (std::size_t i = 0; i < word.length(); ++i) {
if (word[i] == '-') {
std::cout << i << '\n';
}
}
}
If you just want to find the index that a certain character occurs in, you just need to look at each character and check if it's the one you want.
A string can be treated as an array of characters. It's unknown whether you actually want an array of string objects, or are just confused.
Other questions that would need to be answered: do you need to find all occurrences, or just the first? Are you reading the words out of a file? You don't clearly explain how a file comes into play.
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 5 years ago.
Improve this question
please find below program, the sort output is not proper.
string pru[] = { "ruthvi$p", "uthvi$pr", "thvi$pru", "hvi$prut", "vi$pruth", "i$pruthv", "$pruthvi", "pruthvi$" };
sort(pru, pru + 8, cmp);
for(int i = 0; i < 8; i++)
cout << pru[i] << " ";
output is
$pruthvi hvi$prut i$pruthv pruthvi$ thvi$pru ruthvi$p uthvi$pr vi$pruth
mistake "thvi$pru" is before "ruthvi$p"
It is not clear how cmp is defined but you can use this code and you will get the expected result.:)
#include <string>
#include <iterator>
#include <algorithm>
//...
std::string pru[] =
{
"ruthvi$p",
"uthvi$pr",
"thvi$pru",
"hvi$prut",
"vi$pruth",
"i$pruthv",
"$pruthvi",
"pruthvi$"
};
std::sort(std::begin(pru), std::end(pru));
for (const auto &s : pru) std::cout << s << std::endl;
The output is
$pruthvi
hvi$prut
i$pruthv
pruthvi$
ruthvi$p
thvi$pru
uthvi$pr
vi$pruth
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 6 years ago.
Improve this question
So my question is if I have a character array, I'm only allowed to enter characters in it. If I enter integer with character let's suppose "abc123" then this shouldn't be allowed. How to do I do this?
Use std::none_of, along with isdigit:
#include <algorithm>
#include <cctype>
#include <string>
#include <iostream>
int main()
{
std::string test = "abc123";
if ( std::none_of(test.begin(), test.end(), ::isdigit))
std::cout << "All good\n";
else
std::cout << "You've entered an integer\n";
// Try with good data
test = "abcdef";
if ( std::none_of(test.begin(), test.end(), ::isdigit))
std::cout << "All good\n";
else
std::cout << "You've entered an integer\n";
}
Live Example
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 7 years ago.
Improve this question
Im newbie in c++ programming. How can I do something like this?..
int question1;
question1: "What is your name?";
to set the text value in integer?
#include <string>
#include <iostream>
int main( )
{
std::string name;
std::cout << "What is your name?: " << std::endl;
std::cin >> name;
std::cout << "Your Name: " << name << std::endl;
std::cin.get( );
return 0;
}
Simply read the input in as string.
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 9 years ago.
Improve this question
Will fstream overwriting a file multiple times cause any problems?
No, it will not. Try this, you won't get any problems.
#include <iostream>
#include <fstream>
using namespace std;
int main(){
ofstream out;
for(int cnt = 0; cnt < 100; ++cnt){
out.open("file.txt");
out << "This will be written 100 times, and erased 99 times.";
// Uncomment the lines below if you want to see each change
//out << " Run#: " << cnt;
//cin.get();
out.close();
}
return 0;
}
Disclaimer: I did not try to run this code. Apologies if there are any syntax errors.