Find index of symbol in string [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 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.

Related

How can i make cin.ignore to take any non integer value to be an delimiter [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 7 months ago.
Improve this question
#include <iostream>
#include <string>
int main(){
int a, b;
std::cin >> a;
std::cin.ignore(3, '/');
std::cin >> b;
std::cout << a << " " << b;
return 0;
}
How can I change my code so that the separators do not have to be Slash characters “/” but any separator that is not an integer number.
You can't use ignore for that.
Instead use a loop to peek at and fetch the next character until the character matches your condition to stop "ignoring" character.
The characters you read that you want to "ignore", just don't do anything with them.

How can we insert a character to a string in c++? [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 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;
}

Is multiple times overwriting a file, safe with fstream [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 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.

C++ code crashing, can't figure out why [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
my C++ code is crashing when I execute Search via Word (2) from my part of the code.
What it does is scan for a .txt file, then print out some information then it gives me options, my 2nd option is crashing my code.
The part of my code which is causing it to crash is, it's goal to to read user input then scan the file for a matching word then print out it's definition.
case 2:
{
string searchWord;
cout << "Enter a word to search for: ";
std::getline(std::cin, searchWord);
Word *myWord = Dic.findWord(searchWord);
if (myWord != NULL)
{
cout << myWord->definition;
}
break;
}
Your logic in findWords is wrong, you should check for MAX_WORDS before you try to compare not afterwards. Like this
Word* Dictionary::findWord(string searchWord)
{
int wordIndex = 0;
while (wordIndex < MAX_WORDS) {
if (myWords[wordIndex]->word.compare(searchWord) == 0) {
return myWords[wordIndex];
}
wordIndex++;
}
cout << "word not in dictionary";
return NULL;
}

how can i read a series of character value as character and integer simultaneously and can compare 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 8 years ago.
Improve this question
I have a file containing a series of characters like:
......//////////0000000111111111222222222aaaaaaaaaaccccccccccccclllllllllllllllll
I have to scan it from one by one character and have to compare if it is a number or not but in the form of integer.
I used like this:
int x=0;
fscanf(fp,"%d",&x)
if (x>=0 && x<=9)
I must have to read the numbers in the file in integer form and have to compare it.
In c++:
#include <iostream>
#include <locale>
char c;
int i;
while(std::cin >> c) {
if(isdigit(c)) {
i = c - '0';
} else {
//TODO:
}
}
A C++ answer:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream in_file("chars.txt");
char c;
while (in_file >> c)
if (isdigit(c))
cout << c << endl;
}