C++ sorting vector strings not working - c++

For some reason I cannot get this sort the names correctly. Can anyone tell me what is wrong with it? As far as I can tell the problem is that the strings are not compared correctly. I have tried string comparisons before, and I know this kind of code should work. It really has me stumped.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
void sortNames(vector<string> &);
void main()
{
vector<string> namesList;
ifstream namesFile;
namesFile.open("Names.txt");
// Make sure the file exists.
if (namesFile)
{
// Get the names from the file.
string name;
while (getline(namesFile, name))
namesList.push_back(name);
// Sort the imported names.
sortNames(namesList);
for (int i = 0; i < namesList.size(); i++)
cout << namesList[i] << endl;
}
else
{
cout << "Data files are missing";
}
namesFile.close();
}
void sortNames(vector<string> &list)
{
for (int i = 0; i < list.size(); i++)
{
// Find the lowest value after i.
int lowIndex = i;
for (int j = i + 1; j < list.size(); j++)
{
string name = list[i];
string name2 = list[j];
if (name > name2)
lowIndex = j;
}
// Flip the elements if there was a value lower than i.
if (i != lowIndex)
{
string temp = list[i];
list[i] = list[lowIndex];
list[lowIndex] = temp;
}
}
}

Here is the problem: this line
string name = list[i];
should be
string name = list[lowIndex];
Your current implementation compares the element at j not to the smallest string that you have found so far, but to the string at index i. That is incorrect, because it does not find the smallest remaining string: instead, it finds the last string in the vector that is smaller than the current element at index i, which is not what you want.

rather than string name = list[i];, you want string name = list[lowIndex];

Related

c++ Deleting duplicated Names in array

EDIT: Thank you so much everyone, songyuanyao Answered my question, and you guys did lol but i didnt know some codes u put and im sure soon i will learn them :) thanks again.
i really have a question about removing duplicated Names in a string, note that im fairly new to c++, anyways i'll get to the point.
what im trying to do is removing duplicated names in an array, the code below is working fine but here's what im facing.
For example i entered 4 names: (Hana, Alex, Hana, Alex) the results that i want to get is just : ( Hana and Alex) while the other 2 names should be removed yet what im getting is ( Hana , Alex, Alex).
im really confused about what should i do to fix this and i want it to check every name in the list, Thanks in advance :).
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
string nurse[4];
int i, n=3, j, k, num;
int main()
{
cout << "Please Enter names to add to the list --->";
for (i = 0; i <= n; i++)
{
cin >> nurse[i];
}
for (int i = 0; i < n; i++)
{
for (j = i + 1; j < n;)
if (nurse[j] == nurse[i])
{
for (k = j; k < n; k++)
{
nurse[k] = nurse[k + 1];
}
n--;
}
else
{
j++;
}
}
cout << "Printing list after removing duplicated names" << endl;
for (i = 0; i <= n; i++)
cout << " "<<nurse[i] << endl;
system("pause");
return 0;
}
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// ...
vector<string> nurses;
nurses.push_back("Hana");
nurses.push_back("Alex");
nurses.push_back("Hana");
nurses.push_back("Alex");
sort(nurses.begin(), nurses.end());
const vector<string>::iterator it = unique(nurses.begin(), nurses.end());
nurses.erase(it, nurses.end());
Say you start with
vector<string> v{"alex", "emma", "alex"};
Define an unordered_set with these items:
unordered_set<string> h;
Now use the erase-remove idiom using
[&h](const string &s){return !h.insert(s).second;};
This will do the job in (expected) linear time.
Full example:
#include <string>
#include<vector>
#include <unordered_set>
#include <algorithm>
using namespace std;
int main()
{
vector<string> v{"alex", "emma", "alex"};
unordered_set<string> h;
auto r = [&h](const string &s){return !h.insert(s).second;};
v.erase(remove_if(begin(v), end(v), r), end(v));
}
You condition in for is one less than the count of the elements, so the last element won't be checked at all.
for (int i = 0; i <= n; i++)
~
{
for (j = i + 1; j <= n;)
~
if (nurse[j] == nurse[i])
{
for (k = j; k <= n; k++)
~
{
nurse[k] = nurse[k + 1];
}
n--;
}
else
{
j++;
}
}
You are effectively not removing names from your array. You are just shifting them in case same name comes!
You could actually use a std::set which will automatically do this for you!
std::set< std::string > nurses;
std::string nurse;
for (i = 0; i <= n; i++)
{
std::cin >> nurse;
nurses.insert( nurse );
}
Do not forget to include <set> in your code.

