當前位置:首頁 » 編程語言 » c語言顛倒英文句子
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言顛倒英文句子

發布時間: 2022-04-22 00:46:06

① 用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]='';
k++;
printf("%s ",s);
if(k%5==0)
printf(" ");
Insert(S,s);
j++;
}
}
if(!isalpha(a[i-1]))
f=a[i-1];
else
f='';
}
//輸出正文
voidPrint(Node*&T)
{
if(NULL!=T)
{
printf("%s",T->data);
Print(T->next);
}
else
return;
}

intmain()
{
Node*S=NULL;

Creat(S);
printf(" 倒置為:");
Print(S);
printf("%c",f);
printf(" ");
return0;
}


2013 5 25 8:16

耗時14min

1L

⑤ C語言 輸入一個句子,顛倒單詞並輸出。不能使用中間變數,就是不能單獨創建一個數組存放顛倒後的句子。

#include<stdio.h>
main()
{
char a[3][20];
int i=0;
scanf("%s %s %s",a[i],a[i+1],a[i+2]);
for(i=3-1;i>=0;i--)
printf("%s ",a[i]);
printf("\n");
getch();
}
這是用二維數組做的,當輸入how are you 時,第一循環,i=2時,從you開始輸出,然後執行i--,
為i=1;,,,,,,輸出 are ,,,,i--,直到i>=0時終止循環。

⑥ C語言中輸入一個英語句子將它翻轉後輸出

完成!輸入句子,回車換行。之後Ctrl+Z結束輸入,支持句子和段落反轉輸出。size是最大字元數,我設的為30,你可以自己修改,也可以用realloc設成動態數組。有什麼不懂得地方可以接著問我。

#include <stdio.h>

#include <malloc.h>

#include<string.h>

#define size 30

int main()

{

char *a;

int b;

a=(char*)malloc(size*sizeof(char));

scanf("%[^/n]",a);

b=strlen(a);

for(;b>0;b--)

printf("%c",a[b-1]);

printf(" ");

return 0;

}

⑦ C語言輸入一串英文句子,怎麼讓單詞順序不變,再倒序輸出 例如:I love you ,輸出為,y

//希望我的回答對你的學習有幫助
#include<stdio.h>
#include<string.h>
typedefstructStack{
inttop;
charc[100000];
}Stack;

voidinit(Stack&s){
s.top=0;
}

voidpush(Stack&s,charc){
s.c[s.top++]=c;
}

intpop(Stack&s,char*p){
inti=0;
while(s.top>0&&(*p++=s.c[--s.top])!=''){
i++;
}
returni-1;
}

voidrev(Stack&s){
charc[100];
while(s.top>0){
inti=pop(s,c);
for(;i>=0;i--){
printf("%c",c[i]);
}
printf("");
}
printf(" ");
}

intmain(){
Stacks;
init(s);
charc[1000];
gets(c);
for(inti=0;i<strlen(c);i++)
push(s,c[i]);
rev(s);
return0;
}

⑧ C語言把單詞順序倒置 比如see you later 編程later you see 我這個怎麼不行

你的思路不對啊,這樣的話太繁瑣的過程了,你先把字元串全部逆序,然後遇到空格再把一個單詞逆序,雖然可以達到目的,但是明顯,重復勞動了呀。

建議思路:
一個字元串,直接根據空格、逗號、句號直接對字元串進行切割。切割成單個單個的單詞,比如單詞A,B,C。如果從字元串的尾部開始切割,那麼直接將這些單詞保存到一個字元串里就行了。
函數建議:strrchr()函數