?? 有關單詞問題的處理方法.txt
字號:
/*要求很簡單, 用英文輸入隨意一段話,或者一句話,或者一編文章,
只需要把每個單詞的第一個字母跟最后一個字母掉轉后輸出,最后輸出打的單字用了多少行。
最后用戶可以選擇退出或者重新輸入。
例如: We are now really, really having fun with computers!
輸出: Ew era won yeallr, yeallr gavinh unf hitw somputerc!
This paragraph is used 1 line!
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXLINES 100 //最多一百行
#define MAXWORDS 10000 //最多一千個單詞
int get_lines(char *lines[]);//此函數用來輸入英文,并判斷它的行數
int get_words(char *words[], char *lines[], int n);//此函數用來存儲單個的單詞,空格和符號
void change_words(char *words[], int n);//此函數用來掉轉單詞的初,末字母
void print(char *a[], int n); //此函數用來輸出單詞
int main(void)
{
char *lines[MAXLINES];//該數組用來存儲每一行的內容
char *words[MAXWORDS];//該數組用來存儲單個的單詞,空格和符號
int number_of_lines, number_of_words;//此二變量分別存儲總行數和單個的單詞,空格和符號的總數
int flag;
do{
puts("用英文輸入隨意一段話,輸入1表示開始,輸入0表示離開:");
scanf("%d", &flag);
fflush(stdin);
if(flag == 1)
{
number_of_lines = get_lines(lines);//輸入英文,并返回它的行數
if(number_of_lines < 0)
{
puts("Memory allocation error");
return 1;
}
/*把每一行的單詞,空格和符號分別存儲起來*/
number_of_words = get_words(words, lines, number_of_lines);
if(number_of_words < 0)
{
puts("Memory allocation error");
return 1;
}
print(words, number_of_words);//輸出未掉轉前的單詞
change_words(words, number_of_words);//掉轉單詞的初,末字母 ,并輸出掉轉后的單詞
/*輸出打的單字用了多少行*/
printf("\nThis paragraph is used %d line!\n", number_of_lines);
}
} while(flag != 0);
getchar();
return 0;
}
int get_lines(char *lines[])
{
int n=0;
char buffer[80];//先把每一行的內容存儲在數組buffer[80]中
puts("Enter the article at time; enter a blank when done.");
while((n<MAXLINES)&&(gets(buffer)!=0)&&(buffer[0]!='\0'))
{
if((lines[n] = (char*)malloc(strlen(buffer)+1))==NULL)
return -1; //給lines[n],并把buffer[80]的內容復制給lines[n]
strcpy(lines[n++], buffer);
}
return n;//返回文章的行數
}
int get_words(char *words[], char *lines[], int n)
{
int i, j, k=0;
char str1[30], str2[2];
for(i=0; i<n; i++)
{
j = 0;
str1[0] = '\0';
while(lines[i][j]!='\0')
{
if(((lines[i][j]>='A')&&(lines[i][j]<='Z')||(lines[i][j]>='a')&&(lines[i][j]<='z'))
&&((lines[i][j+1]>='A')&&(lines[i][j+1]<='Z')||(lines[i][j+1]>='a')&&(lines[i][j+1]<='z')))
{ //如果該字符和它的下一個字符都是字母,則將其存入數組str1,但不必急著把str1復制給words[k]
str2[0] = lines[i][j];
strcat(str1, str2);
j++;
}
else if(((lines[i][j]>='A')&&(lines[i][j]<='Z')||(lines[i][j]>='a')&&(lines[i][j]<='z'))
&& !((lines[i][j+1]>='A')&&(lines[i][j+1]<='Z')||(lines[i][j+1]>='a')&&(lines[i][j+1]<='z')))
{ //如果該字符是字母,但它的下一個字符不是字母,則將其存入數組str1,并把str1復制給words[k]
str2[0] = lines[i][j];
strcat(str1, str2);
if((words[k] = (char*)malloc(strlen(str1)+1))==NULL)
return -1;
strcpy(words[k++], str1);
j++;
str1[0] = '\0';
}
else
{ //如果該字符不是是字母,則將其存入數組str1,并把str1復制給words[k]
str2[0] = lines[i][j];
strcat(str1, str2);
if((words[k] = (char*)malloc(strlen(str1)+1))==NULL)
return -1;
strcpy(words[k++], str1);
j++;
str1[0] = '\0';
}
} //把換行符復制給words[k]
str2[0] = '\n';
strcat(str1, str2);
if((words[k] = (char*)malloc(strlen(str1)+1))==NULL)
return -1;
strcpy(words[k++], str1);
}
return k;
}
void change_words(char *words[], int n)
{
int i, j;
char temp;
for(i=0; i<n; i++)
{
j = 0;
while(words[i][j+1] !='\0')
j++;
if(j != 0) //掉轉單詞的初,末字母
{
temp = words[i][0];
words[i][0] = words[i][j];
words[i][j] = temp;
}
for(int k=0; k<=j; k++) //輸出掉轉后的單詞
printf("%c", words[i][k]);
}
}
void print(char *a[], int n)
{
int i;
for(i=0; i<n; i++)
printf("%s", a[i]);
printf("\n");
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -