某事错了? [英] sth wrong?

查看:80
本文介绍了某事错了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个程序来从

输入文本中取出非字母字符...但是''ntext''总是比字母大4个字符

j ...为什么?

更好的方法吗?谢谢很多人。


void main()

{


int i,j = 0;

char text [9999];

char * ntext;

得到(文本);

for(i = 0 ; text [i]; i ++){

if(isalpha(text [i]))

j ++;

}

ntext = new char [j];

cout<< j<<" "<< strlen(ntext);

for(i = 0,j = 0; text [i]; i ++){

if(isalpha(text) [i])){

ntext [j] = text [i];

j ++;

}

}

strcpy(text,ntext);

cout<< endl;

for(i = 0; text [i]; i ++)

cout<< endl<< text [i];

delete [] ntext;

}

i''m trying to make a program to take out non alpha characters from
entered text...but ''ntext'' is always 4 char larger than the size
j...why is that?
any better ways to do this? thanks lots people.

void main()
{

int i, j=0;
char text[9999];
char *ntext;
gets(text);
for (i=0; text[i]; i++){
if(isalpha(text[i]))
j++;
}
ntext=new char [j];
cout<<j<<" "<<strlen(ntext);
for (i=0, j=0; text[i];i++){
if (isalpha(text[i])){
ntext[j]=text[i];
j++;
}
}
strcpy(text, ntext);
cout<<endl;
for (i=0; text[i]; i++)
cout<<endl<<text[i];
delete [] ntext;
}

推荐答案

Hello写道:
我正在尝试制作一个程序来从
输入文字...但是''ntext''总是大于4字符大小
j ...为什么会这样?
任何更好的方法来做到这一点?谢谢很多人。


这绝对可以做得更好。

摆脱c风格的字符串,你可以使用C ++字符串'(< string>)。

这看起来更像是C程序而不是C ++程序。

void main()


int i,j = 0;
char text [9999];


字符串文字;


char * ntext;
获取(文字);


使用istream(cin)的getline方法。


有这么多缓冲区溢出

感谢得到。不要使用它。

for(i = 0; text [i]; i ++){
if(isalpha(text [i]))
j ++;
}
ntext = new char [j];


好​​的 - 你正在分配一大块内存

重新开始。

cout<< j<<"" "<< strlen的(NTEXT);


马上,你试图找到

字符串的长度。

将此视为C程序,

这还不错。你在哪里

设置空字符(即strlen查找的

分隔符)。


for(i = 0,j = 0; text [i]; i ++){
if(isalpha(text [i])){
ntext [j] = text [i];
j ++;
}
}
strcpy(text,ntext);
i''m trying to make a program to take out non alpha characters from
entered text...but ''ntext'' is always 4 char larger than the size
j...why is that?
any better ways to do this? thanks lots people.
This can be definitely done better.
Get rid of c-style strings and you can use C++ string''s ( <string> ).
This seems more like a C program than a C++ program.

