首页 > 学院 > 开发设计 > 正文

数据结构与算法之 字符串

2019-11-06 09:18:59
字体:
来源:转载
供稿:网友
#include<iostream>using namespace std;int main(void){ char * p="abc"; char a[]="bdc"; cout<<strcmp(p,a)<<endl; return 0;}#include<stdio.h>#include<string.h>int main(void){ char a[64] ; char p[64] = "hello"; char * q = "world!"; char w[]="he llo world!"; char * ret =strcpy(a,p); char * ret1 =strcat(p,q); char * ret2 = strstr(p,q); char * buf = strtok(w," "); PRintf("%s/n%s/n%s/n%s/n",ret,ret1,ret2,buf); return 0;}//输入一行字符,统计其中有多少个单词,单词之间用空格分隔开。#include<iostream>#include <string>using namespace std;int main(void){ int i = 0,sum=1; /*string ch; getline(cin,ch);*/ char ch[64]; gets(ch) cout<<ch<<endl; while (1) { if(ch[i]==NULL) break; if (ch[i]==' ')//空格这一个字符要用单引号 { sum++; } i++; } cout<<"The number of Words is:"<<sum<<endl; return 0;}//有3个字符串,要求找出其中的最大者#include<iostream>#include <string>using namespace std;int main(void){ /*string ch1,ch2,ch3,max; getline(cin,ch1); getline(cin,ch2); getline(cin,ch3); swap(max,ch1); if(ch1.compare(ch2)<0) swap(max,ch2); if(max.compare(ch3)<0) swap(max,ch3); cout<<"The maxium string is:"<<max<<endl;*/ char max[20]; char ch[3][20];//数组元素是[0][1][2] 没有[3] int i; for (i=0;i<=2;i++) { gets(ch[i]); } strcpy(max,ch[0]); if (strcmp(ch[0],ch[1])<0) strcpy(max,ch[1]); if(strcmp(max,ch[2])<0) strcpy(max,ch[2]); cout<<max<<endl; return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表