项目2-用指针玩字符串

来源:互联网 发布:蓝鸟编程使用教程 编辑:程序博客网 时间:2024/05/02 02:36

功能

用数组名作形参

用指针作形参

1

字符串str1str2连接,连接后的结果存放到str1

void astrcat(char str1[], const char str2[])

void pstrcat(char *str1, const char *str2)

2

去除字符串str中的特定字符c(如空格),结果仍保存到原字符串中

void adelchar(char str[], const char c)

void pdelchar(char *str, const char c)

3

求字符串str的长度并返回

int astrlen(char str[])

int pstrlen(char *str)

4

统计句子str中单词的个数

int awordnum(char str[])

int pwordnum(char *str)

5

去除句子中第一个单词前的空格

void atrim(char str[])

void ptrim(char *str)

6

去除句子中所有多余的空格

void aalltrim(char str[])

void palltrim(char str[])

7

比较两个字符串,返回值同strcmp()

第4个貌似不太对呢。。。。

//字符串连接函数实现和测试示例#include <iostream>using namespace std;void astrcat(char str1[], const char str2[]);void adelchar(char str[], const char c);//去除字符串str中的特定字符c(如空格),结果仍保存到原字符串中int pstrlen(char *str);//求字符串str的长度并返回int pwordnum(char *str);//统计句子str中单词的个数void ptrim(char *str);//去除句子中第一个单词前的空格void aalltrim(char str[]);//去除句子中所有多余的空格int pstrcmp(const char *str1, const char *str2);//比较两个字符串,返回值同strcmp()int main(){    char s1[50]="Hello world.";    char s2[50]="Good morning! ";    astrcat(s1,s2);    cout<<"连接后:"<<s1<<endl;    char str[50]="wo men  dou shi hao hai  zi...";    adelchar(str,' ');    cout<<"去掉空格后:"<<str<<endl;    char str3[50]="wo";//men dou shi hao hai zi...";    cout<<"str3长度为:"<<pstrlen(str3)<<endl;    char str4[50]="wo men dou shi hao hai  zi...";//貌似有点问题。。。    cout<<"str4含有"<< pwordnum(str4)<<"个单词。。"<<endl;    char str5[50]="    wo men  dou  shi  hao hai zi...";    ptrim(str5);    cout<<"去掉头部空格后,str5为:"<<str5<<endl;    char str6[100]="wo       men            dou                     shi  hao     hai           zi...";    aalltrim(str6);    cout<<"去掉多余的空格后,str6为:"<<str6<<endl;    char str7[50]="woaini";char str8[50]="woaini";    cout<<"返回值为:"<<pstrcmp(str7,str8)<<endl;    return 0;}//作为示例,本函数采用了形参为数组,在实现中,直接用下标法进行访问//实际上,在实现中,完全可以用指针法访问void astrcat(char str1[], const char str2[]){    int i,j;//请理解:以下所有str1[i]可以替换为*(str1+i),str2[j]可以……    for(i=0; str1[i]!='\0'; i++); //找到str1的结束    for(j=0; str2[j]!='\0'; i++,j++)    {        str1[i]=str2[j];    }    str1[i]='\0';//切记!!    return;}void adelchar(char str[], const char c)//这个程序自己写出来后立刻就看不懂了,单步了一下才了解,难道这是妙手偶得之。。。{int j=0;for(int i=0;str[i]!='\0';i++){str[j]=str[i];if(str[i]==c)continue;else j++;}    str[j]='\0';}int pstrlen(char *str){int i;for( i=0;*(str+i)!='\0';i++);return i;}int pwordnum(char *str){int j=0;for(int i=0;*(str+i)!='\0';i++){if(' '==*(str+i))j++;}return j;}void ptrim(char *str)//我的想法是设置一个BOOL开关。。{int i=0,j=0;//bool  t=true;while(' '==str[i])i++;for(;*(str+i)!='\0';i++)str[j++]=str[i];str[j]='\0';}void aalltrim(char str[]){int i,j=0;bool t=true;//缺了去掉前导空格这一步
for(i=0;str[i]!='\0';i++)//考虑下换成while循环。。。??{if(str[i]!=' '){t=true;str[j++]=str[i];}if(' '==str[i]&&t){t=false;str[j++]=str[i];}}str[j]='\0';}int pstrcmp(const char *str1, const char *str2){for(int i=0;*(str1+i)!='\0'&&*(str2+i)!='\0';i++){if(str1[i]>str2[i])return 1;if(str2[i]>str1[i])return -1;}return 0;}


看到答案后:

int awordnum(char str[])//统计单词数量{ int i,num=0,word=0;  //word为0,代表现在并不 for(i=0;(str[i]!='\0');i++) {    if (str[i]==' ')word=0;    elseif (word==0){word=1;num++;} } return num;}


0 0
原创粉丝点击