#ifndef _STRING_H
#define STRING_H
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#define ret_value 0
#define SPACE ' '
#define END '\n'
void Get_string(char str[100]);
int Total_ch(char str[100]);
int Total_str(char str[100]);
int Total_letter(char str[100]);
int Total_word(char str[100]);
void Compare_longest_word(char str[100]);
char Find_numMost(char Str[100]);
void print_word(char Str[100], char Ch);
#endif
#include"string.h"
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
void Get_string(char str[100])
{
printf("please input a string: ");
fgets(str, 100, stdin);
return;
}
int Total_ch(char str[100])
{
int count1 = 0;
int index1 = 0;
int Len = strlen(str);
if (Len<0)
{
return -1;
}
while (index1 < Len-1)
{
++count1;
++index1;
}
printf("the total of character is %d\n", count1);
return count1;
}
int Total_str(char str[100])
{
//int str_count = 0;
//int index2 = 0;
//int str_begin = 0;
//int Len = strlen(str);
//if (Len<0)
//{
// return -1;
//}
考虑只有一个字符串的情况
//if ((str[++index2] == END) && (str[++index2] != SPACE))
//{
// return ret_value;
//}
考虑中间出现连续空格的情况
//while (index2<Len)
//{
// if (SPACE != str[index2])
// {
// //不为空格,标记为str_begin
// if (!str_begin)
// {
// str_begin = 1;
// }
// }
// else
// {
// if (str_begin)
// {
// ++str_count;
// str_begin = 0;
// }
// }
// ++index2;
// if (index2 == (strlen(str)) && (1 == str_begin))
// {
// ++str_count;
// }
//}
//printf("the total of string is %d\n", str_count);
//return str_count;
int Count_str = 0;
int index = 0;
int Len = strlen(str);
if (Len<0)
{
return 0;
}
while (index<Len)
{
if (isalnum(str[index])) //是字母或者数字
{
if ((SPACE == str[index + 1]) || (END == str[index + 1]))
{
++Count_str;
}
}
++index;
}
printf("the total of string is %d\n", Count_str);
return Count_str;
}
int Total_letter(char str[100])
{
int Count_letter = 0;
int index = 0;
int Len = strlen(str);
while (index<Len-1)
{
if (((str[index] >= 'A') && (str[index] <= 'Z')) ||
((str[index] >= 'a') && (str[index] <= 'z')))
{
++Count_letter;
}
++index;
}
printf("the total of letter is %d\n", Count_letter);
return Count_letter;
}
int Total_word(char str[100])
{
int Count_word = 0;
int index = 0;
int Len = strlen(str);
if (Len<0)
{
return 0;
}
while (index<Len)
{
if (isalnum(str[index])) //是字母或者数字
{
if ((SPACE == str[index + 1]) || (END == str[index + 1]))
{
++Count_word;
}
}
++index;
}
printf("the total of word is %d\n", Count_word);
return Count_word;
}
void Compare_longest_word(char str[100])
{
//如果是空格,更新当前下标,把单词长度清0;
//往下遍历,与上一个比较,如果大于就更新最大值;记录最大下标,清空数组的数据,记录最长单词;
//与上一个比较,如果等于就更新最大值;记录最大下标,用空格分开上一个单词,继续记录最长单词;
//打印记录的单词
int Longest_len = 0;
int index = 0;
int lenth = 0;
int Max_index = 0;
int Len = strlen(str);
int i = 0;
char New_str[100] = { 0 };
int New_len = strlen(New_str);
int New_index = 0;
while (index < Len-1 )
{
if (SPACE == str[index])
{
++index;
lenth = 0;
}
//if ((SPACE != str[index]) && (END != str[index]))
if ((SPACE != str[index]) && (index < Len - 1))
{
++index;
++lenth;
}
if (lenth > Longest_len)
{
Longest_len = lenth;
Max_index = index - Longest_len;
New_index = 0;//比最长单词还长的单词重新保存
memset(New_str, 0, sizeof(New_str));//出现最长的单词清空数组
for (i = Max_index; i < Max_index + Longest_len; ++i)
{
New_str[++New_index] = str[i];
}
}
else if (lenth == Longest_len)
{
Longest_len = lenth;
Max_index = index - Longest_len;
New_str[++New_index] = SPACE;
//++New_index;
for (int j = Max_index; j < Max_index + Longest_len; ++j)
{
New_str[++New_index] = str[j];
}
}
}
printf("The lenth of longest_string %d\n", Longest_len);
printf("The longest string is ");
for (int z = 0; z < New_index+1; ++z)
{
printf("%c", New_str[z]);
}
printf("\n");
return;
}
//遍历一个个分别与数组元素比较
//如果大于最大值,更新最大值,记录当前的字母;开辟新空间存该字母。
//如果等于最大值,更新最大值,记录当前的字母;遍历新空间,是否有相同的字母出现,如果有则结束循环,没有则存下该字母。
char Find_numMost(char Str[100])
{
int Index = 0;
int Len = strlen(Str);
int Max_count = 0;
int Count = 0;
char Ch = 0;//查找出现最多的字符
int Next_index = 0;
char New_str[100] = { 0 };
int New_index = 0;
int i = 0;
while (Next_index<Len-1)
{
if (SPACE == Str[Next_index])
{
++Next_index;
}
if (Index == Len)
{
Count = 0;
Index = 0;
}
if (isalnum(Str[Next_index])) //是字母或者数字
{
for (Index; Index < Len; ++Index)
{
if (0 == (Str[Next_index] ^ Str[Index]))
{
++Count;
}
}
}
if (Count > Max_count)
{
Max_count = Count;
Ch = Str[Next_index];
New_index = 0;//出现次数最多的字母,下标清0
memset(New_str, 0, sizeof(New_str));//出现次数最多的字母,清空数组
New_str[New_index] = Ch;
}
if (Count == Max_count)
{
Max_count = Count;
Ch = Str[Next_index];
int len = strlen(New_str);
int i = 0;
int flag = 1;//与新开辟的数组有相同的字母
for (i; i < len; ++i)
{
if (Ch == New_str[i])
{
flag = 1;
i = len - 1;//存在相同的字母,就停止循环
}
else
{
flag = 0;
}
}
if (!flag)//不存在相同的字母
{
New_str[++New_index] = Ch;
}
}
++Next_index;
}
printf("the most letter : ");
for (int j = 0; j < New_index + 1; ++j)
{
printf(" %c ", New_str[j]);
}
printf(" \n");
printf("the count of the most letter is %d\n", Max_count);
return Ch;
}
//当没有遇到空格或者遇到出现最多的字母,就遍历数组
//如果遇到出现最多的字母,在空格或者\n之前一直遍历数组。单词起始下标=遍历的下标-单词长度
//找到一个单词后,下标+1,长度清0,更新下一个单词的起始下标。
void print_word(char Str[100], char Ch)
{
int Len = strlen(Str);
int Start_index = 0;
int End_index = 0;
int Index = 0;
int Length = 0;
int i = 0;
while (Index<Len - 1)
{
while ((Str[Index] != SPACE) && (Ch != Str[Index]) && (Index<Len - 1))
{
++Index;
++Length;
}
if (Ch == Str[Index])
{
while ((SPACE != Str[Index]) && (Index<Len - 1))
{
if (END != Str[Index]){
++Length;
++Index;
}
}
End_index = Length;
Start_index = Index - End_index;
printf("The new words:\n");
for (i = Start_index; i <Index; ++i)
{
printf("%c", Str[i]);
}
printf("\n");
}
++Index;
Length = 0;
Start_index = Index;
}
return;
}
//输入一串字符串,字符串中的字串以空格分隔,统计输入字符个数,统计以空格分隔子串个数
//
//1.从键盘获取字符串
// 判断是不是空字符串;
// 判断是不是非法字符串
// 判断字符串长度是否小于定义的空间
// 打印出字符串
//
//2.统计输入字符的个数
//
//当string!='\0'时count++
//
//3.统计字串个数
//当string == ' '时count++
//输入一串字符串,统计有多少个英文字母,统计有多少个单词,统计最长单词长度并显示该单词。
//1.统计英文字母letter:在a-z;A-Z之间,统计一次
//2.统计单词:当不是‘\0‘时遍历字符串,当不是空格的时候,判断有没有连续的字符在a-z;A-Z之间,
//如果是,就统计;如果不是就继续下一个字符判断。
//4.输入一串字符串,找到出现频率最高的字母,统计字母出现次数,显示出包含该字母的所有单词。
#include "string.h"
#include<stdlib.h>
#include <windows.h>
int main()
{
char str[100] = { 0 };
Get_string(str);
//printf("this is string :%s\n", str);
Total_ch(str);
Total_str(str);
Total_letter(str);
Total_word(str);
Compare_longest_word(str);
char ch=Find_numMost(str);
print_word(str,ch);
system("pause");
return 0;
}