" ispunct判断()"没有在std :: string上工作 [英] "ispunct()" not working on std::string

查看:107
本文介绍了" ispunct判断()"没有在std :: string上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/ * C ++ Primer 4 / e

*第3.2节 - 字符串标准库


*练习3.10

*声明

*编写一个程序来从字符串中去掉标点符号。 * /


#include< iostream>

#include< string>


int main()

{

std :: cout<< 输入一个字符串:" ;;

std :: string a_word,stripped_word;

std :: cin> a_word;


for(std :: string :: size_type ix = 0; ix!= a_word.size(); ++ ix)

{

if(ispunct( a_word [ix])== 0)

stripped_word [ix] = a_word [ix];

}


std: :cout<< stripped_word<< std :: endl;


返回0;

}


-

- http://arnuld.blogspot.com

/* C++ Primer 4/e
* section 3.2 - String Standard Library

* exercise 3.10
* STATEMENT
* write a programme to strip the punctation from the string. */

#include <iostream>
#include <string>

int main()
{
std::cout << "Enter a string: ";
std::string a_word, stripped_word;
std::cin >a_word;

for(std::string::size_type ix = 0; ix != a_word.size(); ++ix)
{
if(ispunct(a_word[ix]) == 0)
stripped_word[ix] = a_word[ix];
}

std::cout << stripped_word << std::endl;

return 0;
}

--
-- http://arnuld.blogspot.com

推荐答案

2007年7月18日星期三14:07:59 +0500,arnuld写道:
On Wed, 18 Jul 2007 14:07:59 +0500, arnuld wrote:

/ * C ++ Primer 4 / e

*第3.2节 - 字符串标准库


*练习3.10

*语句
*编写一个程序来从字符串中去除标点符号。 * /


#include< iostream>

#include< string>


int main()

{

std :: cout<< 输入一个字符串:" ;;

std :: string a_word,stripped_word;

std :: cin> a_word;


for(std :: string :: size_type ix = 0; ix!= a_word.size(); ++ ix)

{

if(ispunct( a_word [ix])== 0)

stripped_word [ix] = a_word [ix];

}


std: :cout<< stripped_word<< std :: endl;


返回0;

}
/* C++ Primer 4/e
* section 3.2 - String Standard Library

* exercise 3.10
* STATEMENT
* write a programme to strip the punctation from the string. */

#include <iostream>
#include <string>

int main()
{
std::cout << "Enter a string: ";
std::string a_word, stripped_word;
std::cin >a_word;

for(std::string::size_type ix = 0; ix != a_word.size(); ++ix)
{
if(ispunct(a_word[ix]) == 0)
stripped_word[ix] = a_word[ix];
}

std::cout << stripped_word << std::endl;

return 0;
}



抱歉,我忘记了发布输出:


[arnuld @ arch cpp]%g ++ -ansi -pedantic -Wall -Wextra ex_03-10.cpp

[arnuld @ arch cpp ]%。/ a.out

输入一个字符串:comp.lang.c ++


[arnuld @ arch cpp]%
$ b $你看,它没有打印任何东西,但它编译成功。

哪里有错误?

-

- < a rel =nofollowhref =http://arnuld.blogspot.comtarget =_ blank> http://arnuld.blogspot.com


SORRY, i forgot to post the output:

[arnuld@arch cpp ]% g++ -ansi -pedantic -Wall -Wextra ex_03-10.cpp
[arnuld@arch cpp ]% ./a.out
Enter a string: comp.lang.c++

[arnuld@arch cpp ]%
you see, it does not prinit anything at all but it compiled successfully.
where is the bug ?
--
-- http://arnuld.blogspot.com


2007年7月18日星期三14:07:59 +0500,arnuld写道:
On Wed, 18 Jul 2007 14:07:59 +0500, arnuld wrote:

/ * C ++ Primer 4 / e
*第3.2节 - 字符串标准库


*练习3.10

*语句

*写一个程序到剥去字符串中的标点符号。 * /


#include< iostream>

#include< string>


int main()

{

std :: cout<< 输入一个字符串:" ;;

std :: string a_word,stripped_word;

std :: cin> a_word;


for(std :: string :: size_type ix = 0; ix!= a_word.size(); ++ ix)

{

if(ispunct( a_word [ix])== 0)

stripped_word [ix] = a_word [ix];
/* C++ Primer 4/e
* section 3.2 - String Standard Library

* exercise 3.10
* STATEMENT
* write a programme to strip the punctation from the string. */

#include <iostream>
#include <string>

int main()
{
std::cout << "Enter a string: ";
std::string a_word, stripped_word;
std::cin >a_word;

for(std::string::size_type ix = 0; ix != a_word.size(); ++ix)
{
if(ispunct(a_word[ix]) == 0)
stripped_word[ix] = a_word[ix];



std :: string不是一个开放数组,你不能只将

分配给随机不存在的位置。 br />

stripped_word + = a_word [ix];



stripped_word.append(a_word [ix]);

A std::string is not an open array, you can''t just assign
to random non-existing positions.

stripped_word += a_word[ix];
or
stripped_word.append(a_word[ix]);


}


std :: cout<< stripped_word<< std :: endl;


返回0;

}
}

std::cout << stripped_word << std::endl;

return 0;
}



-

令人讨厌的用户

--
Obnoxious User


arnuld写道:
arnuld wrote:

/ * C ++ Primer 4 / e

*第3.2节 - 字符串标准库


*练习3.10

*语句

*编写程序从字符串中剥去标点符号。 * /


#include< iostream>

#include< string>


int main()

{

std :: cout<< 输入一个字符串:" ;;

std :: string a_word,stripped_word;

std :: cin> a_word;


for(std :: string :: size_type ix = 0; ix!= a_word.size(); ++ ix)

{

if(ispunct( a_word [ix])== 0)

stripped_word [ix] = a_word [ix];

}


std: :cout<< stripped_word<< std :: endl;


返回0;

}
/* C++ Primer 4/e
* section 3.2 - String Standard Library

* exercise 3.10
* STATEMENT
* write a programme to strip the punctation from the string. */

#include <iostream>
#include <string>

int main()
{
std::cout << "Enter a string: ";
std::string a_word, stripped_word;
std::cin >a_word;

for(std::string::size_type ix = 0; ix != a_word.size(); ++ix)
{
if(ispunct(a_word[ix]) == 0)
stripped_word[ix] = a_word[ix];
}

std::cout << stripped_word << std::endl;

return 0;
}



这是固定的( hack)版本:

#include< iostream>

#include< string>


int main()

{

std :: cout<< 输入一个字符串:" ;;

std :: string a_word,stripped_word;

std :: cin> a_word;


for(std :: string :: size_type ix = 0; ix!= a_word.size(); ++ ix)

{

if(ispunct( a_word.at(ix))== 0)

stripped_word.append(1,a_word.at(ix));

}


std :: cout<< stripped_word<< std :: endl;


返回0;

}

Here is the fixed (hack) version:
#include <iostream>
#include <string>

int main()
{
std::cout << "Enter a string: ";
std::string a_word, stripped_word;
std::cin >a_word;

for(std::string::size_type ix = 0; ix != a_word.size(); ++ix)
{
if(ispunct(a_word.at(ix)) == 0)
stripped_word.append(1,a_word.at(ix));
}

std::cout << stripped_word << std::endl;

return 0;
}


这篇关于&QUOT; ispunct判断()&QUOT;没有在std :: string上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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