常见字符串问题总结(三)

常见字符串问题总结(三)

字符串问题中经常会有删除某一字符串的某一个字符,可以是不同方式的删除,比如将指定的字符删除,删除字符中的空格(包括字符串的头和尾)。本文就列举了部分有关字符串删除的问题。

1.  删除字符串中的空格,要求将字符串前面和后面的所有空格都删除,对字符串中间,如果出现多个连续的空格,只保留一个空格。

#include <iostream>
#include <cstring>
using namespace std;
/*删除一个字符串中的空格,包括前面的和后面的;如果中间字符间的空格大于1个则删除其它的空格,只保留一个空格 */
#define MAXSIZE 100//字符串数组的大小
void delspace(char *str)
{
    if(NULL == str)
        return;
    char *temp = strdup(str);
    int i =0;
    int j =0;
    while('\x20'== temp[i])
        i++;
    while('\0' != temp[i])
    {
        if(temp[i]!= ' ')
            str[j++] = temp[i++];
        else
        {
           while(temp[i] == ' ')
                i++;
            str[j++] = ' ';
        }
        }
    if(str[j-1] == ' ')
        str[j-1] = '\0';
    else
        str[j] = '\0';
    //strcpy(str, result);
    free(temp);
    }

int main()
{
    char test[MAXSIZE];
    gets(test);
    delspace(test);
    cout<<strlen(test)<<endl<<test<<endl;
    return 0;
}

2. 查找一个字符串中某个字符出现的频率。

利用hash表就可以实现该问题的解决办法,因为ASSIC码中共有256个字符因此可以建立一个256的hash映射数组,一次遍历字符串,记录每个字符出现的频率。

#include <iostream>

using namespace std;

int main()
{
    char str[100];
    int hash[256];
    memset(hash, 0, sizeof(int)*256);
    gets(str);
    char *temp = str;
    while('\0' != *temp)
    {
        hash[*temp + '\0'] +=1;
        temp++;
    }
    int max =0;
    char ch = '\0';
    for(int i =0; i< 256; i++)
    {
        if(hash[i] > max)
         {
            max = hash[i];
            ch = i+'\0';
        }
    }
    cout<<max<<endl<<ch<<endl;
    return 0;
}

3. 输入若干个字符串,按字典顺序对其排序,并输出。

一种情况是字符串只有大写或者小写字符,这是可以用strcmp进行比较。而strcmp的实现已经在前面的博文中说过了。

#include <iostream>
#include <cstring>
using namespace std;
/*输入五个字符串,按字典顺序对他们进行输出*/

int main()
{
    char str[5][10], temp[10], (*p1)[10], (*p2)[10];
    for(p1 = str; p1 < str+5; p1++)
        gets(*p1);
    cout<<endl;
    for(int i =1; i< 5;i++)
    {
       for(p1 =str, p2 =str +1;p1<str+5-i;p1++,p2++ )  //冒泡法排序
        {
            if(strcmp(*p1,*p2)>0)
            {
                strcpy(temp,*p2);
                strcpy(*p2, *p1);
                strcpy(*p1, temp);
                }
            }
        }
       for(int i =0; i< 5; i++)
            cout<<str[i]<<endl;
    return 0;
}

另一种情况是字符串中掺杂着大小写字符,这是可以用库函数中的strcasecmp()进行比较,本文中实现了strcasecmp()函数。

#include <iostream>
#include <cstring>
using namespace std;
/*输入五个字符串,按字典顺序对他们进行输出*/
char mytolower(char ch)
{
    if(ch >= 'A' && ch <= 'Z')
        ch = ch - ('A' - 'a');
    return ch;
    }

int Mystrcasecmp(char* src, char *dest)
{
    if(NULL == src || NULL == dest)
    {
        cout<<"parameter error";
        return 0;
        }

    while((*src == *dest)&&*src != '\0'&& *dest!='\0')
    {
        src++;
        dest++;
        }
    if(mytolower(*src) == mytolower(*dest))
        return 0;
    else if(mytolower(*src) > mytolower(*dest))
        return 1;
    else
        return -1;
    }
int main()
{
    char str[5][10], temp[10], (*p1)[10], (*p2)[10];
    for(p1 = str; p1 < str+5; p1++)
        gets(*p1);
    cout<<endl;
    for(int i =1; i< 5;i++)
    {
       for(p1 =str, p2 =str+1;p1<str+5-i;p1++,p2++ )
        {
            if(Mystrcasecmp(*p1,*p2)>0)
            {
                strcpy(temp,*p2);
                strcpy(*p2, *p1);
                strcpy(*p1, temp);
                }
            }
        }
       for(int i =0; i< 5; i++)
            cout<<str[i]<<endl;
    return 0;
}

每天练一些,继续加油。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值