2013年6月28日金曜日

開発環境

プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)の第1章(やさしい入門)、1.9(文字配列)の演習1-19を解いてみる。

その他参考書籍

演習 1-19.

コード

sample.c

#include <stdio.h>
#define MAXLINE 1000

int my_getline(char line[], int maxline);
int remove_last_blank_and_tab(char line[]);
void reverse(char s[]);

int main()
{
    char line[MAXLINE];
    
    while (my_getline(line, MAXLINE) > 0) {
        reverse(line);
        printf("%s", line);
    }
    return 0;
}

int my_getline(char s[], int lim)
{
    int c, i;
    
    for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i) {
        s[i] = c;
    }
    if (c == '\n') {
        s[i] = c;
        ++i;
    }
    s[i] = '\0';
    return i;
}

void reverse(char s[])
{
    int i, j;
    char c;
    
    i = 0;
    while (s[i] != '\0') {
        i++;
    }
    i--;
    if (s[i] == '\n') {
        i--;
    }
    j = 0;
    while (j < i) {
        c = s[j];
        s[j] = s[i];
        s[i] = c;
        j++;
        i--;
    }
}

入出力結果(Terminal)

$ ./a.out < sample.c
>h.oidts< edulcni#
0001 ENILXAM enifed#

;)enilxam tni ,][enil rahc(enilteg_ym tni
;)][enil rahc(bat_dna_knalb_tsal_evomer tni
;)][s rahc(esrever diov

)(niam tni
{
;]ENILXAM[enil rahc    
    
{ )0 > )ENILXAM ,enil(enilteg_ym( elihw    
;)enil(esrever        
;)enil ,"s%"(ftnirp        
}    
;0 nruter    
}

)mil tni ,][s rahc(enilteg_ym tni
{
;i ,c tni    
    
{ )i++ ;'n\' =! c && FOE =! ))(rahcteg = c( && 1 - mil < i ;0 = i( rof    
;c = ]i[s        
}    
{ )'n\' == c( fi    
;c = ]i[s        
;i++        
}    
;'0\' = ]i[s    
;i nruter    
}

)][s rahc(esrever diov
{
;j ,i tni    
;c rahc    
    
;0 = i    
{ )'0\' =! ]i[s( elihw    
;++i        
}    
;--i    
{ )'n\' == ]i[s( fi    
;--i        
}    
;0 = j    
{ )i < j( elihw    
;]j[s = c        
;]i[s = ]j[s        
;c = ]i[s        
;++j        
;--i        
}    
}
$ ./a.out < sample.txt
eripsnoc etaF htiw I dna uoy dluoc !evoL hA
,eritne sgnihT fo emehcS yrros siht psarg oT
neht dna -- stib ot ti rettahs ew ton dluoW
!eriseD s'traeH eht ot reraen ti dluom-eR
eripsnoc etaf htiw i dna uoy dluoc !evol ha
,eritne sgniht fo emehcs yrros siht psarg ot
neht dna -- stib ot ti rettahs ew ton dluow
!erised s'traeh eht ot reraen ti dluom-er
eripsnoc etaF htiw I dna uoy dluoc !evoL hA%$#@!
,eritne sgnihT fo emehcS yrros siht psarg oT%$#@!
neht dna -- stib ot ti rettahs ew ton dluoW%$#@!
!eriseD s'traeH eht ot reraen ti dluom-eR%$#@!
eripsnoc etaf htiw i dna uoy dluoc !evol ha%$#@!
,eritne sgniht fo emehcs yrros siht psarg ot%$#@!
neht dna -- stib ot ti rettahs ew ton dluow%$#@!
!erised s'traeh eht ot reraen ti dluom-er%$#@!
$ cat sample.txt
Ah Love! could you and I with Fate conspire
To grasp this sorry Scheme of Things entire,
Would not we shatter it to bits -- and then
Re-mould it nearer to the Heart's Desire!
ah love! could you and i with fate conspire
to grasp this sorry scheme of things entire,
would not we shatter it to bits -- and then
re-mould it nearer to the heart's desire!
!@#$%Ah Love! could you and I with Fate conspire
!@#$%To grasp this sorry Scheme of Things entire,
!@#$%Would not we shatter it to bits -- and then
!@#$%Re-mould it nearer to the Heart's Desire!
!@#$%ah love! could you and i with fate conspire
!@#$%to grasp this sorry scheme of things entire,
!@#$%would not we shatter it to bits -- and then
!@#$%re-mould it nearer to the heart's desire!
$

0 コメント:

コメントを投稿