How to solve Project 12.15 in Walter Savitch Absolute C++ 5th Ed

I have been working the Project 15 in Chapter 12 of Walter Savitch Absolute C++ (5th ed.) for a long time. The problem is at the bottom, which you can check if you are interested in my problem.
I figure out one way to solve this problem which is to read the file containing a paragraph many times and at each time, locate the keyword, the line number and the context. But it sounds to me very tedious. So I tried to read the file only twice. At the first time reading it, I locate all the keywords and line numbers. At the second time reading the file, I determine the context. But I failed.
I do not attempt to fix bugs in my code (
// header file for project 15
#ifndef PROJECT15_H
#define PROJECT15_H
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
#include<cstring>
#include<iomanip>
#include<vector>
using std::ios;
using std::cout;
using std::endl;
using std::setw;
using std::string;
using std::istream;
using std::ostream;
using std::ifstream;
using std::ofstream;
using std::stringstream;
const int MAX_CHARACTERS = 72;
const int N_CHARACTERS_BEFORE = 2;
const int N_CHARACTERS_AFTER = 1;
namespace
{
void sortKWIX(string** list, int size);
void advanceChain(string chain[], string newStr);
void printKWIX(string** list, int size, ostream& outStream);
} // namespace
namespace project15
{
void getKWIX(string keyWords[], int size, string file, ostream& outStream);
} //namespace project15
#endif // PROJECT15_H
/******************************************************************************//******************************************************************************//******************************************************************************/
// function definitions for project 15
#include"Project15.h"
namespace
{
void sortKWIX(string** list, int size)
{
string tmp;
for (int outer = 0; outer < size - 1; outer++)
for (int inner = outer + 1; inner < size; inner++)
if (strcmp(list[outer][0].c_str(), list[inner][0].c_str()) > 0)
{
for (int index = 0; index < 3; index++)
{
tmp = list[outer][index];
list[outer][index] = list[inner][index];
list[inner][index] = tmp;
}
}
}
void advanceChain(string chain[], string newStr)
{
for (int index = 0; index < N_CHARACTERS_AFTER + N_CHARACTERS_BEFORE; index++)
chain[index] = chain[index + 1];
chain[N_CHARACTERS_AFTER + N_CHARACTERS_BEFORE] = newStr;
}
void printKWIX(string** list, int size, ostream& outStream)
{
outStream.setf(ios::left);
outStream.width(12);
outStream << "KWIX Listing:\n"
<< "Keyword " << "Line Number " << "Keyword in Context\n";
for (int index = 0; index < size; index++)
{
for (int ncol = 0; ncol < 3; ncol++)
outStream << list[index][ncol] << " ";
outStream << endl;
}
}
} // namespace
namespace project15
{
void getKWIX(string keyWords[], int size, string file, ostream& outStream)
{
using namespace std;
ifstream fin(file.c_str());
if (fin.fail())
{
cout << "Input file openning failed.\n";
exit(1);
}
string inp;
char next;
int nChars = 0;
int nline = 0;
int position = 0;
vector<string> keys;
vector<int> positions;
vector<int> nlines;
while (fin >> inp)
{
position++;
if (!fin.eof())
fin.get(next);
else
next = '\0';
// determine line number
if ((nChars + inp.length() > MAX_CHARACTERS) || (next == '\n'))
{
nline++;
nChars = inp.length();
}
else
nChars += inp.length();
if (next != '\n')
nChars++;
if (!fin.eof() && (fin.peek() == '\n'))
nline++;
// determine whether inp is a keyword
// first, determine if the last character in inp is not an alphabet
if (!(isalpha(inp[inp.length() - 1])))
inp = inp.substr(0, inp.length() - 1);
for (int index = 0; index < size;index++)
if (inp == keyWords[index])
{
keys.push_back(inp);
positions.push_back(position);
nlines.push_back(nline);
break;
}
}
fin.close();
// open the file for the 2nd time to get contexts
fin.open(file.c_str());
if (fin.fail())
{
cout << "Input file openning failed.\n";
exit(1);
}
position = 0;
int index = 0;
vector<string> contexts;
string tmp[N_CHARACTERS_AFTER + N_CHARACTERS_BEFORE + 1];
for (int iter = 0; iter < N_CHARACTERS_AFTER + N_CHARACTERS_BEFORE + 1; iter++)
tmp[iter] = "";
while (fin >> inp)
{
//position++;
advanceChain(tmp, inp);
if (!fin.eof())
fin.get(next);
else
next = '\0';
// determine where to start to save contexts
if (positions[index] - position <= N_CHARACTERS_BEFORE)
{
// determine where to start to save strings
string str = "";
if (positions[index] - N_CHARACTERS_BEFORE < 0)
{
for (int iter = N_CHARACTERS_AFTER + N_CHARACTERS_BEFORE - position; iter < N_CHARACTERS_AFTER + N_CHARACTERS_BEFORE + 1; iter++)
str += tmp[iter];
position++;
}
else
{
for (int iter = N_CHARACTERS_AFTER - position + positions[index]; iter < N_CHARACTERS_AFTER + N_CHARACTERS_BEFORE + 1; iter++)
str += tmp[iter];
position++;
}
for (int iter = 0; iter < N_CHARACTERS_AFTER; iter++)
{
if (!fin.eof())
{
fin >> inp;
advanceChain(tmp, inp);
str += inp;
position++;
}
}
contexts.push_back(str);
index++;
}
}
fin.close();
// form a new list
string** list = new string*[keys.size()];
for (int i = 0; i < keys.size(); i++)
list[i] = new string[3];
stringstream ss;
for (int nrow = 0; nrow < keys.size(); nrow++)
{
list[nrow][0] = keys[nrow];
ss.str("");
ss << nlines[nrow];
list[nrow][1] = ss.str();
list[nrow][2] = contexts[nrow];
}
sortKWIX(list, keys.size());
printKWIX(list, keys.size(), outStream);
}
} //namespace project15
) right now, but I want to hear from you what kind of method you would like to use to solve this problem. Would you give me some hint?
===================================================================== Problem:
In this program you are to process text to create a KWIX table (Key Word In conteXt table). The idea is to produce a list of keywords (not programming language keywords, rather words that have important technical meaning in a discussion),then for each instance of each keyword, place the keyword, the line number of the context, and the keyword in its context in the table. There may be more than one context for a given keyword. The sequence of entries within a keyword is to be the order of occurrence in the text. For this problem, “context” is a user-selected number of words before the keyword, the keyword itself, and a user-selected number of words after the keyword. The table has an alphabetized column of keywords followed by a line number(s) where the keyword occurs, followed by a column of all contexts within which the keyword occurs. See the following example.
Hints: To get your list of keywords, you should choose and type in several paragraphs from the text, then omit from your paragraph “boring” words such as forms of the verb “to be”; pronouns such as I, me, he, she, her, you, us, them, who, which, etc. Finally, sort the keyword list and remove duplicates. The better job you do at this, the more useful output you will get.
Example: A paragraph and its KWIX Listing:
There are at least two complications when reading and writing with random access via an fstream : (1) You normally work in bytes using the typechar or arrays of char and need to handle type conversions on your own, and (2) you typically need to position a pointer (indicating where the read or write begins) before each read or write.
KWIX Listing:
Keyword Line Number Keyword in Context
access 2 with random access via
arrays 3 char or arrays of
bytes 2 work in bytes using char 3 the type
char or
char 3 array of char and
conversions 3 handle type conversions on
The table is longer than these sample entries.

