c语言求字符串中大写字母个数,单词个数,子串个数及一个整数是否为回文数
#include <stdio.h>
#include <ctype.h>
#pragma mark 统计从终端输入的字符中每个大写字母的个数。用#号作为输入结束标志
int main()
{
int num[26] = {0}, i; char c;
while ((c = getchar())!='#') {
if (isupper(c)) {
num[c-65]++;
}
}
for (int i = 0; i<26; i++) {
if (num[i]) {
printf("%c:%d\n",i+65, num[i]);
}
}
return 0;
}
#pragma mark 统计一行字符中单词个数
int main()
{
char s[81];
int i , c, num=0,word=0;
gets(s);
for(i=0;(c=s[i])!='\0';i++){
/*一:判断*/
if (c == 32) {
word++;
}else continue;
}
/*二:判断*/
if(c==' ') word = 0;
else if (word==0) {word=1; num++;}
printf("there are %d words.\n",word);
return 0;
}
#include <stdio.h>
#pragma mark 计算一个字符串中子串出现的次数
int main()
{
int i ,j, k,count;
char str1[20],str2[20];
printf("zhu chuan:");
gets(str1);
printf("zi chuan:");
gets(str2);
count = 0;
for(i=0;str1[i];i++)
for(j=i,k=0;str1[j]==str2[k];j++,k++)
if ( !str2[k+1])
count++;
printf("chuxian cishu=%d\n",count);
}
#pragma mark 求一个整数是否为回文数
#include <stdio.h>
int main()
{
long num;
int array[10];
scanf("%ld", &num);
int n = num, k = 0;
do {
array[k] = n%10;
k++;
n = n/10; // 完成了递进求个个位上的数字
} while (n!=0);
int i = 0;
int flag = 0;
for (; i<(k-1)/2; i++) {
if (array[i] == array[k-1-i]){ flag = 1;}
}
if (flag) {
printf("%ld 是回文", num);
}else{
printf("%ld 不是是回文", num);
}
}
/*
编程在一个已知的字符串中查找最长单词,假定字符串中只含字母和空格,用空格来分隔单词。
*/
#include <stdio.h>
int main()
{
const char *strings = "ac china uinforms word hellow";
int currentLen = 0;
char c;
int maxLen = 0, maxLenIndex = 0;
for (int i = 0; (c = strings[i])!='\0'; i++) {
if (c!=32) {
currentLen++;
}else
{
if (maxLen<currentLen) {
maxLen = currentLen;
maxLenIndex = i-currentLen;
}
currentLen = 0;
}
}
for (int i = maxLenIndex; i<maxLenIndex+maxLen; i++) {
printf("%c\n", strings[i]);
} return 0;
}