第11章 字符串和字符串函數(shù)

練習(xí)題

char * s_gets(char * st, int n)
{
    char * ret_val;

    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
        while (*st != '\n' && *st != '\0')
            st++;
        if (*st == '\n')
            *st = '\0';
        else 
            while (getchar() != '\n')
                continue;
        
    }
    
    return ret_val;
}

設(shè)計(jì)并測(cè)試一個(gè)函數(shù)富雅,從輸入中獲取下n個(gè)字符(包括空白,制表符陨收、換行符)饭豹,把結(jié)果存儲(chǔ)在一個(gè)數(shù)組里,他的地址唄船體作為一個(gè)參數(shù)

#include <stdio.h>
#define SIZE    40

void * s_gets(char * st, int n);

int main(void)
{
    char st[SIZE];
    puts("請(qǐng)輸入:");
    s_gets(st, SIZE);
    puts(st);
    
    return 0;
}

void * s_gets(char * st, int n)
{
    fgets(st, n, stdin);
}

修改并編程練習(xí)1的函數(shù)畏吓,在n個(gè)字符后停止墨状,或在讀到第一個(gè)空白、制表符或換行符時(shí)停止菲饼,哪個(gè)先遇到哪個(gè)停止肾砂。不能只使用scanf()

#include <stdio.h>

void sgets(char * st, int n);

void main(void)
{
    char str[100];
    int n;
    printf("請(qǐng)輸入字符數(shù)量:");
    scanf("%d", &n);
    while (getchar() != '\n')
        continue;
    
    printf("請(qǐng)輸入字符:");
    sgets(str, n);
    puts(str);
}

void sgets(char * st, int n)
{
    int i;
    for (i = 0; i < n; i++)
    {
        st[i] = getchar();
        if (st[i] == '\n' || st[i] ==' ' || st[i] == '\t')
        {
            
            break;
        }
    }
    st[i] = '\0';
}

3

#include <stdio.h>
#define SIZE    40

void sgets(char * st);

int main(void)
{
    char st[SIZE];
    printf("請(qǐng)輸入字符:");
    sgets(st);
    puts(st);
}

void sgets(char * st)
{
    scanf("%s", st);
    while (getchar() != '\n')
        continue;
    
}

4

#include <stdio.h>
#include <ctype.h>
void sgets(char * st, int n);
void main(void)
{
    char st[100];
    int num;
    printf("請(qǐng)輸入字符數(shù)量:");
    scanf("%d", &num);
    printf("請(qǐng)輸入字母:");
    sgets(st, num);
    puts(st);
}

void sgets(char * st, int n)
{
    int i = 0;
    while (iswspace(st[i] = getchar()))
    {
        continue;
    }
    for ( i = 1; i < n; i++)
    {
        st[i] = getchar();
        if (iswspace(st[i]))
        {
            st[i] = '\0';
            break;
        }
        
    }
    while (getchar() != '\n')
        continue;
    
}

5

#include <stdio.h>
#include <string.h>
#define SIZE 100

char *strchrr(char * st, char chr);

void main(void)
{
    char st[SIZE];
    char ch;
    int i;
    char * fin;
    printf("請(qǐng)輸入要查找的字符串:");
    fgets(st, SIZE, stdin);
    printf("請(qǐng)輸入查找的字符:");
    ch = getchar();
    fin = strchrr(st, ch);
    if (fin)
    {
        printf("找到了\n");
    }
    else
    {
        puts("沒找到");
    }
    
}

char *strchrr(char * st, char  chr)
{
    int x, i;
    x = strlen(st);
    for ( i = 0; i < x; i++)
    {
        if (st[i] == chr)
        {
            return &st[i];
        }
        
    }
    return NULL;
}

編寫一個(gè)名為is_within()的函數(shù),接受一個(gè)字符和一個(gè)指向字符串的指針作為兩個(gè)函數(shù)的形參宏悦。如果指定字符在字符串中镐确,該函數(shù)返回一個(gè)非零值(真),否則返回0.在一個(gè)完整的程序中測(cè)試該函數(shù)饼煞,使用一個(gè)循環(huán)給函數(shù)提供輸入值

#include <stdio.h>
#include <string.h>
#define SIZE    100

int find(char ch, char * st);
void s_gets(char * st, int n);
char get_ch(void);

void main(void)
{
    char st[SIZE];
    char ch;
    int x;
    printf("請(qǐng)輸入查找的字符串:");
    s_gets(st, SIZE);
    while ((ch = get_ch()) != 'q')
    {
        x = find(ch, st);
        if (x)
        {
            puts("找到了");
        }
        else
            puts("沒找到");
        }
    
}

void s_gets(char * st, int n)
{
    char * ret_val;
    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
        while (*st != '\n' && *st != '\0')
            st++;
        if (*st == '\n')
            *st = '\0';
        else
            while (getchar() != '\n')
                continue;
    }
    
    
}

int find(char ch, char * st)
{
    int i, x;

    x = strlen(st);
    for ( i = 0; i < x; i++)
    {
        if (st[i] == ch)
            return 1;
    }
    
    return 0;
}

