1. 程式人生 > >將一句英文按單詞反序輸出

將一句英文按單詞反序輸出

題目:將一個英文語句以單詞為單位逆序排放。例如“I am a boy”,逆序排放後為“boy a am I”所有單詞之間用一個空格隔開,語句中除了英文字母外,不再包含其他字元 

思路1:每次輸出一個單詞後,str_len就減少該單詞的長度

心得體會:用fputs()輸入的字串,敲回車結束輸入時,該函式會將換行符也保留在字串中,所以用strlen計算字串長度時,比一般的函式輸入的要多一個字元(‘\0’是不算在內的)。

/*************************************************************************
    > File Name: e15.c
    > Author: LNM
    > Mail: 
[email protected]
> Created Time: 2018年09月03日 星期一 19時52分53秒 >function:將一個英文語句以單詞為單位逆序排放。例如“I am a boy”,逆序排放後為“boy a am I”所有單詞之間用一個空格隔開,語句中除了英文字母外,不再包含其他字元 ************************************************************************/ #include<stdio.h> #include<string.h> #define MAX 1000 int main() { char str[MAX]; int len,i,j; fgets(str,MAX,stdin); len = strlen(str); for(i = len - 2;i > 0;i -- ) { if(str[i] == ' ') { for(j = i + 1;j < len - 1;j ++) { printf("%c",str[j]); } j --; len = len -((j - i) + 1); printf(" "); } } while(str[i] != ' ') { printf("%c",str[i]); i ++; } return 0; }

思路2:每次遇到空格就停止輸出,進入輸出下一個單詞的迴圈

#include<stdio.h>
#include<string.h>

#define MAX     1000

int main()
{
    char str[MAX];
    int len,i,j;

    fgets(str,MAX,stdin);

    len = strlen(str);

	for(i = len - 2;i > 0;i -- )
    {
        if(str[i] == ' ')
        {
            j = i + 1;
            while(str[j] != ' ')
            {
            	if(j == len -1)
            		break;
            	printf("%c",str[j]);
            	j ++;
            }                       
            printf(" ");
        }
    }
    while(str[i] != ' ')
    {
        printf("%c",str[i]);
        i ++;
    }

    return 0;
}

相關推薦

英文單詞輸出

題目:將一個英文語句以單詞為單位逆序排放。例如“I am a boy”,逆序排放後為“boy a am I”所有單詞之間用一個空格隔開,語句中除了英文字母外,不再包含其他字元  思路1:每次輸出一個單詞後,str_len就減少該單詞的長度 心得體會:用fputs()輸入的

英文句子中多餘的空格去掉

#include<iostream> #include<string> using namespace std; int main(){ string str; int i,len,flag=0; getline(cin,str);

字串反轉(單詞輸出),保留並列印所有空格。

這是一個作業,用了好久寫出來的,寫完後,感覺我的思維方式不適合學程式設計。。。#字串按單詞反轉(必須保留所有空格) a='Jane love China!'#把字串存入列表 #a=input('請輸入字串(用引號引起來,,,):') li=list()#用來存放空格所在位置的

鍵盤輸入英文 每個單詞的首字母大寫

在python中,有程式碼可以直接實現此功能str2 = "hello nice To meet Youprint(str2.title()) 還有幾個相似的功能#將字串中的大寫轉化成小寫 小寫轉化成大寫 print(str2.swapcase()) #就第一個首字母大寫

Java中判斷英文中有多少個以p開頭的單詞

package Pak01; public class TestString { public static void main(String[] args) {      String s="peter piper picked a peck of pickled p

一個英文句子的單詞輸出到另一個檔案,單詞內容不倒

#include<stdio.h> #include<string.h> void reverse(char* buf,char* b) { int i=0; int j,k=0; for(i=strlen

2011-10-30---輸入單詞單詞輸出

    單詞是以空格為間隔的,仔細消化譚浩強書上的例題,可以成功解決問題。 【輸入】                源字串。 【輸出】                逆序後的字串。 【返回值】               單詞個數。 #include <stdio

C++實現英文句子中的單詞逆置

去騰訊面試的時候被問到這個題,當時說了兩種方法,但是都需要移動元素,且需要輔助空間,面試官不太滿意。其實這道題不難,只能怪我太笨嘍。後來面試官告訴了我答案,確實是不錯的方法,只用了一點小技巧就很容易的實現了,回來之後我就用程式碼實現了下,寫到這裡分享一下,也當是給自己長記性

總有英文,能夠溫暖到你!

art from 技術分享 ant when my life png people webp I will make you happy when you are depressed. I will make you delighted when you a

段(英文)按照字元出現的頻率進行倒敘排列

1、文章Newly-added concrete barriers and steel posts can be seen alongside the bike path, and more will be installed in the coming months across the city,

段(英文)按照字符出現的頻率進行倒敘排列

concrete 2.3 block new mis 拆分 and see cross 1、文章 Newly-added concrete barriers and steel posts can be seen alongside the bike path, and

英文中文

CS-Computer Science CAP Consistency(一致性), 資料一致更新,所有資料變動都是同步的Availability(可用性), 好的響應效能Partition tolerance(分割槽容忍性) 可靠性定理:任何分散式系統只可同時滿足二點,沒法三者兼顧。

演算法:輸入任意一個4位數整數,該數輸出(例如:輸入1354,輸出4531)

程式分析:可以用取餘符來獲取這個四位數的每一位,然後反序重新組合輸出。 #include "stdio.h" int main(void) { int num = 0, opnum = 0; in

【c語言】個數的二進位制序列逆,然後輸出之後的二進位制,所對應的數

<pre name="code" class="cpp">// 將一個數的二進位制序列逆序,然後輸出逆序之後的二進位制序,所對應的數 #include <stdio.h> // 從原數拿出最低位,放到mid中,mid左移,原數右移 int r

輸入一個int型整數,其逆輸出,每個數字後有一個空格。 n其逆輸出,每個數字後有一個空格,輸出佔一行。例如,輸入12354,輸出4 5 3 2 1

#include<stdio.h>#include<math.h> int main(){ int n,a; scanf("%d",&n); while(1) { if(n>=10)  {  a=n%10;  n=n/10;  print

C語言 字串輸出

#include <stdio.h> #include <string.h> void rev(char *buf, int size) { int i = 0; int temp; fo

個數組從小到大的順序排列

在公司等開發板,只能寫寫簡單的java程式碼來補補腦。程式碼參考了java語言程式設計-基礎篇 需求:將陣列按從小到大的順序排列 不多說,直接上程式碼: 1.按照選擇排序的方法進行排序 public

程式設計小練習:最大公約數,字串輸出,全排列,不用加減法求和,字串內容,字串中最長數字串,陣列是否遞增,陣列反轉,連結串列反轉,翻轉單詞順序

最大公約數 --- 遞迴、非遞迴 #include <stdio.h> int gcd(int a, int b); int gcd_recursive(int a, int b); int main(int argc, char *argv[]) {

C8輸出

任意輸入四位十六進位制整數,以反序的方式輸出該十六進位制數,如輸入9AF0,則輸出0FA9 #include<stdio.h> void main() { int a, b, c, d, n; printf("輸入四位的十六進位制整數"); scanf("%X", &n)

輸出字串

題目描述 輸入任意4個字元(如:abcd), 並按反序輸出(如:dcba) 輸入描述: 題目可能包含多組用例,每組用例佔一行,包含4個任意的字元。 輸出描述: 對於每組輸入,請輸出一行反序後的字串。 具體可見樣例。 示例1 輸入 Upin cvYj WJpw