C++ find words with first and last same letter and sort them in alphabetical order

i can't make program that reads from file words ( there is no limit of words or length to them ) with same first letter and last. I use class and object and first of all i can't make reading them
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
class Texts{
public:
void Realding(string &All);
void Searching(string &text, char *Word);
};
int main()
{
Texts A;
string Text; char word[40];
A.Reading(Text);
A.Searching(Text, word);
system("PAUSE");
}
void Texts::Reading(string &All)
{
string temp;
ifstream read("Text.txt");
while (getline(read, temp)) { All += temp; All += "\n"; }
cout << All;
}
void Texts::Searching(string &text, char *Word)
{
int i = 0;
int j = 0;
int letters = 0;
int zodz = 0;
int index = 0;
while (1)
{
if (text[i] == ' ' || text[i] == '\0')
{
zodz++;
if (text[0] == text[i - 1])
{
letters = j;
for (int l = 0; l < letters; l++)
{
//cout << text[l];
}
j = 0;
}
if (text[i + 1 - j] == text[i - 1])
{
letters = j;
for (int l = i - j; l < letters; l++)
{
// cout << text[l];
}
j = 0;
}
}
if (text[i] == '\0') break;
else
i++;
j++;
}
}
I can't make it properly read from file... Text.txt looks like
asdfa su egze hah ktis faf
And how later when selected words with first and last same letter to assign to array, that later to sort them in alphabetical order. Thanks if someone helps me.
Reading from file:
std::ifstream in(NameOfFile);
std::string word;
while (in >> word) //will stop when word can't be read. probably bad file or end of file
{
// do something with word
}
Find word with same first and last letter
if (word.front() == word.back())
{
// do something with word
}
Note this does not handle words with capitalized letters. It would not find "Mom". It will likely crash over empty words. Both have trivial fixes.
Assign word to array
array[index++] == word;
This assumes you want to advance the index after inserting into the array. Please note the program will behave poorly if the array is overfilled. If you are allowed to by the assignment, please consider using a std::vector.
Sorting the array
std::sort(array, array + index);
This assumes you are allowed to use std::sort. Again, if possible use a std::vector in place of the array. index is assumed to be the value of index from the adding example above after all of the adding has been done. If you are NOT allowed to use std::sort ask another question. It's a lengthy topic.