char get_ch(void)
{
    char ch;
    printf("請(qǐng)輸入字符:");
    ch = getchar();
    while (getchar() != '\n')
        continue;
    return ch;
}

7

#include <stdio.h>
#include <string.h>
char * mystrncpy(char * st, char * source, int n);

void main(void)
{
    char string[20];
    char source[40];
    printf("請(qǐng)輸入:");
    fgets(source, 40, stdin);
    mystrncpy(string, source, 20 - strlen(string));
    puts(string);
}

char * mystrncpy(char * st, char * source, int n)
{
    int i;
    for ( i = 0; i < n; i++)
    {
        st[i] = source[i];
    }
    st[19] = '\0'; 
    return &st[0];
}

8

#include <stdio.h>
#include <string.h>
char * find(char * st, char * source);

void main(void)
{
    char st[40];
    char ch[40];
    char * p;
    printf("請(qǐng)輸入字符串:");
    fgets(st, 40, stdin);
    printf("請(qǐng)輸入字符:");
    fgets(ch, 40, stdin);
    p = find(st, ch);
    if (p)
    {
        printf("在第%d個(gè)位置", (p - st) + 1);
    }
    else
    {
        puts("沒有");
    }
    
}
char * find(char * st, char * source)
{
    int i, x;
    x = strlen(st);
    for ( i = 0; i < x; i++)
    {
        if (source[0] == st[i])
            return &st[i];
    }
    return NULL;
    
}

9

#include <stdio.h>
#include <string.h>
#define SIZE    40

char * sggets(char * st, int n);
void stsrt(char * str);

void main(void)
{
    char st[SIZE];
    char * p;
    printf("請(qǐng)輸入:");
    while (sggets(st, SIZE) != NULL && st[0] != '\0')
    {
        p = st;
        stsrt(p);
        puts(p);
    }
    
}


char * sggets(char * st, int n)
{
    char * ret_val;
    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
        while (*st != '\n' && *st != '\0')
            st++;
        if (*st == '\n')
            *st = '\0';
        else
            while (getchar() != '\n')
                continue;
        
    }
    return ret_val;
    
}

void stsrt(char * str)
{
    int x, i;
    char temp;
    x = strlen(str);
 
    for (i = 0; i < x / 2; i++)
    {    
        temp = str[i];
        str[i] = str[x - i - 1];
        str[x - i - 1] = temp;
       
    }
    
}

10

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define SIZE    10

void del(char * st);
char * s_gets(char * st, int n);

void main(void)
{
    char str[SIZE];


    while ( s_gets(str, SIZE) != NULL && str[0] != '\0')
    {
        del(str);
        puts(str);
    }
     
}

char * s_gets(char * st, int n)
{
    char * ret_val;
    printf("請(qǐng)輸入字符串:");
    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
        while (*st != '\n' && *st != '\0')
            st++;
        if (*st == '\n')
            *st = '\0';
        else
            while (getchar() != '\n')
                continue;
            
    }
    
}

void del(char * st)
{
    char linshi[SIZE];
    int i, j, gross;
    
    gross = strlen(st);
    for ( i = 0, j = 0; i <= gross; i++)
    {
        if (!isspace(st[i]))
        {
            linshi[j] = st[i];
            j++;
        }
    }
    i = 0;
    while (st[i] != '\0')
    {
        st[i] = '\0';
        i++;
    }

    gross = strlen(linshi);
    for ( i = 0; i < gross; i++)
    {
        st[i] = linshi[i];
    }
    
    
}

11

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define SIZE    11
#define SIGN    "*********************************"

char * s_gets(char * st, int n);
char get_choice(void);
char get_first(void);

void main(void)
{
    char str[SIZE];
    char ch;
    int i;
    s_gets(str, SIZE);

    while ((ch = get_choice()) != 'q')
    {
        switch (ch)
        {
        case 'a':
            puts(str);
            break;
        case 'b':
            for ( i = 0; i < strlen(str); i++)
                printf("%d ", str[i]);
            printf("\n");
            break;
        case 'c':
            puts("xiangbudaoba");
            break;
        case 'd':
            puts("womexie");
            break;
        default:
            puts("error");
            break;
        }
    }
     
}

char get_choice(void)
{
    char ch;
    printf("%s%s\n", SIGN, SIGN);
    puts("a) 打印字符串列表                 b) 以ASCII中的順序打印字符串");
    puts("c) 按長(zhǎng)度遞增順序打印字符串        d) 按自負(fù)床中第一個(gè)單詞的長(zhǎng)度打印字符串");
    puts("q) 退出");
    ch = get_first();
    while (ch < 'a' || ch > 'd' && ch != 'q')
    {
        printf("請(qǐng)輸入a, b, c, d, q:");
        ch = get_first();
    }
    return ch;

}

char get_first(void)
{
    char ch;
    ch = getchar();
    while (getchar() != '\n')
    {
        continue;
    }
    
    return ch;
}

