7-1 word statistics (100 points)
A given string contains only # and lowercase letters, where # is a separator, and consecutive lowercase letters form a word. There is no # sign inside a word, and there is no # between two words. Please write a program to count the number and # of words in a string.
Input format:
The first line is an integer n (0 < n < 10), representing a total of N strings. The next N lines, each with a string with a length of no more than 100.
The test case ensures that the input is legal.
Output format:
There are n lines in total, and each line corresponds to the input of each string.
The format of each line is as follows:
The first is an integer representing the number of words, followed by a space; Then there is an integer representing # the quantity, followed by another space; Finally, output all words in turn, separated by a space between the words, and there is no space after the last word. If the number of words is 0, three consecutive words are output where the word should be output #.
Input sample:
2 # hello#world
Output example:
0 1 ### 2 1 hello world
Idea:
num two-dimensional array, the first deposit receipt word number and the second deposit well number
Principle of word function: when encountering a pound sign, judge whether the previous one is a pound sign. If not, save a space in words, and the number of pound signs + 1. The first and last cases should be judged separately
When you encounter a letter, if the first one is a pound sign, add one to the number of words and save the letter. Save only the beginning of the letter to judge whether it is a separate letter
Point of pit: 1 It's said that it won't exceed 100, but 101 is not enough. As a result, I've been wrong before. If I turn the array larger, I'll tm pass
2. Remove the space at the end of the word output, otherwise the format is wrong, md
code:
#include <stdio.h> #include <string.h> #include <stdlib.h> //A function is done //Set global variables int num[11][3]={0}; char words[11][150]={0}; void word(char arr[],int n); //void trim(char *strIn, char *strOut);// De whitespace function int rTrim(char *szBuf); int main(){ char a[12][150]={0}; int n=0; scanf("%d",&n); int i=0; for(i=0;i<n;i++){ scanf("%s",a[i]); } for(i=0;i<n;i++){ //Call function judgment char *p=a[i]; word(p,i); } int j=0; for(i=0;i<n;i++){ //char strout[100]={0}; printf("%d %d ",num[i][0],num[i][1]); //trim(words[i],strout); rTrim(words[i]); if(i==n-1) printf("%s",words[i]); else printf("%s\n",words[i]); } } void word(char arr[],int n){ int len=strlen(arr); char *s=arr; int i=0; int j=0; for(i=0;i<len;i++){ //If it's a well number if(s[i]=='#'){ if(i==0){ num[n][1]++; } else if(i==len-1){ num[n][1]++; } else if(s[i-1]!='#' && i!=0){ num[n][1]++; words[n][j]=' '; j++; } else if(s[i-1]=='#' && i!=0){ num[n][1]++; } } else if(s[i]!='#'{/ / letter if(i==0){//head num[n][0]++; words[n][j]=s[i]; j++; } else if(s[i-1]=='#'& & I > = 1) {/ / well number in front num[n][0]++; words[n][j]=s[i]; j++; } else if(s[i-1]!='#' && i>=1){ words[n][j]=s[i]; j++; } } } if(num[n][0]==0){//0 words words[n][0]='#'; words[n][1]='#'; words[n][2]='#'; words[n][3]='\0'; } /*if(num[n][1]==0){ num[n][0]=0; words[n][0]='#'; words[n][1]='#'; words[n][2]='#'; words[n][3]='\0'; } */ } int rTrim(char *szBuf)//Go to the end space { int i; i = strlen(szBuf)-1; while(szBuf[i] == ' '&&i >0) i--; szBuf[i+1] = '\0'; return 0; }