matlab201a教程,实验6 - 数媒201郭凯妮的个人空间 - OSCHINA - 中文开源技术交流社区...
1 .編程實現:輸入兩個英文名字串,比較兩個串的串長和串的大小,按要求輸出結 果。(比如,輸入的兩個名字串:Tom 與 Charli,輸出結果為: 較長的串是 Charli, 較 大的串是:Tom)
#include
#include
#include
#define STR_LEN 80
int main(){
char s1[STR_LEN+1],s2[STR_LEN+1];
printf("first str:");
gets(s1);
printf("second str:");
gets(s2);
if(strlen(s1)>strlen(s2)){
printf("%s is the longer string.\n",s1);
}
else if(strlen(s1)
printf("%s is the longer string.\n",s2);
}
else{
printf("%s and %s are same longer.\n",s1,s2);
}
if(strcmp(s1,s2)<0){
printf("%s is the largest string.\n",s2);
}
else if(strcmp(s1,s2)>0){
printf("%s is the largest string.\n",s1);
}
else{
printf("%s and %s are same largest.\n",s1,s2);
}
system("pause");
return 0;
}
2 .編程實現:輸入一個長串 str1(比如“my heart will go on”),再輸入一個短串 str2(比如“ear”),判斷在長串中是否有短串,比輸出結果(按以上的情況查詢,輸出結 果是在 str1 中有 str2)
#include
#include
#include
#define STR_LEN1 80
#define STR_LEN2 50
int main(){
char str1[STR_LEN1+1],str2[STR_LEN2+1];
printf("請輸入一個長串:");
gets(str1);
printf("請輸入一個短串:");
gets(str2);
if(strstr(str1,str2)!=NULL){
printf("%s 中沒有 %s.\n",str1,str2);
}
else{
printf("%s 中有 %s.\n",str1,str2);
}
system("pause");
return 0;
}
3
3.編程實現:輸入一行英文句子,按空格分出若干個單詞,每一行只輸出一個單詞。
#include
#include
#include
#define STR 80
int main(){
char a[STR+1];
int len;
printf("Enter a sentence:\n");
gets(a);
len=strlen(a);
for(int i=0;i
if(a[i]==' '){
a[i]='\n';
}
}
printf("Output as:\n");
printf("%s\n",a);
system("pause");
return 0;
}
4 .編寫程序:輸入一個字符串,分別統計字符串中大寫字母和小寫字母的個數。例 如,給字符串 SS 輸入:AaaaBBb123CCccccd,則輸出結果應為:upper=5,lower=9.
#include
#include
#include
#define STR_LEN 80
int main()
{
char ss[STR_LEN+1];
char c;
int upper=0,low=0;
printf("??????×?ˇ?′?:\n");
gets(ss);
int i=0;
while(ss[i]!='\0'){
if(isupper(ss[i]))
upper++;
if(islower(ss[i]))
low++;
i++;
}
printf("upper=%d,low=%d\n",upper,low);
system("pause");
return 0;
}
總結
以上是生活随笔為你收集整理的matlab201a教程,实验6 - 数媒201郭凯妮的个人空间 - OSCHINA - 中文开源技术交流社区...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: FastReport批量打印
- 下一篇: 草根IT对博客模板MaterialDes