char * s_gets(char * st, int n)
{
    char * ret_val;
    printf("請(qǐng)輸入字符串:");
    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
        while (*st != '\n' && *st != '\0')
            st++;
        if (*st == '\n')
            *st = '\0';
        else
            while (getchar() != '\n')
                continue;
            
    }
    
}

13

#include <stdio.h>

int main(int argc, char const *argv[])
{
    int count;
    count = argc - 1;
    for ( ; count > 0; count-- )
    {
        printf("%s ", argv[count]);
    }
    

    return 0;
}

15

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

char * s_gets(char * st, int n);
void ToUpper(char * st);
void ToLow(char * st);
int main(int argc, char const *argv[])
{
    char st[100];
    s_gets(st, 100);
    if (argc > 1)
    {
        if (strcmp(argv[1], "-p") == 0)
        puts(st);
        else if(strcmp(argv[1], "-u") == 0)
        {   
            ToUpper(st);
            puts(st);
        }
        else if(strcmp(argv[1], "-l") == 0)
        {
            ToLow(st);
            puts(st);
        }
        
    }
    else
        puts(st);
    return 0;
}

void ToUpper(char * st)
{
    int i;
    
    for ( i = 0; i < strlen(st); i++)
    {
        st[i] = toupper(st[i]);
    }
    
}

void ToLow(char * st)
{
    int i;
    
    for ( i = 0; i < strlen(st); i++)
    {
        st[i] = tolower(st[i]);
    }
    
}

char * s_gets(char * st, int n)
{
    char * ret_val;
    printf("請(qǐng)輸入字符串:");
    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
        while (*st != '\n' && *st != '\0')
            st++;
        if (*st == '\n')
            *st = '\0';
        else
            while (getchar() != '\n')
                continue;
            
    }
    
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末源葫,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子砖瞧,更是在濱河造成了極大的恐慌息堂,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,386評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件块促,死亡現(xiàn)場(chǎng)離奇詭異荣堰,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)竭翠,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門振坚,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人斋扰,你說我怎么就攤上這事渡八。” “怎么了传货?”我有些...
    開封第一講書人閱讀 164,704評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵屎鳍,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我问裕,道長(zhǎng)哥艇,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,702評(píng)論 1 294
  • 正文 為了忘掉前任僻澎,我火速辦了婚禮貌踏,結(jié)果婚禮上十饥,老公的妹妹穿的比我還像新娘。我一直安慰自己祖乳,他們只是感情好逗堵,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,716評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著眷昆,像睡著了一般蜒秤。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上亚斋,一...
    開封第一講書人閱讀 51,573評(píng)論 1 305
  • 那天作媚,我揣著相機(jī)與錄音,去河邊找鬼帅刊。 笑死纸泡,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的赖瞒。 我是一名探鬼主播女揭,決...
    沈念sama閱讀 40,314評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼栏饮!你這毒婦竟也來了吧兔?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,230評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤袍嬉,失蹤者是張志新(化名)和其女友劉穎境蔼,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體伺通,經(jīng)...
    沈念sama閱讀 45,680評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡欧穴,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,873評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了泵殴。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,991評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡拼苍,死狀恐怖笑诅,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情疮鲫,我是刑警寧澤吆你,帶...
    沈念sama閱讀 35,706評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站俊犯,受9級(jí)特大地震影響妇多,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜燕侠,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,329評(píng)論 3 330
  • 文/蒙蒙 一者祖、第九天 我趴在偏房一處隱蔽的房頂上張望立莉。 院中可真熱鬧,春花似錦七问、人聲如沸蜓耻。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,910評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽刹淌。三九已至,卻和暖如春讥耗,著一層夾襖步出監(jiān)牢的瞬間有勾,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,038評(píng)論 1 270
  • 我被黑心中介騙來泰國(guó)打工古程, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留蔼卡,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,158評(píng)論 3 370
  • 正文 我出身青樓籍琳,卻偏偏與公主長(zhǎng)得像菲宴,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子趋急,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,941評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • 葉子開口說某地如一棵樹 我眼前的景色便開始結(jié)果 一顆一顆 貯藏于心窩 停止保鮮 葉子說那種高度夜間恐懼癥至今為止...
    瑾懿閱讀 205評(píng)論 0 0
  • 東琴/文 當(dāng)你愛了喝峦, 空氣中彌漫著甜甜的味道, 就像月桂飄香的季節(jié)呜达, 連笑容也是蜜一般的甜谣蠢; 當(dāng)你愛了, 他的一顰...
    東琴閱讀 220評(píng)論 5 4
  • 我只是一個(gè)女人查近,受了委屈能做的只有哭 覺得自己好沒用眉踱。明明知道哭解決不了任何問題,可是還是沒出息的哭了 老公給我打...
    ss珊珊來了閱讀 179評(píng)論 0 0
  • 一霜威、現(xiàn)狀 畢業(yè)幾天以來谈喳,在激情余韻的推動(dòng)下,自己依然保持了精進(jìn)的練習(xí)狀態(tài)——每天5:30起床戈泼,6:00開始練習(xí)婿禽,7...
    kevali閱讀 516評(píng)論 0 2