C++中常用字符串相关的编程题

索引

找出字符串中的数字,字母和符号,并分别存储

找出字符串中所有不重复的字符,并输出

统计字符串中每个字符的个数,并输出

编译环境

以下所用的开发环境是vs2010,创建的都为控制台输出程序。下面只贴出创建项目后修改了的cpp文件,即主要实现,其它文件保持自动生成的不变。

示例1-找出字符串中的数字,字母和符号,并分别存储

项目结构
在这里插入图片描述

具体的实现如下:

// sortTest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <stdlib.h>

using namespace std;

/************************************************************************/
/* 找出字符串中的数字,字母和符号,并分别存储       
*  不足:输出的数字是其数字字符的十进制,没有按照字符输出/
/************************************************************************/

int getCharType(char c)
{
    int nType = 0;
    if(c >= 48 && c <= 57){
        nType = 1;
    }else if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')
    {
        nType = 2;
    }else{
        nType = 3;
    }
    return nType;
}

void storeToVec(const char c,vector<int> &nVec,vector<char> &cVec,vector<char> &scpaceVec){
    int type = getCharType(c);
    switch(type){
    case 1:
        nVec.push_back(c);
        break;
    case 2:
        cVec.push_back(c);
        break;
    case 3:
        scpaceVec.push_back(c);
        break;
    default:
        break;
    }
}

template<typename T>
void printVec(const vector<T> vec){
    cout<<"================================="<<endl;
    vector<T>::const_iterator it = vec.begin();
    for (;it != vec.end();++it)
    {
        cout<<(char)*it<<"\t";
    }
    cout<<endl;
}


void sortString(const char *parr){
    int i = 0;
    vector<int> nVec;//存储0-9数字
    vector<char> cVev;//存储a-z或A-Z的字符
    vector<char> spaceVec;//存储回车符等
    while (parr[i] != '\0')
    {
        storeToVec(parr[i],nVec,cVev,spaceVec);
        ++i;
    }
    printVec<int>(nVec);
    printVec<char>(cVev);
    printVec<char>(spaceVec);
}

int _tmain(int argc, _TCHAR* argv[])
{
    const char array[] = "sdgygy7764s22dfsjhhj#!@kdf*";
    sortString(array);
    system("pause");

	return 0;
}

运行结果如下:

在这里插入图片描述

示例2-找出字符串中所有不重复的字符,并输出

项目结构
在这里插入图片描述
实现代码如下:

// stringUniqueOutTest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <stdlib.h>

using namespace std;

/************************************************************************/
/* 找出字符串中所有不重复的字符,并输出      */
/************************************************************************/

void compareExist(vector<char> &c,char sc){
    bool flag = false;
    for(int i = 0; i < c.size(); ++i){
        if(sc == c[i]){
            flag = true;
            break;
        }
    }
    if(!flag){
        c.push_back(sc);
    }
}

void printVec(const vector<char> vec){
    vector<char>::const_iterator it = vec.begin();
    while(it != vec.end()){
        cout<<*it<<"\t";
        ++it;
    }
    cout<<endl;
}


void printSingleChar(const char *parr){
    int i = 0;
    vector<char> cVec;
    if(parr[i] != '\0'){
        cVec.push_back(parr[i]);
        ++i;
    } 
    while (parr[i] != '\0'){
        compareExist(cVec,parr[i]);
        ++i;
    }

    printVec(cVec);
}


int _tmain(int argc, _TCHAR* argv[])
{
    const char srray[] = "hudfyegtysdsgftfsd";
    printSingleChar(srray);

    system("pause");
	return 0;
}

运行结果如下:

在这里插入图片描述

示例3-统计字符串中每个字符的个数,并输出

项目结构
在这里插入图片描述
实现代码如下:

// countCharNumberTest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <stdlib.h>

using namespace std;

/************************************************************************/
/* 统计字符串中每个字符的个数,并输出                       */
/************************************************************************/

bool findCharIsInVec(vector<char> &vec,char c){
    vector<char>::const_iterator it = vec.begin();
    for(;it != vec.end(); ++it){
        if (*it == c)
        {
            return true;
        }
    }
    return false;
}

void charOccurNumber(int &count,char c,const char *parr,int i,int nSize){
    for (int n = i + 1; n < nSize; ++n)
    {
        if(c == parr[n]){
            ++count;
        }
    }
}

void CountChar(const char * parr){
    vector<char> cVec;
    int count = 0;
    int i = 0;
    int nSize = strlen(parr);//求字符串的长度
    while (parr[i] != '\0'){
        if(!cVec.empty()){
            bool ret = findCharIsInVec(cVec,parr[i]);
            if (ret)
            {
                ++i;
                continue;
            }
        }
        ++count;
        charOccurNumber(count,parr[i],parr,i,nSize);
        cout<<"字符"<<parr[i]<<"出现的次数:"<<count<<endl;
        cVec.push_back(parr[i]);
        count = 0;
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    const char starr[] = "hudhfuhuheweashjhjdsfsd";
    CountChar(starr);

    system("pause");
	return 0;
}

运行结果如下:

在这里插入图片描述
示例3的第二种实现:

#include "stdafx.h"
#include <iostream>
#include <stdlib.h>

using namespace std;

/************************************************************************/
/* 统计字符串中每个字符的个数,并输出                       */
/************************************************************************/
void showCharNums(const char* pArr)
{
    if(pArr==NULL)
        return;
    char ascii[256] = {0};
    int nLen = strlen(pArr);
    for (int i=0;i<nLen;++i)
    {
        ascii[pArr[i]]++;//ascii[pArr[i]] = ascii[pArr[i]] + 1
    }
    for (int i=0;i<256;++i)
    {
        if (ascii[i]>0)
        {
            cout<<(char)i<<" : "<<(int)ascii[i]<<endl;
        }
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    const char starr[] = "hudhfuhuheweashjhjdsfsd";
    CountChar(starr);
    cout<<"========================================="<<endl;
    showCharNums(starr);

    system("pause");
	return 0;
}

运行结果

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

肩上风骋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值