用于打印的分段错误计算字符串中重复单词的数量。 [英] Segmentation error for printing counting the number of duplicate words in a string.

查看:106
本文介绍了用于打印的分段错误计算字符串中重复单词的数量。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在给定字符串中打印重复元素。以下是我的代码:



我收到分段错误。



测试用例=>如果输入是嘿这就是我,那么程序必须打印这个。



我尝试过: < br $>


 int main(){
char str [30];
int n,i;
cout<<输入大小;
cin>> n;
cout<<输入字词;
int a [30];
for(i = 0; i< n; i ++)
{
cin<< str [i];

}
for(i = 0; i< n; i ++)
{
a [str [i]] + = 1;
}
for(i = 0; i< n; i ++)
{
if(a [i]> 1)
{
cout< ;< STR [1];
}
}
}

解决方案

嗯...你确定要这个吗?

 cin<< str [i]; 



因为我怀疑你可能想要这个:

 cin>> str [i]; 



然后......这是错的:

 a [str [i]] + = 1; 

因为无论i的值如何,str [i]都是一个字符,这意味着空格字符将具有值为32,字母表比它高很多: ASCII表 [ ^ ]由于你的数组 a 只有30个元素,你可以输入的任何字符都不会产生良好的索引值 a



然后就是任务:即使完全整理出来,代码也不会接近 - 你需要做的是通过你的字符串,并识别每个单词开始和结束。然后,您可以使用该信息来识别重复的单词。

首先在纸上尝试,当您有一个好的算法时,您可以尝试将其转换为计算机可以理解的内容。目前,你在没有正确考虑工作的情况下急于进入代码 - 这对于比Hello World!更复杂的事情来说效果不佳。


嘿,男人,这是 C ++

  #include   <   string  >  
#include < iostream >
#include < sstream < span class =code-keyword>>
#include < span class =code-keyword>< set >
使用 命名空间标准;

int main()
{
string line;
cout<< 请输入句子<< ENDL;
getline(cin,line);

istringstream iss(line);
set< string> wordset;
字符串字;

while ((iss>> word))
{
if (wordset.find(word)== wordset.end())
wordset.insert(word);
else
cout<<单词<< ENDL; // 打印出重复的字词
}
}


I am trying to print the duplicate element in a given string. The following is my code:

I am getting segmentation error.

Test case=> if the input is "hey this this is me" then the program has to print "this".

What I have tried:

int main(){
	 char str[30];
	 int n,i;
	 cout<<"enter the size";
	 cin>>n;
	 cout<<"Enter the words";
	 int a[30];
	 for(i=0;i<n;i++)
	 	{
	 		cin<<str[i];

	 	}
	for(i=0;i<n;i++)
	{
		a[str[i]]+=1;
	}
	for(i=0;i<n;i++)
	{
		if(a[i]>1)
		{
			cout<<str[i];
		}
	}
}

解决方案

Um... Are you sure you wanted this?

cin<<str[i];


Because I suspect you might have wanted this:

cin>>str[i];


Then...this is wrong:

a[str[i]]+=1;

Because regardless of the value of i, str[i] will be a character, which means that the space character will have a value of 32, and alphabetics a lot higher than that: ASCII Table[^] Since your array a only has 30 elements, no character you can type will result in a good index value into a

Then there is the task: which even well and truly sorted out, that code isn't going to get close to - what you need to do is pass through your string, and identify each word start and end. You can then use that info to identify repeated words.
Try it on paper first, and when you have a good algorithm worked out, then you can try converting that to something a computer can understand. At the moment, you are rushing into code without properly thinking about the job first - and that just doesn't work well for anything more complex than "Hello World!"


Hey man, it's C++

#include <string>
#include <iostream>
#include <sstream>
#include <set>
using namespace std;

int main()
{
  string line;
  cout << "please, enter the sentence" << endl;
  getline(cin, line);

  istringstream iss(line);
  set<string> wordset;
  string word;

  while ((iss >> word))
  {
    if ( wordset.find(word) == wordset.end() )
      wordset.insert(word);
    else
      cout << word << endl; // print out the duplicate word
  }
}


这篇关于用于打印的分段错误计算字符串中重复单词的数量。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