void main()
{

int i, j=0;
char text[9999];
string text;

char *ntext;
gets(text);
use getline method of istream ( cin ).

There had been so many buffer overflows
thanks to gets. Do not use it.
for (i=0; text[i]; i++){
if(isalpha(text[i]))
j++;
}
ntext=new char [j];
Ok - you are allocating a chunk of memory
afresh.
cout<<j<<" "<<strlen(ntext);
And right away, you are trying to find
the length of the string.
Considering this as a C program,
this is still bad. where are you
setting the null character (that is the
delimeter that strlen looks for ).

for (i=0, j=0; text[i];i++){
if (isalpha(text[i])){
ntext[j]=text[i];
j++;
}
}
strcpy(text, ntext);




问题依然存在。

您没有为ntext设置空字符

。并且你试图从ntext复制




考虑使用C ++字符串。你不会因为所有这些麻烦而感到尴尬。



The problem remains the same.
You are not setting the null character
for ntext. and you are trying to copy
from ntext.

Consider using C++ strings. You won''t
have all these troubles.


Hello写道:
void main()


没有''void main()''这样的东西!任何编译器接受

此代码没有诊断都是错误的。

得到(文本);


你应该*使用''gets()''!它是

安全问题的主要来源。问题是你不能保证你给''gets()''的数组足够大。

使用

''fgets()''或更好的''std :: getline()''(成员

函数''getline()' '有一个类似的问题虽然它可以设置一个最大宽度

:它很容易忘记

因此这个成员函数容易出错)。

ntext = new char [j];


这至少有一个字符太小:你需要空间来终止空字符



cout<< J<<" "<<< strlen(ntext);
for(i = 0,j = 0; text [i]; i ++){
if(isalpha(text [i])){
ntext [j] = text [i];
j ++;
}
}


....当然还有你需要在复制新字符串之前添加终止null

字符:

strcpy(text,ntext);
void main()
There is no such thing as ''void main()''! Any compiler accepting
this code without a diagnostic is in error.
gets(text);
You shall *NEVER* use ''gets()''! It is the primary source for
security problems. The issue is that you cannot guarantee that
the array you give to ''gets()'' is sufficiently large. Use
''fgets()'' or, even better, ''std::getline()'' instead (the member
function ''getline()'' has a similar problem although it is
possible to setup a maximum width: it is just easy to forget
and thus this member function is error prone).
ntext=new char [j];
This is at least one character too small: you need space for a
terminating null character.
cout<<j<<" "<<strlen(ntext);
for (i=0, j=0; text[i];i++){
if (isalpha(text[i])){
ntext[j]=text[i];
j++;
}
}
.... and, of course, you need to add the terminating null
character before copying the new string somewhere:
strcpy(text, ntext);




您可能想调查类''std :: string''和

算法''std :: remove()''。以下是使用C ++的

代码的道德等价物:


| #include< iostream>

| #include< string>

| #include< algorithm>

| #include< ctype.h>

|

| bool notalpha(char c)

| {

| return!std :: isalpha(static_cast< unsigned char>(c));

| }

|

| int main()

| {

| for(std :: string line; std :: getline(std :: cin,line);)

| {

| std :: string tmp(line);

| tmp.erase(std :: remove_if(tmp.begin(),tmp.end(),notalpha),

| tmp.end());

| std :: cout<< tmp<< " \ n";

| }

| }

-

< mailto:di *********** @ yahoo.com> < http://www.dietmar-kuehl.de/>

< http://www.contendix.com> - 软件开发&咨询



You might want to investigate the class ''std::string'' and the
algorithm ''std::remove()''. Here is the moral equivalent of your
code using C++:

| #include <iostream>
| #include <string>
| #include <algorithm>
| #include <ctype.h>
|
| bool notalpha(char c)
| {
| return !std::isalpha(static_cast<unsigned char>(c));
| }
|
| int main()
| {
| for (std::string line; std::getline(std::cin, line); )
| {
| std::string tmp(line);
| tmp.erase(std::remove_if(tmp.begin(), tmp.end(), notalpha),
| tmp.end());
| std::cout << tmp << "\n";
| }
| }
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting


" Dietmar Kuehl" <二*********** @ yahoo.com>在消息中写道

news:11 ********************** @ f14g2000cwb.googlegr oups.com ...
"Dietmar Kuehl" <di***********@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
| #include< iostream>
| #include< string>
| #include< algorithm>
| #include< ctype.h>
|
| bool notalpha(char c)
| {
| return!std :: isalpha(static_cast< unsigned char>(c));
| }
|
| int main()
| {
| for(std :: string line; std :: getline(std :: cin,line);)
| {
| std :: string tmp(line);
| tmp.erase(std :: remove_if(tmp.begin(),tmp.end(),notalpha),
| tmp.end());
| std :: cout<< tmp<< \ n;
| }
| }
| #include <iostream>
| #include <string>
| #include <algorithm>
| #include <ctype.h>
|
| bool notalpha(char c)
| {
| return !std::isalpha(static_cast<unsigned char>(c));
| }
|
| int main()
| {
| for (std::string line; std::getline(std::cin, line); )
| {
| std::string tmp(line);
| tmp.erase(std::remove_if(tmp.begin(), tmp.end(), notalpha),
| tmp.end());
| std::cout << tmp << "\n";
| }
| }




如果你将''|''/ $
更改为/ ** /?然后我们可以复制/粘贴和编译。


-Mike



Will google post that correctly if you change the ''|''
to /**/ ? Then we could copy/paste and compile.

-Mike


这篇关于某事错了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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