注册 登录
编程论坛 C++教室

删除字符串中的标点符号的问题

liang890806 发布于 2009-09-11 12:19, 5520 次点击
代码如下
程序代码:
#include "stdafx.h"
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    string s1;
    cout<<"请输入一行带有标点的字符串"<<endl;
    getline(cin,s1);
    cout<<s1<<endl;
    for(string::size_type i=0;i!=s1.size();i++)
    {
        if(ispunct(s1[i]))
        {
            for(string::size_type j=i;j<=s1.size();j++)
            {
                s1[j]=s1[j+1];
            }
            --i;
        }
    }
    cout<<s1<<endl;
    return 0;
}

编译时没有问题
但是运行的时候会出错

string subscript out of rang

12 回复
#2
edward90922009-09-11 12:42
这里有个删除 符号的 函数 你看看:


程序代码:
void del_space(char *str)
{
        int i, j;
 
        for (i = j = 0; str[i] != '\0'; i++) {
                if (!ispunct(str[i])) {
                       str[j++] = str[i];
                }
        }
        str[j] = '\0';
}
#3
liang8908062009-09-11 12:44
哦哦~~~
这样写
嘿嘿
谢谢了
#4
xufen3402009-09-11 12:45
#include <string>
#include<iostream>
using namespace std;
int main(int argc, char* argv[])
{
    string s1;
    cout<<"请输入一行带有标点的字符串"<<endl;
    getline(cin,s1);
    cout<<s1<<endl;
    string::size_type i=0;
    string s2;
    while(i<s1.size())
    {
        if(!ispunct(s1[i])) s2+=s1[i];
        i++;
     }
    cout<<s2<<endl;
    return 0;
}
#5
edward90922009-09-11 12:48
你的算法没有问题  不知道是不是编译器的问题

我用vc  把你的代码改了一些  运行没问题啊

程序代码:
#include <iostream>
#include <string>
using namespace std;
 
 
int main()
{
    string s1;
    cout<<"请输入一行带有标点的字符串"<<endl;
    getline(cin,s1);
    cout<<s1<<endl;
    for(string::size_type i=0;i!=s1.size();i++)
    {
        if(ispunct(s1[i]))
        {
            for(string::size_type j=i;j<=s1.size();j++)
            {
                s1[j]=s1[j+1];
            }
            --i;
        }
    }
    cout<<s1<<endl;
    return 0;
}
#6
edward90922009-09-11 12:53
顺便问一下  这些事什么东西呢?  我看了半天没看懂..

不要笑话我  少见多怪

#include "stdafx.h" //这个头文件有什么用呢?
using namespace std;
 
 
int _tmain(int argc, _TCHAR* argv[])
//这是主函数吧?  带参数有什么用呢?  听说是给int main() 是一样的.不过用 这个编译没通过


谢谢了
#7
liang8908062009-09-11 12:53
这个函数无法删除在句尾的标点啊
#8
liang8908062009-09-11 12:55
我用的vc++ 2008
程序代码:
// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <string>



// TODO: 在此处引用程序需要的其他头文件
这是stdafx.h
#9
edward90922009-09-11 12:55
没问题啊??
#10
edward90922009-09-11 12:56
回复 8楼 liang890806
哦  难怪我的 vc里找不到 那个头文件啰..
#11
liang8908062009-09-11 12:57
我用vc++6.0
试试

#12
edward90922009-09-11 12:57
我的 代码 :

程序代码:
#include<iostream>
#include<cctype>
using namespace std;
void del_punct(char *str)
{
        int i, j;
 
        for (i = j = 0; str[i] != '\0'; i++) {
                if (!ispunct(str[i])) {
                       str[j++] = str[i];
                }
        }
        str[j] = '\0';
}
 
int main()
{
    char a[30];
    cin.getline(a,30);
    cout<<a<<endl;
 
    del_punct(a);
    cout<<endl;
    cout<<a<<endl;
    return 0;
    system("pause");
}


#13
liang8908062009-09-11 13:04
恩 在vc++6.0里面可以运行
还是编译器的问题
1