1)统计单词个数:
介绍:统计字符串单词个数,判断标志为'\0'
void countWords(){
int i = 0;
int j = 0;
int count = 0;
char ch[100];
printf("请输入一串字符串:");
scanf("%s", &ch);
printf("\n");
while(ch[i] != '\0'){
if(ch[i] == ','){
count++;
}
i++;
}
printf("一共有单词 %d 个...",count+1);
}
2)字符串排序:(冒泡排序实现)
void countWords(char** ch, int row){
//row是指二维数组的行数
int i,j;
int flag = 0;//标志位,如果flag=0意味着整个字符串是有序的
char temp[100];
for(i=0; i<row-1; i++){
for(j=0, j<row-i-1; j++){
if(strcmp(ch[i],ch[i++])>0){
strcpy(temp, *(ch+i));
strcpy(*(ch+i),*(ch+i+1));
strcpy(*(ch+i+1),temp);
flag = 1;
}
}
if(flag=0){
break;
}
}
}
3)统计子字符串出现的次数:
void countWords(char* parent, char* chid){
/*strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串。
如果是,则该函数返回str2在str1中首次出现的地址;
否则,返回NULL。
*/
int count = 0;
char* p;
do{
p = strstr(parent, chid);//p是一个地址,如果存在子字符串,返回该子字符串第一次出现的位置
if(p != NULL){
count++;
parent = p+1;//将指针指向下一位置
}
}while(p != NULL);
printf("子字符串出现的次数为:%d", count);
}
4)统计单个字符在字符串出现的次数:
int countOneWordShow(char ch, char* parent){
int i = 0;
int count = 0;
for(i; i<strlen(parent); i++){
if(ch == parent[i]){
count++;
}
}
//printf("字符%c出现的次数为:%d", ch, count);
return count;
}
5)统计字符串中所有字符出现的次数:
思路:统计经过遍历第一次出现的字符在字符串的所有次数
//统计字符串所有字符出现的次数
void countAllWordShow(char* parent){
int count = 0;//计数出现次数
int i;
int j;
char ch;
for(i=0; i<strlen(parent); i++){
ch = parent[i];
int flag = 0;//如果flag=1,说明该字符已经统计过
if(i==0){
count = countOneWordShow(ch, parent);
printf("字符%c出现的次数为:%d\n", ch, count);
}else{
for(j=0; j<i; j++){
if(parent[j]==ch){
flag = 1;
}
}
if(flag != 1){
count = countOneWordShow(ch, parent);
printf("字符%c出现的次数为:%d\n", ch, count);
}
}
}
}
//统计某个字符出现的次数
int countOneWordShow(char ch, char* parent){
int i = 0;
int count = 0;
for(i; i<strlen(parent); i++){
if(ch == parent[i]){
count++;
}
}
//printf("字符%c出现的次数为:%d", ch, count);
return count;
}
6)字符串加密(加密算法:Assic+3):
void encrypt(char* ch){
int i;
char* temp;
temp = (char*)malloc(sizeof(char)*strlen(ch));//对temp作一个动态分配内存
for(i=0; i<strlen(ch); i++){
temp[i] = (int)(ch[i] + 3);
printf("%d\n", temp[i]);
}
}