C++ C++;,计算字符串中重复的单词并显示
我让字符串说“走路说话,不能说话。”我想数一数重复的单词并显示出来。 注意:它不区分大小写 我用过度数表C++ C++;,计算字符串中重复的单词并显示,c++,arrays,string,C++,Arrays,String,我让字符串说“走路说话,不能说话。”我想数一数重复的单词并显示出来。 注意:它不区分大小写 我用过度数表 strtok(string, ",.;:\"!? -_\n\t*()@#=+"); 并保存在 char *temp[100]; 现在我如何检查单词的重复?并显示如下 3 won't 2 talk 1 can't 1 walk 它应该从最高重复到最低显示。如果重复相同,则按字母顺序显示 对不起,我的英语不好。使用std::string保存strtok()的结果。。然后创建一个std::m
strtok(string, ",.;:\"!? -_\n\t*()@#=+");
并保存在
char *temp[100];
现在我如何检查单词的重复?并显示如下
3 won't
2 talk
1 can't
1 walk
它应该从最高重复到最低显示。如果重复相同,则按字母顺序显示
对不起,我的英语不好。使用std::string保存
strtok()的结果。
。然后创建一个std::map
来保存字符串(键)出现的次数
您可以使用以下内容填充地图:
std::map<string, int> myMap;
myMap[tokenizedWord]++; //Increase count of word.
您也可以在地图上执行标准的插入和定位操作,但关联数组语法更简单,何必麻烦呢?:)
PS:为了完全回答您的评论,以防万一,myMap[tokenizedWord]++末尾的++只是说将为该键存储的整数值的值增加1。您也可以使用myMap[tokenizedWord]=myMap[tokenizedWord]+1,也可以使用myMap[tokenizedWord]+=1。问题的完整实现(如果需要排序的示例代码,请告诉我):
#包括
#包括
#包括
#包括
#定义数组元素计数(A)sizeof(A)/sizeof(*A)
typedef struct\u word\t
{
字符*字;
内占用计数;
结构字下一步;
}单词t;
类型定义结构\u单词\u列表\u t
{
结构字头;
结构字尾;
元素计数;
}单词列表;
/*创建单词列表*/
单词列表*制作列表(无效)
{
单词列表w列表=(单词列表)malloc(sizeof(struct单词列表));
if(w_list==NULL)
{
fprintf(stderr,“malloc faild-->%s\n”,strerror(errno));
返回NULL;
}
w_list->head=w_list->tail=NULL;
w_列表->元素计数=0;
返回w_列表;
}
int list\u word\u查找(word\u list\u t*w\u list,char*word)
{
word\u t*temp\u word=w\u list->head;
while(临时单词)
{
如果(strcmp(临时字->字,字)==0)
{
/*我们以前拿到过,增加计数*/
临时单词->发生计数++;
返回1;
}
其他的
{
临时单词=临时单词->下一步;
}
}
返回0;
}
/*如果新词不存在,则将其添加到单词列表中,否则增加其出现次数*/
/*TODO:使用合并排序对列表进行排序以提高性能*/
int向列表中添加(单词列表,字符*单词)
{
int return_status=0;
char*tmp_word=(char*)malloc(sizeof(char)*(strlen(word)+1));
word_t*new_word=(word_t*)malloc(sizeof(struct_word t));
/*空列表*/
如果(w_list->head==NULL)
{
strcpy(tmp_单词,单词);
新单词->单词=tmp单词;
新单词->占用者计数=1;
w_列表->head=w_列表->tail=new_单词;
w_list->head->next=NULL;
w_list->elems_count++;
}
其他的
{
/*列表不是空的*/
/*检查列表中是否存在该单词*/
返回\状态=列表\单词\查找(w\列表,单词);
如果(返回_状态==1)
{
fprintf(stdout,“我们在-->递增计数\n”之前得到这个字);
}
其他的
{
strcpy(tmp_单词,单词);
新单词->单词=tmp单词;
新单词->占用者计数=1;
w_list->tail->next=新单词;
w_list->tail=新单词;
w_list->tail->next=NULL;
}
}
返回0;
}
无效单词列表转储(单词列表w列表)
{
单词*temp;
用于(临时=工作列表->标题;临时;临时=临时->下一步){
fprintf(标准输出,“字:%s——计数=%d\n”,临时->字,临时->占用计数);
}
}
/*毁于一旦*/
无效自由字(字列表\u t*w列表)
{
单词*temp;
用于(临时=工作列表->标题;临时;临时=临时->下一步){
/*释放字串*/
免费(临时->文字);
/*释放单词*/
免费(临时);
}
w_list->head=NULL;
w_list->tail=NULL;
}
/*销毁单词表*/
无效自由词列表(词列表w列表)
{
如果(!w_列表)
{
返回;
}
自由词(w_列表);
免费(w_列表);
}
/*TODO:创建一个函数,将输入文本转换为字符**数组,以便将其传递给添加到列表*/
/*用于测试*/
int main(int argc,字符**argv)
{
const char*string[]={“Hello”、“World”、“Stackoverflow”、“C”、“Hello”、“C”、“WORDS”、“WORDS”、“List”、“List”、“Hello”、“World”、“Count”};
单词列表*我的列表=制作列表();
int i;
对于(i=0;i<数组元素计数(字符串);i++)
将_添加到_列表(我的_列表,字符串[i]);
单词列表转储(我的列表);
免费单词列表(我的列表);
返回0;
}
这里是一个使用strtok
但不使用std::map
的答案。在字符串的一次传递中,将对照前面的单词检查中的每个单词,并计算重复次数
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <string>
using std::string;
#include <vector>
using std::vector;
#include <cstring>
using std::tolower;
int main()
{
char *strin;
string inputstr;
vector<string> svec;
vector<int> cvec;
char *pch;
int unique_word_count=0;
while(getline(cin,inputstr))
{
//token-ize the string
//First string
strin = &inputstr[0];
pch = std::strtok(strin," ,-");
bool unique_word_found = true;
//subsequent words
while (pch != NULL)
{
string word(pch);
for(string::size_type i=0; i < word.size(); i++)
word[i]=tolower(word[i]);
//first word
//just add to svec and no comparisons
if(unique_word_count==0)
{
svec.push_back(word);
cvec.push_back(1);
cvec[unique_word_count++]=1; //init count of first word
//next word
pch = std::strtok(NULL, " ,-");
unique_word_found = true; //reset flag
continue;
}
//start comparing with other words currently in string vector
//do not do this if only 1 word present
vector<string>::iterator iter=svec.begin();
while(iter < svec.end())
{
if(word == *iter)
{
//match found
cvec[iter-svec.begin()]++; //increment count of that word
unique_word_found = false;
}
iter++;
}
if(unique_word_found)
{
//add to unique word list and increment count
svec.push_back(word);
cvec.push_back(1);
cvec[unique_word_count++]=1;
}
//next word
pch = std::strtok(NULL, " ,-");
unique_word_found = true; //reset flag
}
}
cout << "Word" << " ---> " << "Occurences" << endl;
for(vector<string>::size_type i=0; i < svec.size(); i++)
{
cout << svec[i] << " ---> " << cvec[i] << endl;
}
return 0;
}
#包括
使用std::cin;
使用std::cout;
使用std::endl;
#包括
使用std::string;
#包括
使用std::vector;
#包括
使用std::tolower;
int main()
{
char*strin;
字符串输入str;
向量svec;
载体cvec;
char*pch;
int unique_word_count=0;
while(getline(cin,inputstr))
{
//标记字符串的大小
//第一串
strin=&inpu
//Create a map and insert a couple things into it - prices of meat?
std::map<string, float> myMap;
myMap["Chicken"] = 4.99;
myMap["Turkey"] = 6.99;
//Retrieve the price of something using the key.
std::cout << "Chicken costs " << myMap["Chicken"] << std::end;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define ARRAY_ELEMS_COUNT(A) sizeof(A)/sizeof(*A)
typedef struct _word_t
{
char *word;
int occurr_count;
struct _word_t *next;
} word_t;
typedef struct _word_list_t
{
struct _word_t *head;
struct _word_t *tail;
int elems_count;
} word_list_t;
/* Creation of the words list */
word_list_t *make_list(void)
{
word_list_t *w_list = (word_list_t *)malloc(sizeof (struct _word_list_t));
if (w_list == NULL)
{
fprintf(stderr, "malloc faild --> %s\n", strerror(errno));
return NULL;
}
w_list->head = w_list->tail = NULL;
w_list->elems_count = 0;
return w_list;
}
int list_word_lookup(word_list_t *w_list, char *word)
{
word_t *temp_word = w_list->head;
while(temp_word)
{
if (strcmp(temp_word->word, word) == 0)
{
/* We got it before, increment the count */
temp_word->occurr_count++;
return 1;
}
else
{
temp_word = temp_word->next;
}
}
return 0;
}
/* Adding new words to the list of words if they are not present, otherwise increment their occurrence count */
/* TODO : Sort the list using Merge sort for performance */
int adding_to_list(word_list_t *w_list, char *word)
{
int return_status = 0;
char *tmp_word = (char *)malloc(sizeof(char)*(strlen(word) + 1));
word_t *new_word = (word_t *)malloc(sizeof(struct _word_t));
/* Empty list */
if (w_list->head == NULL)
{
strcpy(tmp_word, word);
new_word->word = tmp_word;
new_word->occurr_count = 1;
w_list->head = w_list->tail = new_word;
w_list->head->next = NULL;
w_list->elems_count++;
}
else
{
/* The list is not empty */
/* Checking if the word exist in the list */
return_status = list_word_lookup(w_list, word);
if (return_status == 1)
{
fprintf(stdout, "WE got this word before --> increment count\n");
}
else
{
strcpy(tmp_word, word);
new_word->word = tmp_word;
new_word->occurr_count = 1;
w_list->tail->next = new_word;
w_list->tail = new_word;
w_list->tail->next = NULL;
}
}
return 0;
}
void words_list_dump(word_list_t *w_list)
{
word_t *temp;
for (temp = w_list->head; temp; temp = temp->next) {
fprintf(stdout, "Word : %s -- Count = %d\n", temp->word, temp->occurr_count);
}
}
/* Destroying all words */
void free_words(word_list_t *w_list)
{
word_t *temp;
for (temp = w_list->head; temp; temp = temp->next) {
/* Freeing the word string */
free(temp->word);
/* Freeing the word */
free(temp);
}
w_list->head = NULL;
w_list->tail = NULL;
}
/* Destroying the words list */
void free_words_list(word_list_t *w_list)
{
if (!w_list)
{
return;
}
free_words(w_list);
free(w_list);
}
/* TODO : create a function that converts your input text to a char ** array, so you can pass it to adding_to_list */
/* For testing */
int main(int argc, char **argv)
{
const char *string[] = {"Hello", "World", "Stackoverflow", "C", "Hello", "C", "WORDS", "words", "List", "list", "Hello", "World", "Count"};
word_list_t *my_list = make_list();
int i;
for (i = 0; i < ARRAY_ELEMS_COUNT(string); i++)
adding_to_list(my_list, string[i]);
words_list_dump(my_list);
free_words_list(my_list);
return 0;
}
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <string>
using std::string;
#include <vector>
using std::vector;
#include <cstring>
using std::tolower;
int main()
{
char *strin;
string inputstr;
vector<string> svec;
vector<int> cvec;
char *pch;
int unique_word_count=0;
while(getline(cin,inputstr))
{
//token-ize the string
//First string
strin = &inputstr[0];
pch = std::strtok(strin," ,-");
bool unique_word_found = true;
//subsequent words
while (pch != NULL)
{
string word(pch);
for(string::size_type i=0; i < word.size(); i++)
word[i]=tolower(word[i]);
//first word
//just add to svec and no comparisons
if(unique_word_count==0)
{
svec.push_back(word);
cvec.push_back(1);
cvec[unique_word_count++]=1; //init count of first word
//next word
pch = std::strtok(NULL, " ,-");
unique_word_found = true; //reset flag
continue;
}
//start comparing with other words currently in string vector
//do not do this if only 1 word present
vector<string>::iterator iter=svec.begin();
while(iter < svec.end())
{
if(word == *iter)
{
//match found
cvec[iter-svec.begin()]++; //increment count of that word
unique_word_found = false;
}
iter++;
}
if(unique_word_found)
{
//add to unique word list and increment count
svec.push_back(word);
cvec.push_back(1);
cvec[unique_word_count++]=1;
}
//next word
pch = std::strtok(NULL, " ,-");
unique_word_found = true; //reset flag
}
}
cout << "Word" << " ---> " << "Occurences" << endl;
for(vector<string>::size_type i=0; i < svec.size(); i++)
{
cout << svec[i] << " ---> " << cvec[i] << endl;
}
return 0;
}