I want to be able to search an array of strings in C++. I have this data:
"Foo Becky, 924-334-2514",
"Becky Warren, 555-1223",
"Geri Palmer, 555-8787",
"Ron Palmer, 555-2783"
If the user types Bec, the program finds the name Foo Becky, 924-234-2314. If the user types Palmer, then the program should show Geri Palmer, 555-8787 and Ron Palmer, 555-2783
Here is what I have so far:
#include <iostream>
#include <string>
using namespace std;
int main(){
int n;
string search;
while(1){
cout << "How many data you want to input: "<< endl;
cin >> n;
cin.ignore(1000, 10);
if(n > 0 && n < 20){
break;
}
cout << "Number of data can not be negative or more than 20. "<< endl;
}
string* data = new string[n];
for(int i=0; i< n; i++){
cout << "Enter [First Name] [Last Name], [Phone-Number] and then hit "
<< "enter." << endl << "e.g: Foo Becky, 925-245-413"<< endl;
getline(cin,data[i]);
cout << endl;
}
cout << "Enter what you want to search for: "<< endl;
getline(cin, search);
for(int i =0; i< n; i++){
if(search == data[i]){
cout << data[i]<< endl;
}
}
delete [] data;
return 0;
}
How do I search an array of strings in C++?
You have to use the find method of std::string. This function return the start postion of string searched for in the string searched in. If no match was found it returns npos witch is actually just -1.
if(data[i].find(search, 0) != std::string::npos);
{
cout << data[i]<< endl;
}
You should use find, as A4L already mentioned. I just want to add that your use of cin.ignore will not work well if bad value entered. you need
cin.clear()
also. see this link for more details.
How to search an array of strings example in C++:
This is the brute force search method. Brute force means means we step through the entire string array and at each index of the array we search for our matching string.
#include<iostream>
#include<string>
using namespace std;
int main(){
//Create a structure to hold the data:
string data[] = {"pidgeon", "abcd", "1234", "%^*#"};
//Get the length of the array.
int size = 4;
//loop through all the items and print them if they match
string matchString = "bc";
for(int x = 0; x < size; x++){
if (data[x].find(matchString, 0) != std::string::npos){
cout << data[x] << endl;
}
}
}
Above code prints:
abcd
You should be verbalizing the above code from top to bottom in your head like this:
An array of string called data is initialized to contain 4 elements and given four values pidgeon, abcd, 1234 and %^&#. An int variable called size is created which represents the number of elements in the string array. A string variable called matchString is created which contains the string 'bc'.
The for loop index starts at position zero and increments by 1 until it reaches one less than the size of the array. So the for loop goes through: 0, 1, 2, 3. The first value of x is 0. The if statement resolves data[0] to be pidgeon. The find method is applied against that string and two parameters are passed in. The string to be matched, and the position of the first character in the string to be considered in the search (0).
if 'bc' exists within 'pidgeon' then it will return the position of the first character of the first match, otherwise it will print std::string:npos which is the maximum value for size_t designating not found.
bc does not exist in pidgeon so it skips the interior of the for loop. The for loop continues to index position 1. bc is contained in abcd. So that string is printed. When all the items are searched, the for loop ends and the program completes.
Related
I'm creating a piece of code which is supposed to simulate a guessing game in which I pull a random line from a text file, in this example "premier league football teams", then I have to take the random line and have it convert into the corresponding numbers in the alphabet, so in the example of "arsenal" I would have to output "1 18 19 5 14 1 12". as this is a game the user has to guess that the numbers mean "arsenal" and then type that in to gain a point and continue. so far I have been able to code a method of pulling a random line from the file, however I am unsure how I would be able to then convert that code into numbers without outputting the answer, as this is meant to be a guessing game.
this is the relevant section of text, but to keep this short I will not paste the full code
vector<string> lines;
srand(time(NULL));
//ensures random selection on each new code run
ifstream file("Premier_League_Teams.txt");
//pulls the file from the directory
int total_lines = 0;
while (getline(file, line))
//this function counts the number of lines in a text file
{
total_lines++;
lines.push_back(line);
}
int random_number = rand() % total_lines;
//selects a random entry from the text file
//end of adapted code
cout << lines[random_number];
I am assuming that I will have to do a switch statement, but I do not know how to apply that case statement to the randomly selected line, and then have the user input the plain english text
NEW CODE WITH HELP FROM COMMENTS
#include <fstream>
//fstream included to pull answers from category files
#include <string>
#include <vector>
#include <list>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
{
cout << "Richard Osman's house of Games: This round is in code.\n\n\n";
cout << "The objective of this round is to unscramble the coded word, you will be given the category as the clue and you have to type out what you belive the answer to be, with a capital letter\n\n";
cout << endl;
string Name;
cout << "But First, Enter your first name.\n";
cin >> Name;
cout << "Welcome contestant " << Name << " Are you ready to begin the quiz?\n";
// this is to add a level of immersion to the game, by allowing the code to recall the players name so that they have a personalised experience
string respond;
cout << "please type Yes or No (case sensitive).\n";
cin >> respond;
if (respond == "Yes")
{
cout << "\nGood luck!";
}
else
{
cout << "Maybe next time!";
return 0;
}
{ cout << "The first category is..." << endl;
cout << "Premier League Football Teams!" << endl;
//code adapted from Ranjeet V, codespeedy
string line;
vector<string> lines;
srand(time(NULL));
//ensures random selection on each new code run
ifstream file("Premier_League_Teams.txt");
//pulls the file from the directory
int total_lines = 0;
while (getline(file, line))
//this function counts the number of lines in a text file
{
total_lines++;
lines.push_back(line);
}
int random_number = rand() % total_lines;
//selects a random entry from the text file
//end of adapted code
int random_number = 0;
vector<string> lines;
lines.push_back("abc arsenal");
string alpha = "abcdefghijklmnopqrstuvwxyz";
string word = lines[random_number];
//line from lines vector for which we will calculate numbers
int* codes;
codes = new int[word.length()];
//array in which we will store codes for letters
for (int i = 0; i < word.length(); i++) {
//iterate over each characte of string word
if (word[i] != ' ')
//as we dont want to replace spaces with numbers or somethings, but it will automatically become 0 as it is default
codes[i] = alpha.find(word[i]) + 1;
//assign codes correspoding element to (index of char)+1
}
for (int i = 0; i < word.length(); i++) {
if (codes[i] == 0) {
continue;
//do not print codes' element if it is zero because it will become zero for spaces
}
cout << codes[i] << " ";
//outputting each codes element with a space in between
}
You can declare a string like
std::string alphabet="abcdefghijklmnopqrstuvwxyz"; and then for particular character if you want to find the equivalent position number you can use str.find(char); to get index of char in str and then add one to get its position number.
For eg;
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
int random_number=0;
vector<string> lines;
lines.push_back("abc arsenal");
string alpha="abcdefghijklmnopqrstuvwxyz";
string word=lines[random_number]; //line from lines vector for which we will calculate numbers
int* codes;
codes=new int[word.length()]; //array in which we will store codes for letters
for(int i=0;i<word.length();i++) { //iterate over each characte of string word
if(word[i]!=' ') //as we dont want to replace spaces with numbers or somethings, but it will automatically become 0 as it is default
codes[i]=alpha.find(word[i])+1; //assign codes correspoding element to (index of char)+1
}
for(int i=0;i<word.length();i++) {
if(codes[i]==0) {
continue; //do not print codes' element if it is zero because it will become zero for spaces
}
cout<<codes[i]<<" "; //outputting each codes element with a space in between
}
}
[Note] : Just for example I have assigned random_number to 0 and made a sample vector lines so that you can have more clarity on how to use this method in your case.
the char datatype have a numerical value and are displayed as a character. We make use of this!
Lowercase a = 97, lowercase b = 98, etc. So if you subtract 96 and then cast it to an interger using (int), you will be all set :)
For my code, I am trying to create a class with two functions that:
Display a cstring where each word is reversed
Display an entire cstring reversed
My two test sentences are "Hi There" and "To Be", so the output is:
erehT iH
eB oT
iH erehT
oT eB
Here is my code:
#include <iostream>
#include <cstring>
using namespace std;
class cStringType {
public:
char sentenceInput[80]; //Member variable
void reverse_sentence(); //Member function
void reverse_words(); //Member function
}; //Bottom of cStringType
int main()
{
cStringType sentence1, sentence2;
//Objects declared of cStringType
cout << "Please enter a sentence!\n" << endl;
cin.get(sentence1.sentenceInput, 79, '\n');
cin.ignore(80, '\n');
cout << "\nPlease enter another sentence!\n" << endl;
cin.get(sentence2.sentenceInput, 79, '\n');
cout << "\nThe first sentence reversed: ";
sentence1.reverse_sentence();
cout << endl;
cout << "The second sentence where each word is reversed: ";
sentence2.reverse_words();
cout << endl;
cout << endl;
cout << "The first sentence where each word is reversed: ";
sentence1.reverse_words();
cout << endl;
cout << "The second sentence reversed: ";
sentence2.reverse_sentence();
cout << endl;
return 0;
}
void cStringType::reverse_sentence()
{
char reverse_sentence;
//Reverse entire sentence using loop
for (int i = 0; i < strlen(sentenceInput) / 2; i++)
{
//Reverse the sentence using the length of the
//variable in the class
reverse_sentence = sentenceInput[i];
//First get the user input
//Set your variable equal to the variable in the class
sentenceInput[i] = sentenceInput[strlen(sentenceInput) - i - 1];
//Then reverse the characters and word order
//Starts from the last character in the array
//and goes backwards to 0
sentenceInput[strlen(sentenceInput) - i - 1] = reverse_sentence;
//Set the variable equal to the result
//sentenceInput is now the reverse of the user input in main
}
cout << sentenceInput << endl;
//Output of the new sentence
}
void cStringType::reverse_words()
{
int beginning, end, j = 0;
char reverse_words;
//Reverse each word separately using loop
for (int i = 0; i <= strlen(sentenceInput); i++)
//Get the length of the sentence in the class
{
if (sentenceInput[i] == ' ' || sentenceInput[i] == '\0')
//Check for spaces or null characters
//This allows only the letters of each word to be
//reversed, not the entire sentence
{
for (beginning = j, end = i - 1;
beginning < (i + j) / 2; beginning++, end--)
//j is the beginning of the array; increases
//i is the end of the array; decreases
{
reverse_words = sentenceInput[beginning];
//Set a variable equal to the first
//word in the original user input
sentenceInput[beginning] = sentenceInput[end];
//Set the first letter of a word equal to
//the last letter of a word
sentenceInput[end] = reverse_words;
//Set the result equal to the variable
//sentenceInput is now the user input where each
//word is reversed
}
}
j = i + 1;
}
cout << sentenceInput << endl;
//Output of the new sentence
}
When I try to run the code, the output becomes something like this:
Please enter a sentence!
Hi There
Please enter another sentence!
To Be
The first sentence reversed: erehT iH
The second sentence where each word is reversed: oT eB
The first sentence where each word is reversed: There Hi
The second sentence reversed: Be To
I tried fixing it, but to no avail. The output is never correct.
Is there some way to fix this issue? Or better yet, to simplify the code? I believe the issue is with the code in the function.
The main problem with your code is that it's using the same buffer for both transformations. In other words: you are reversing the words in the same string which you've already reversed entirely. So you need to have another copy of the original string to do these independently.
Regarding simplifying your code you need to define a function that would reverse a string given a pointer and size or begin and end pointers. Then you can use this function on your entire string or on every word you find while searching for a space character:
char *begin = sentenceInput; //points to the beginning of the word
char *end = sentenceInput + strlen(sentenceInput);
for (char *it = begin; it != end; ++it)
if (*it == ' ') {
reverse(begin, it);
begin = it + 1;
}
reverse(begin, end); //reverse the last word
The reverse function can be either std::reverse, which can be used in the above code and on the entire string as follows:
std::reverse(sentenceInput, sentenceInput + strlen(sentenceInput))
or you can create a similar function like this:
void reverse(char *begin, char *end)
{
--end; //point to the last character instead of one-past-last
while (begin < end)
std::swap(*begin++, *end--);
}
I would suggest using stack for it, it is a natural way of looking at it.
so
#include <stack>
and then the function would be like that
void cStringType::reverse_words()
{
int beginning, end, j = 0;
char reverse_words;
stack<char> lastWord;
//Reverse each word separately using loop
for (int i = 0; i <= strlen(sentenceInput); i++)
//Get the length of the sentence in the class
{
if (sentenceInput[i] == ' ' || sentenceInput[i] == '\0')
//Check for spaces or null characters
//This allows only the letters of each word to be
//reversed, not the entire sentence
{
//we want to print the last word that was parsed
while(!lastWord.empty())
{
//we print in the reverse order the word by taking off the stack char by char
cout<< lastWord.top();
lastWord.pop();
}
cout<<" ";
}
//if the letter is not space or end of string then push it on the stack
else
lastWord.push(sentenceInput[i]);
j = i + 1;
}
cout << sentenceInput << endl;
//Output of the new sentence
}
Here is the instructions:
Write a program that reads in a text file one word at a time. Store a word into a dynamically created array when it is first encountered. Create a paralle integer array to hold a count of the number of times that each particular word appears in the text file. If the word appears in the text file multiple times, do not add it into your dynamic array, but make sure to increment the corresponding word frequency counter in the parallel integer array. Remove any trailing punctuation from all words before doing any comparisons.
Create and use the following text file containing a quote from Bill Cosby to test your program.
I don't know the key to success, but the key to failure is trying to please everybody.
At the end of your program, generate a report that prints the contents of your two arrays
Here is my Code:
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cctype>
using namespace std;
int main()
{
ifstream inputFile;
int numWords;
string filename;
string *readInArray = 0;
char testArray[300] = {0};
char *realArray = 0;
const char *s1 = 0;
string word;
int j =1;
int k = 0;
int start =0;
int ending = 0;
char wordHolder[20] = {0};
cout << "Enter the number of words the file contains: ";
cin >> numWords;
readInArray = new string[(2*numWords)-1];
cout << "Enter the filename you wish to read in: ";
cin >> filename;
inputFile.open(filename.c_str());
if (inputFile)
{
cout << "\nHere is the text from the file:\n\n";
for (int i=0; i <= ((2*numWords) -1); i +=2)
{
inputFile >> readInArray[i]; // Store word from file to string array
cout << readInArray[i];
strcat(testArray, readInArray[i].c_str()); // Copy c-string conversion of word
// just read in to c-string
readInArray[j] = " ";
cout << readInArray[j];
strcat(testArray, readInArray[j].c_str()); // This part is for adding spaces in arrays
++j;
}
inputFile.close();
}
else
{
cout << "Could not open file, ending program";
return 0;
}
realArray = new char[strlen(testArray)];
cout << "\n\n";
for(int i=0; i < strlen(testArray); ++i)
{
if (isalpha(testArray[i]) || isspace(testArray[i])) // Is makes another char array equal to
{ // the first one but without any
realArray[k]=testArray[i]; // Punctuation
cout << realArray[k] ;
k++;
}
}
cout << "\n\n";
for (int i=0; i < ((2*numWords) -1); i+=2)
{
while (isalpha(realArray[ending])) // Finds space in char array to stop
{
++ending;
}
cout << "ending: " << ending << " ";
for ( ; start < ending; ++start) // saves the array up to stopping point
{ // into a holder c-string
wordHolder[start] = realArray[start];
}
cout << "start: " << start << " ";
readInArray[i] = string(wordHolder); // Converts holder c-string to string and
cout << readInArray[i] << endl; // assigns to element in original string array
start = ending; // Starts reading where left off
++ending; // Increments ending counter
}
return 0;
}
Output:
Enter the number of words the file contains: 17
Enter the filename you wish to read in: D:/Documents/input.txt
Here is the text from the file:
I don't know the key to sucess, but the key to failure is trying to please everybody.
I dont know the key to sucess but the key to failure is trying to please everybody
ending: 1 start: 1 I
ending: 6 start: 6 I dont
ending: 11 start: 11 I dont know
ending: 15 start: 15 I dont know the
ending: 19 start: 19 I dont know the key
ending: 22 start: 22 I dont know the key to>
ending: 29 start: 29 I dont know the key to sucess
ending: 33 start: 33 I dont know the key to sucess but↕>
My Question:
Something is wrong with the last for-loop, it crashes after I run it. I included the ending and starting variables to maybe help see whats going on. I know there are better ways of doing this problem but the instructor wants it done this way. If you know where I went wrong with the last for-loop any help would be very much appreciated!!
You aren't null-terminating your strings as you go along. You copy the characters correctly, but without null terminators, your loops might go off into the weeds.
Here is the assignment:
Write a program that reads in a text file one word at a time. Store a word into a dynamically created array when it is first encountered. Create a paralle integer array to hold a count of the number of times that each particular word appears in the text file. If the word appears in the text file multiple times, do not add it into your dynamic array, but make sure to increment the corresponding word frequency counter in the parallel integer array. Remove any trailing punctuation from all words before doing any comparisons.
Create and use the following text file containing a quote from Bill Cosby to test your program.
I don't know the key to success, but the key to failure is trying to please everybody.
At the end of your program, generate a report that prints the contents of your two arrays in a format similar to the following:
Word Frequency Analysis
I 1
don't 1
know 1
the 2
key 2
...
Here is my code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int readInFile (string tempArray [], string file, int arraySize);
int main()
{
ifstream inputFile;
string *readInArray = 0,
*compareArray = 0,
filename,
word;
int wordCount = 0;
int encountered = 0;
int j = 0,
*wordFrequency = 0;
cout << "Enter the filename you wish to read in: ";
getline(cin, filename);
inputFile.open(filename.c_str());
if (inputFile)
{
while (inputFile >> word)
{
wordCount++;
}
inputFile.close();
readInArray = new string[wordCount];
readInFile(readInArray, filename, wordCount);
}
else
{
cout << "Could not open file, ending program";
return 0;
}
compareArray = new string[wordCount];
wordFrequency = new int[wordCount];
for (int count = 0; count < wordCount; count++)
wordFrequency[count] = 0;
for(int i = 0; i < wordCount; ++i)
{
j = 0;
encountered = 0;
do
{
if (readInArray[i] == compareArray[j])
encountered = 1;
++j;
} while (j < wordCount);
if (encountered == 0)
{
compareArray[i]=readInArray[i];
wordFrequency[i] += 1;
}
}
for(int k=0; k < wordCount; ++k)
{
cout << "\n" << compareArray[k] << " ";
}
for(int l=0; l < wordCount; ++l)
{
cout << "\n" << wordFrequency[l] << " ";
}
return 0;
}
int readInFile (string tempArray [], string file, int arraySize)
{
ifstream inputFile;
inputFile.open(file.c_str());
if (inputFile)
{
cout << "\nHere is the text file:\n\n";
for(int i=0; i < arraySize; ++i)
{
inputFile >> tempArray[i];
cout << tempArray[i] << " ";
}
inputFile.close();
}
}
Here is my question:
How do you store a word into a dynamically created array when it is first encountered? As you can see from my code made a string array with some of the elements empty. I believe it is suppose to be done using pointers.
Also how do I get rid of the punctuation in the string array? Should it be converted to a c-string first? But then how would I compare the words without converting back to a string array?
Here is a link to a java program that does something similar:
http://math.hws.edu/eck/cs124/javanotes3/c10/ex-10-1-answer.html
Thank you for any help you can offer!!
As to the first part of your question, you are not using a dynamically created array. You are using a regular array. C++ provides implementations of dymnamic arrays, like the vector class http://www.cplusplus.com/reference/vector/vector/
As to the second part of your question, I see no reason to convert it to a c string. The string class in c++ provides functionality for removing and searching for characters. http://www.cplusplus.com/reference/string/string/
The string::erase function can be used to erase punctuation characters found with string::find.
Note: There are other ways of doing this assignment that may be easier (like having an array of structs containing a string and an int, or using a map) but that may defeat the purpose of the assignment.
The assignment requires three functions. The user enters digits 0-9 until they enter a 10, which stops input, and counts each number, then outputs how many of each number has been counted. It should only output if the user entered a number for it.
My only problem is that for every element in the array that the user doesn't use, Xcode counts it as a 0, so the final output has an abnormally large amount of zeros. Everything else works fine.
here is my code
#include <iostream>
using namespace std;
// counter function prototype
void count(int[], int, int []);
// print function prototype
void print(int []);
int main()
{
// define variables and initialize arrays
const int SIZE=100;
int numbers[SIZE], counter[10], input;
// for loop to set all counter elements to 0
for (int assign = 0; assign < 10; assign++)
{
counter[assign]=0;
}
// for loop to collect data
for (int index=0 ; input != 10 ; index++)
{
cout << "Enter a number 0-9, or 10 to terminate: ";
cin >> input;
// while loop to ensure input is 0-10
while (input < 0 || input > 10)
{
cout << "Invalid, please enter 0-9 or 10 to terminate: ";
cin >> input;
}
// if statements to sort input
if (input >= 0 && input <=9)
{
numbers[index] = input;
}
}
// call count function
count(numbers, SIZE, counter);
// call print function
print(counter);
return 0;
}
// counter function
void count(int numbers[], int SIZE, int counter[])
{
// for loop of counter
for (int index = 0 ; index < 10 ; index++)
{
// for loop of numbers
for (int tracker=0 ; tracker < SIZE ; tracker++)
{
// if statement to count each number
if (index == numbers[tracker])
{
counter[index]++;
}
}
}
return;
}
// print function
void print(int counter[])
{
// for loop to print each element
for (int index=0 ; index < 10 ; index++)
{
// if statement to only print numbers that were entered
if (counter[index] > 0)
{
cout << "You entered " << counter[index] << ", " << index << "(s)" << endl;
}
}
return;
}
What you're referring to as "XCode count[ing] as a 0" is actually just the uninitialized value. Given that you've decided to restrict the user's input to 0-9, an easy way of solving this dilemma would be, immediately after you size the array, to iterate through the array and set each value to -1.
Thereafter, when the user finishes their input, instead of just couting every single value, only print it with a conditional like the following:
if (counter[index] != -1)
{
cout << "You entered " << counter[index] << ", " << index << "(s)" << endl;
}
Note that this is the kind of use case that's much better suited to something like a linked list or a vector. As it stands, you're not doing anything to resize the array, or guard against overflow, so if the user attempts to enter more than 100 numbers, you'll run into serious problems.
First off, this isn't an answer to your exact question, but rather a suggestion on how to write your code in a much simpler form.
I'm not going to write this for you, as it's an assignment, and a rather simple one. Looks like you have a good handle on things as far as coding goes.
Consider this:
You need to allow the user to enter 0-10, and count all 0-9's. An array has indices, and a integer of array 10, would hold those 10 numbers you're counting by the indices. Now you just have some empty ints sitting around, so why not use them to count?
A code hint:
++numbers[input];
Second hint: Don't forget to initialize everything to zero.