① 用c語言編寫的反轉句子 如把: what is your name 換成 name your is what 求大神幫忙,看看哪裡錯
自己寫的一個,用C標准庫字元串函數實現的,指針用不好很容易出錯。
主要思路:將整個句子以空格「 」分割成多個字元串,保存在臨時數組str_tmp[MAXLEN][20]中;將臨時數組反序寫入到str_reverse[MAXLEN]中,輸出就可以了。
源程序:
#include <stdio.h>
#include <string.h>
#define MAXLEN 80
void main()
{
//char str[MAXLEN] = "what is your name";
char str[MAXLEN] = {0};
char str_tmp[MAXLEN][20] = {0};
char str_reverse[MAXLEN] = {0};
char *p,*delim = " ";
int i = 0,n;
printf("please input :"); //長度不要超過MAXLEN=80,可以修改宏;
scanf("%[^ ]",str); //允許輸入空格,遇回車才結束輸入;
p = strtok(str,delim);
while(p != NULL)
{
strcpy(str_tmp[i],p); //分割的字元串保存到臨時數組;
p = strtok(NULL,delim);
i++;
}
for(n = i-1;n >= 0;n--)
{
strcat(str_reverse,str_tmp[n]); //將臨時數組從後往前的字元串追加到反序數組中;
strcat(str_reverse," ");
}
printf("%s ",str_reverse);
}
運行結果截圖:
② C語言每個句子的每一個單詞 倒置
給出一個句子,把每一個字母倒置,但不改變單詞的順序。
輸入:
這個題有多個CASE。第一行給出一個正整數N(N<500),然後緊接著一個空行,再緊接著N個數據塊,。每一個數據塊,包括一個正整數T與T行英語句子。句子總長不超過300。每兩個數據塊之間有一個空行。每個句子只包含大寫字母與小寫字母。
輸出:
對於每個數據塊 一行一句輸出。每兩個數據塊之間用一個空行分隔。
輸入樣例:
1
3
I am happy today
To be or not to be
I want to win the practice contest
輸出樣例:
I ma yppah yadot
oT eb ro ton ot eb
I tnaw ot niw eht ecitcarp tsetnoc
③ 英文句子倒敘:(C語言實現)請按要求實現!
#include <stdio.h>
int nw(char *s)
{
int n=0,flag=0;
while (*s!='\0')
{
if(*s==' ')
flag=0;
else if(flag==0)
{
n++;
flag=1;
}
s++;
}
return n;
}
void iw(char *s)
{
char a[10][20];
int i,j,b;
b=nw(s);
for(i=0;i<b;i++)
{
j=0;
while(*s!='\0')
{
if(*s!=' ')
{
a[i][j]=*s;
j++;
s++;
}
else
{
s++;
a[i][j]='\0';
break;
}
}
}
a[i-1][j]='\0';
for(i=b-1;i>=0;i--)
printf("%s ",a[i]);
printf("\n");
}
void main()
{
char a[100];
printf("請輸入一句話:\n");
gets(a);
iw(a);
}
④ c語言編寫程序顛倒句子
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#defineOK1
#defineM5
//建單詞節點
structNode{
char*data;
Node*next;
};
charf;
//插入函數
voidInsert(Node*&S,chare[])
{
Node*p;
p=(Node*)malloc(sizeof(Node));
p->data=(char*)malloc(strlen(e)+1);
p->next=NULL;
strcpy(p->data,e);
p->next=S;
S=p;
}
voidCreat(Node*&S)
{
inti=0,k=0,j,flag=1,insert;
chars[20];
chara[2000]={0};
printf(" 輸入正文:");
fflush(stdin);
gets(a);
printf(" 原文為:");
while(a[i]!=NULL)
{
insert=0;
memset(s,0,20);
j=0;
//跳過單詞前面的前導字元
while(flag&&!isalpha(a[i])||a[i]==39)//39為'的ASCII碼
{
i++;
if(a[i]==NULL)
flag=0;
}
//當前單詞尚未結束,則一直循環
while(flag&&isalpha(a[i])||a[i]==39)
{
insert=1;
s[j]=tolower(a[i]);
i++;
j++;
}
//單詞插入
if(insert)
{
s[i]='