VC++ Runtime Error : Debug Assertation Failed

Currently I am getting an runtime "assertation error"
Here is the error:
I'm reading words from a text file into dynamically allocated arrays.
this block of code is where I am filling the new arrays.
I know the problem is being caused by this block of code and something about my logic is off just can't see what it is.
//fill new arrays
for( int y = 0; y < new_numwords; y++)
{
for( int i = 0; i < NUM_WORDS; i++)
{
if (!strcmp(SentenceArry[i], EMPTY[0]) == 0)
{
New_SentenceArry[y] = SentenceArry[i];
New_WordCount[y] = WordCount[i];
y++;
}
}
}
Also how would I pass this dynamically allocated 2D array to a function? (the code really needs to be cleaned up as a whole)
char** SentenceArry = new char*[NUM_WORDS]; //declare pointer for the sentence
for( int i = 0; i < NUM_WORDS; i++)
{
SentenceArry[i] = new char[WORD_LENGTH];
}
Here is the full extent of the code.. help would be much appreciated!
Here is what is being read in:
and the current output (the output is how it's suppose to be ):
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <cstring>
#include <cctype>
#include <iomanip>
using std::setw;
using std::left;
using std::cout;
using std::cin;
using std::endl;
using std::ifstream;
int main()
{
const int NUM_WORDS = 17;//constant for the elements of arrays
const int WORD_LENGTH = 50;//constant for the length of the cstrings (NEED TO GIVE THE VALUE ZERO STILL!)
short word_entry = 0; //declare counter
short new_numwords= 0; //declare new word count
char EMPTY[1][4]; //NULL ARRAY
EMPTY[0][0] = '\0';//define it as null
char** SentenceArry = new char*[NUM_WORDS]; //declare pointer for the sentence
for( int i = 0; i < NUM_WORDS; i++)
{
SentenceArry[i] = new char[WORD_LENGTH];
}
int WordCount[NUM_WORDS];//declare integer array for the word counter
for(int i = 0; i < NUM_WORDS; i++)//fill int array
{
WordCount[i] = 1;
}
int New_WordCount[NUM_WORDS] = {0};
ifstream read_text("DataFile.txt"); //read in our text file
if (read_text.is_open()) //check if the the file was opened
{
read_text >> SentenceArry[word_entry];
//REMOVE PUNCTUATION BEFORE BEING READ INTO THE ARRAY
while (!read_text.eof())
{
word_entry++; //increment counter
read_text >> SentenceArry[word_entry]; //read in single words of the text file into the array SentenceArry
char* ptr_ch;//declare our pointer that will find chars
ptr_ch = strstr( SentenceArry[word_entry], ",");//look for "," within the array
if (ptr_ch != NULL)//if true replace it with a null character
{
strncpy( ptr_ch, "\0" , 1);
}//end if
else
{
ptr_ch = strstr( SentenceArry[word_entry], ".");//look for "." within the array
if (ptr_ch != NULL)//if true replace it with a null character
{
strncpy( ptr_ch, "\0" , 1);
}//end if
}//end else
} //end while
}//end if
else
{
cout << "The file could not be opened!" << endl;//display error message if file doesn't open
}//end else
read_text.close(); //close the text file after eof
//WORD COUNT NESTED FOR LOOP
for(int y = 0; y < NUM_WORDS; y++)
{
for(int i = y+1; i < NUM_WORDS; i++)
{
if (strcmp(SentenceArry[y], EMPTY[0]) == 0)//check if the arrays match
{
y++;
}
else
{
if (strcmp(SentenceArry[y], SentenceArry[i]) == 0)//check if the arrays match
{
WordCount[y]++;
strncpy(SentenceArry[i], "\0" , 3);
}//end if
}//end if
}//end for
}//end for
//find how many arrays still contain chars
for(int i = 0; i < NUM_WORDS; i++)
{
if (!strcmp(SentenceArry[i], EMPTY[0]) == 0)
{
new_numwords++;
}
}
//new dynamic array
char** New_SentenceArry = new char*[new_numwords]; //declare pointer for the sentence
for( int i = 0; i < new_numwords; i++)
{
New_SentenceArry[i] = new char[new_numwords];
}
//fill new arrays
for( int y = 0; y < new_numwords; y++)
{
for( int i = 0; i < NUM_WORDS; i++)
{
if (!strcmp(SentenceArry[i], EMPTY[0]) == 0)
{
New_SentenceArry[y] = SentenceArry[i];
New_WordCount[y] = WordCount[i];
y++;
}
}
}
//DISPLAY REPORT
cout << left << setw(15) << "Words" << left << setw(9) << "Frequency" << endl;
for(int i = 0; i < new_numwords; i++) //compare i to the array constant NUM_WORDS
{
cout << left << setw(15) << New_SentenceArry[i] << left << setw(9) << New_WordCount[i] << endl; //display the contents of the array SentenceArry
}
//DEALLOCATION
for( int i = 0; i < NUM_WORDS; i++)//deallocate the words inside the arrays
{
delete [] SentenceArry[i];
}
for(int i = 0; i < new_numwords; i++)
{
delete [] New_SentenceArry[i];
}
delete [] SentenceArry; //deallocate the memory allocation made for the array SentenceArry
delete [] New_SentenceArry;//deallocate the memory allocation made for the array New_SentenceArry
}//end main
There are several issues with the code, not withstanding that this could be written using C++, not C with a sprinkling of C++ I/O..
Issue 1:
Since you're using c-style strings, any copying of string data will require function calls such as strcpy(), strncpy(), etc. You failed in following this advice in this code:
for( int y = 0; y < new_numwords; y++)
{
for( int i = 0; i < NUM_WORDS; i++)
{
if (!strcmp(SentenceArry[i], EMPTY[0]) == 0)
{
New_SentenceArry[y] = SentenceArry[i]; // This is wrong
New_WordCount[y] = WordCount[i];
y++;
}
}
}
You should be using strcpy(), not = to copy strings.
strcpy(New_SentenceArry[y], SentenceArry[i]);
Issue 2:
You should allocate WORD_LENGTH for both the original and new arrays. The length of the strings is independent of the number of strings.
char** New_SentenceArry = new char*[new_numwords]; //declare pointer for the sentence
for( int i = 0; i < new_numwords; i++)
{
New_SentenceArry[i] = new char[new_numwords];
}
This should be:
char** New_SentenceArry = new char*[new_numwords]; //declare pointer for the sentence
for( int i = 0; i < new_numwords; i++)
{
New_SentenceArry[i] = new char[WORD_LENGTH];
}
Issue 3:
Your loops do not check to see if the index is going out of bounds of your arrays.
It seems that you coded your program in accordance to the data that you're currently using, instead of writing code regardless of what the data will be. If you have limited yourself to 17 words, where is the check to see if the index goes above 16? Nowhere.
For example:
while (!read_text.eof() )
Should be:
while (!read_text.eof() && word_entry < NUM_WORDS)
Issue 4:
You don't process the first string found correctly:
read_text >> SentenceArry[word_entry]; // Here you read in the first word
while (!read_text.eof() )
{
word_entry++; //increment counter
read_text >> SentenceArry[word_entry]; // What about the first word you read in?
Summary:
Even with these changes, I can't guarantee that the program won't crash. Even it it doesn't crash with these changes, I can't guarantee it will work 100% of the time -- a guarantee would require further analysis.
The proper C++ solution, given what this assignment was about, is to use a std::map<std::string, int> to keep the word frequency. The map would automatically store similar words in one entry (given that you remove the junk from the word), and would bump up the count to 1 automatically, when the entry is inserted into the map.
Something like this:
#include <string>
#include <map>
#include <algorithm>
typedef std::map<std::string, int> StringMap;
using namespace std;
bool isCharacterGarbage(char ch)
{ return ch == ',' || ch == '.'; }
int main()
{
StringMap sentenceMap;
//...
std::string temp;
read_text >> temp;
temp.erase(std::remove_if(temp.begin(), temp.end(), isCharacterGarbage),temp.end());
sentenceMap[temp]++;
//...
}
That code alone does everything your original code did -- keep track of the strings, bumps up the word count, removes the junk characters from the word before being processed, etc. But best of all, no manual memory management. No calls to new[], delete[], nothing. The code just "works". That is effectively 5 lines of code that you would just need to write a "read" loop around.
I won't go through every detail, you can do that for yourself since the code is small, and there are vast amounts of resources available explaining std::map, remove_if(), etc.
Then printing out is merely going through the map and printing each entry (string and count). If you add the printing, that may be 4 lines of extra code. So in all, practically all of the assignment is done with effectively 10 or so lines of code.
Remove below code.
for(int i = 0; i < new_numwords; i++)
{
delete [] New_SentenceArry[i];
}

Max Repeated Word in a string

I was trying to do a very common interview problem "Finding the max repeated word in a string " and could not find much resources in net for c/c++ implementation. So I coded it myself here.
I have tried to do most of the coding from scratch for better understanding.
Could you review my code and provide comments on my algorithm. Some people have suggested using hashtables for storing the count, but am not using hashtables here.
#include<stdafx.h>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<iostream>
using namespace std;
string word[10];
//splitting string into words
int parsestr(string str)
{
int index = 0;
int i = 0;
int maxlength = str.length();
int wordcnt = 0;
while(i < maxlength)
{
if(str[i]!= ' ')
{
word[index] = word[index]+str[i];
}
else
{
index++;//new word
wordcnt = index;
}
i++;
}
return wordcnt;
}
//find the max word count out of the array and return the word corresponding to that index.
string maxrepeatedWord(int wordcntArr[],int count)
{
int max = 0;
int index = 0;
for(int i=0;i<=count;i++)
{
if(wordcntArr[i] > max)
{
max = wordcntArr[i];
index = i;
}
}
return word[index];
}
void countwords(int count)
{
int wordcnt = 0;
int wordcntArr[10];
string maxrepeatedword;
for(int i=0;i<=count ;i++)
{
for(int j=0;j<=count;j++)
{
if(word[i]==word[j])
{
wordcnt++;
//word[j] = "";
}
else
{}
}
cout<<" word "<< word[i] <<" occurs "<< wordcnt <<" times "<<endl;
wordcntArr[i] = wordcnt;
wordcnt = 0;
}
maxrepeatedword = maxrepeatedWord(wordcntArr,count);
cout<< " Max Repeated Word is " << maxrepeatedword;
}
int main()
{
string str = "I am am am good good";
int wordcount = 0;
wordcount = parsestr(str);
countwords(wordcount);
}
Just for the sake of comparison, the most obvious way to do this is C++ is:
#include <map>
#include <string>
#include <iostream>
#include <sstream>
int main()
{
std::istringstream input("I am am am good good");
std::map<std::string, int> count;
std::string word;
decltype(count)::const_iterator most_common;
while (input >> word)
{
auto iterator = count.emplace(word, 0).first;
++iterator->second;
if (count.size() == 1 ||
iterator->second > most_common->second)
most_common = iterator;
}
std::cout << '\"' << most_common->first << "' repeated "
<< most_common->second << " times\n";
}
See it run here.
Notes:
map::emplace returns a pair<iterator,bool> indicating where the word & its count are in the map, and whether its newly inserted. We only care about where so capture emplace(...).first.
As we update the count, we check if that makes the word the most-common word seen so far. If so we copy the iterator to the local variable most_common, so we have a record of both the most commonly seen word so far and its count.
Some things you're doing that are worth thinking about:
word is a global variable - it's a good habit to pass things as function arguments unless it's terribly inconvenient, means the code can be reused more easily from async signal handlers or other threads, and it's more obvious in looking at a function call site what the inputs and outputs might be. As is, the call countwords(wordcount) makes it look like countwords' only input is the int wordcount.
fixed sized arrays: if you've more than 10 words, you're sunk. C++ Standard containers can grow on demand.
there are a few convenience functions you could use, such as std::string::operator+=(char) to append a char more concisely ala my_string += my_char;
Generally though, your code is quite sensible and shows a good understanding of iteration and problem solving, doing it all very low-level but that's good stuff to understand in a very hands-on way.
Code Snippet :
void mostRepeat(string words[], int n)
{
int hash[n]={0};
for(int j=0; j<n; j++)
{
for(int i=0; i<n; i++)
{
if(words[j]==words[i]) hash[j]++;
}
}
int maxi = hash[0];
int index = 0;
for(int i=0; i<n; i++)
{
if(maxi<hash[i])
{
maxi=hash[i];
index = i;
}
}
cout<<words[index]<<endl;
}
Full program : Link
import java.util.*;
public class StringWordDuplicates {
static void duplicate(String inputString){
HashMap<String, Integer> wordCount = new HashMap<String,Integer>();
String[] words = inputString.split(" ");
for(String word : words){
if(wordCount.containsKey(word)){
wordCount.put(word, wordCount.get(word)+1);
}
else{
wordCount.put(word, 1);
}
}
//Extracting of all keys of word count
Set<String> wordsInString = wordCount.keySet();
for(String word : wordsInString){
if(wordCount.get(word)>1){
System.out.println(word+":"+wordCount.get(word));
}
}
}
public static void main(String args[]){
duplicate("I am Java Programmer and IT Server Programmer with Java as Best Java lover");
}
}