地址:http://blog.csdn.net/sxhelijian/article/details/41939705
【项目2-用指针玩字符串】
指针是神奇的,指向整型的指针int *p1,可以操作整型数组int a[];指向字符型的指针char *p2,可以操作字符数组(字符串)char str[];更灵活的是,在函数的传递中,指针、数组名在一定程度上可以互换。请编制函数,对字符串的进行各种操作。
序 |
功能 |
用数组名作形参 |
用指针作形参 |
1 |
字符串str1和str2连接,连接后的结果存放到str1中 |
char *astrcat(char str1[], const char str2[]) |
char *pstrcat(char *str1, const char *str2) |
2 |
去除字符串str中的特定字符c(如空格),结果仍保存到原字符串中 |
char *adelchar(char str[], const char c) |
char *pdelchar(char *str, const char c) |
3 |
求字符串str的长度并返回 |
int astrlen(char str[]) |
int pstrlen(char *str) |
4 |
统计句子str中单词的个数 |
不再重重复,做右边的(下同) |
int pwordnum(char *str) |
5 |
去除句子中第一个单词前的空格,返回去除了空格的字符串 |
|
char *ptrim(char *str) |
6 |
去除句子中所有多余的空格,返回去除了空格的字符串 |
|
char *palltrim(char *str) |
7 |
比较两个字符串,返回值同strcmp() |
|
int pstrcmp(const char *str1, const char *str2) |
提示1:不少函数中的参数用了const 限定符,如char *astrcat(char str1[], const char str2[])中的str2,其意义在于指定str2数组(或称str2指针指向的值)可以视为常变量,是不可改变的,从而有效的“保护”了数据,免得出现意外的修改;
提示2:在完成任务时,自编main函数进行测试,测试中,给出的的实际参数可以是以’\0’结束的字符数组,也可以是指向字符的指针。
提示3:在实际项目中,通过包括string.h头文件,可以调用C语言中操作字符串的函数完成相关操作,而不必专门定义。另外C++中加入的string类是更强大的支撑。不过,此处的练习,更利于同学们从微观理解,这也是专业人员要体会到的。
1、
功能:字符串str1和str2连接,连接后的结果存放到str1中
char *astrcat(char str1[], const char str2[])
[cpp] view plaincopyprint?
- #include <iostream>
- using namespace std;
- char *astrcat(char str1[], const char str2[]);
- int main(){
- char s1[50]="Hello world. ";
- char s2[50]="Good morning. ";
- char s3[50]="vegetable bird! ";
- astrcat(s1,s2);
- cout<<"连接后:"<<s1<<endl;
- cout<<"连接后:"<<astrcat(s2,s3)<<endl;
- return 0;
- }
-
-
- char *astrcat(char str1[], const char str2[])
- {
- int i,j;
-
- for(i=0; str1[i]!='\0'; i++);
- for(j=0; str2[j]!='\0'; i++,j++) {
- str1[i]=str2[j];
- }
- str1[i]='\0';
- return str1;
- }
char *pstrcat(char *str1, const char *str2)
[cpp] view plaincopyprint?
- #include <iostream>
- using namespace std;
- char *pstrcat(char *str1, const char *str2);
- int main(){
- char s1[50]="Hello world. ";
- char s2[50]="Good morning. ";
- char s3[50]="vegetable bird! ";
- pstrcat(s1,s2);
- cout<<"连接后:"<<s1<<endl;
- cout<<"连接后:"<<pstrcat(s2,s3)<<endl;
- return 0;
- }
-
-
-
-
- char *pstrcat(char *str1, const char *str2)
- {
- char *p;
- int i;
- for(p=str1;*p!='\0';p++);
- for(i=0;str2[i]!='\0';i++,p++)
- {
- *p=str2[i];
- }
- *p='\0';
- return str1;
- }
[cpp] view plaincopyprint?
-
- #include <iostream>
- #include <string>
- using namespace std;
- char* adelchar(char str[], const char c);
- char* pdelchar(char *str, const char c);
- int main(void)
- {
- char s[50]="Hello world. ";
- adelchar(s,'o');
- cout<<"\""<<s<<"\"去除 o 后,字符串为:"<<s<<endl;
- strcpy(s, "my name is tom.");
- adelchar(s,'m');
- cout<<"\""<<s<<"\" 去除 m 后 ,字符串为:"<<s<<endl;
- return 0;
- }
-
- char* adelchar(char str[], const char c)
- {
- int i=0,j;
- for(j=0;str[j]!='\0';j++)
- {
- if(str[j]!=c)
- str[i++]=str[j];
- }
- str[i]='\0';
- return str1;
- }
-
- char* pdelchar(char *str, const char c)
- {
- char *p=str,*q=str;
- for(;*q!='\0';q++)
- {
- if(*q!=c)
- *p++=*q;
- }
- *p='\0';
- return str1;
- }
[cpp] view plaincopyprint?
-
- #include <iostream>
- using namespace std;
- int astrlen(char str[]);
- int pstrlen(char *str);
- int main(void)
- {
- char s[50]="Hello world. ";
- cout<<"\""<<s<<"\"的长度为:"<<astrlen(s)<<endl;
- cout<<"\""<<s<<"\"的长度为:"<<pstrlen(s)<<endl;
- return 0;
- }
-
-
- int astrlen(char str[])
- {
- int l;
- for(l=0;str[l]!='\0';l++);
- return l;
- }
-
- int pstrlen(char *str)
- {
- int l=0;
- char *p;
- for(p=str;*p!='\0';p++) l++;
- return l;
- }
[cpp] view plaincopyprint?
-
- #include <iostream>
- #include <cstdio>
- using namespace std;
- int pwordnum(char *str);
-
- int main()
- {
- char s[81];
- cout<<"请输入一个句子:";
- gets(s);
- cout<<"\""<<s<<"\"中的单词数为:"<<pwordnum(s)<<endl;
- return 0;
- }
-
- int pwordnum(char *str)
- {
- int i,num=0,word=0;
- for(i=0; (*(str+i)!='\0'); i++)
- {
- if (*(str+i)==' ')
- word=0;
- else if (word==0)
- {
- word=1;
- num++;
- }
- }
- return num;
- }
[cpp] view plaincopyprint?
-
- #include <iostream>
- using namespace std;
- void ptrim(char *str);
- int main(void)
- {
- char s[81]=" Some spaces before the first word. ";
- cout<<"请输入一个句子:";
- cout<<"原句子:\""<<s<<"\""<<endl;
- ptrim(s);
- cout<<"去除前导空格后为:\"";
- cout<<s<<"\""<<endl;
- return 0;
- }
-
- void ptrim(char *str)
- {
- char *p=str,*q=str;
- while(*q==' ')
- q++;
- while(*q!='\0')
- *p++=*q++;
- *p='\0';
- }
[cpp] view plaincopyprint?
-
- #include <iostream>
- using namespace std;
-
- void palltrim(char str[]);
- int main(void)
- {
- char s[81]=" Some spaces are unnecessary. ";
- cout<<"请输入一个句子:";
- cout<<"原句子:\""<<s<<"\""<<endl;
- palltrim(s);
- cout<<"去除多余空格后为:\"";
- cout<<s<<"\""<<endl;
- return 0;
- }
-
- void palltrim(char *str)
- {
- char *p=str,*q=str;
- bool notSpace=false;
- while(*q==' ')
- q++;
- while(*q!='\0')
- {
- if (*q!=' ')
- {
- notSpace=true;
- *p++=*q++;
- }
- else if (notSpace)
- {
- notSpace=false;
- *p++=*q++;
- }
- else
- {
- q++;
- }
- }
- *p='\0';
- }
[cpp] view plaincopyprint?
-
- #include <iostream>
- #include <iomanip>
- using namespace std;
-
- int pstrcmp(char *str1, char *str2);
- int main(void)
- {
- char *s[81]= {"Tudou","Shanyaodan","Yangyu","Malingshu"};
- int i=0,j=0;
- cout<<"土豆大PK:"<<endl;
- cout<<setw(12)<<" ";
- for(; i<4; i++)
- {
- cout<<setw(12)<<s[i];
- }
- cout<<endl;
- for(i=0; i<4; i++)
- {
- cout<<setw(12)<<s[i];
- for(j=0; j<4; j++)
- {
- cout<<setw(12)<<pstrcmp(s[i],s[j]);
- }
- cout<<endl;
- }
- return 0;
- }
-
-
-
-
-
- int pstrcmp(char *str1, char *str2)
- {
- char *p=str1,*q=str2;
- while(*p==*q&&*p!='\0'&&*q!='\0')
- {
- p++;
- q++;
- }
- if (*p>*q)
- return 1;
- else if(*p<*q)
- return -1;
- else
- return 0;
- }
-