c primer plus 11 编程练习

 

1、

#include <stdio.h>

#define SIZE 100

void get_n(char * ar, int n);

int main(void)
{
    int n;
    printf("n = "); scanf("%d", &n);
    
    while(getchar() != '\n')
        continue;
    
    char st1[SIZE];
    
    get_n(st1, n);
    
    
    puts(st1);
    int i;
    for(i = 0; i < n; i++)
    {
        printf("st1[%d]: %c\n", i, st1[i]);
    }

    return 0;
}

void get_n(char * ar, int n)
{
    int i;
    
    puts("please input the elements of the strings.");
    for(i = 0; i < n; i++)
    {
        scanf("%c", &ar[i]);
    }
    
}

 

#include <stdio.h>
#define SIZE 40

char * read_char(char *st, int n);

int main(void)
{
    char test[SIZE];
    puts("Start to test function, enter a string.");
    
    read_char(test, 5);
    
    puts("The string you input is: ");
    puts(test);
    
    return 0;
}

char * read_char(char *st, int n)
{
    int i = 0;
    do
    {
        st[i] = getchar();
    }
    while(st[i] != EOF && ++i < n);
    
    return 0;
}

 

2、

#include <stdio.h>
#define SIZE 40

char * read_char(char *st, int n);

int main(void)
{
    char test[SIZE];
    
    puts("Start to test function, enter a string.");
    read_char(test, 5);
    
    puts("The string you input is:");
    puts(test);
    
    return 0;
}

char * read_char(char *st, int n)
{
    int i = 0;
    
    do
    {
        st[i] = getchar();
        if(st[i] == '\n' || st[i] == '\t' || st[i] == ' ')
            break;
    }
    while(st[i] != EOF && ++i < n);
    
    return st;
}

 

3、

#include <stdio.h>

#define SIZE 100

char * get_word(char *st, int n);

int main(void)
{
    char word[SIZE];
    char * temp;
    
    temp = get_word(word, SIZE);
    
    puts(temp);
    
    return 0;    
} 

char * get_word(char *st, int n)
{
    char * temp;
    int i = 0;
    
    temp = fgets(st, n, stdin);

    if(temp)
    {
        if(st[0] == ' ')
        {
            while(st[i] == ' ')
                i++;
        }
    }
    
    temp += i;
    
    while(st[i] != ' ' && st[i] != '\t' && st[i] != '\n')
        i++;
    
    st[i] = '\0';
    
    return temp;
}

 

#include <stdio.h>

#define SIZE 100

char * get_word(char *st, int n);

int main(void)
{
    char word[SIZE];
    char * temp;
    
    temp = get_word(word, SIZE);
    
    puts(temp);
    
    return 0;    
} 

char * get_word(char *st, int n)
{
    char * temp;
    int i = 0;
    
    temp = fgets(st, n, stdin);

    if(temp)
    {
        if(st[0] == ' ')
        {
            while(st[i] == ' ')
                i++;
        }
    }
    
    temp += i;
    
    while(st[i] != ' ' && st[i] != '\t' && st[i] != '\n')
        i++;
    
    if(st[i] == ' ' || st[i] == '\t' || st[i] == '\n')
        st[i] = '\0';
    else
        while(getchar() != '\n')
            continue;
            
    return temp;
}

 

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

#define SIZE 80

char * get_word(char *out);

int main(void)
{
    char output[SIZE];
    get_word(output);
    
    printf("the first word you input is: %s", output);
    
    return 0;
}

char * get_word(char *out)
{
    char input[SIZE];
    char *in = input;
    
    puts("enter a string: ");
    fgets(input, SIZE, stdin);
    
    while((*in == '\n' || *in == '\t' || *in == ' ') && *in != '\0')
        in++;
    
    while(*in != '\n' && *in != '\t' && *in != ' ' && *in != '\0')
        *out++ = *in++;
    
    return out;
}

 

#include <stdio.h>

#define SIZE 100

char * get_word(char *st, int n);
int main(void)
{
    char word[SIZE];
    char *tmp;
    
    tmp = get_word(word, SIZE);
    
    puts(tmp);
    
    return 0;
}

char * get_word(char *st, int n)
{
    char *tmp;
    int i = 0;
    
    tmp = fgets(st, n, stdin);
    
    if(tmp)
    {
        while((st[i] == ' ' || st[i] == '\t' || st[i] == '\n') && st[i] != '\0')
            i++;
        
        tmp += i;
        
        while(st[i] != ' ' && st[i] != '\t' && st[i] != '\n' && st[i] != '\0' )
            i++;
        
        if(st[i] == ' ' || st[i] == '\t' || st[i] == '\n')
            st[i] = '\0';
        else
            while(getchar() != '\n')
                continue;    
    }
    
    return tmp;
}

 

#include <stdio.h>

#define SIZE 100

char * get_word(char *st, int n);

int main(void)
{
    char st1[SIZE];
    char * fword;
    
    printf("please input the strings.\n");
    
    fword = get_word(st1, SIZE);
    
    printf("first word: %s\n", fword);
    
    return 0;
}

char * get_word(char *st, int n)
{
    char * temp;
    int i = 0;
    
    temp = fgets(st, n, stdin);
    
    if(temp)
    {
        while((st[i] == ' ' || st[i] == '\t' || st[i] == '\n') && st[i] != '\0')
            i++;
        
        temp += i;
        
        while(st[i] != ' ' && st[i] != '\t' && st[i] != '\n' && st[i] != '\0')
            i++;
        
        if(st[i] == ' ' || st[i] == '\t' || st[i] == '\n')
            st[i] = '\0';
        else
            while(getchar() != '\n')
                continue;
    }
    
    return temp;
}

 

4、

#include <stdio.h>

#define SIZE 100

char * get_word(char *st, int n);

int main(void)
{
    char st1[SIZE];
    char * fword;
    
    printf("please input the strings.\n");
    
    fword = get_word(st1, SIZE);
    
    printf("first word: %s\n", fword);
    
    return 0;
}

char * get_word(char *st, int n)
{
    char * temp;
    int i = 0;
    
    temp = fgets(st, n, stdin);
    
    if(temp)
    {
        while((st[i] == ' ' || st[i] == '\t' || st[i] == '\n') && st[i] != '\0')
            i++;
        
        temp += i;
        
        while(st[i] != ' ' && st[i] != '\t' && st[i] != '\n' && st[i] != '\0')
            i++;
        
        if(st[i] == ' ' || st[i] == '\t' || st[i] == '\n')
            st[i] = '\0';
        else
            while(getchar() != '\n')
                continue;
    }
    
    return temp;
}

 

5、

#include <stdio.h>

#define SIZE 100

char * search_ch(char * ar, char ch);
char * s_gets(char *st, int n);


int main(void)
{
    char st1[SIZE];
    char * find;
    char ch;
    char xx;
    
    do
    {
        puts("input the string: ");
        s_gets(st1, SIZE);
        
        printf("please input the ch: "); scanf("%c", &ch); 
        find = search_ch(st1, ch);
        if(find)
            puts(find);
        else
            puts("nothing found!");
        
        while(getchar() != '\n')
            continue;    
        printf("quit(q to quit!); other continue xx: "); scanf("%c", &xx);
        while(getchar() != '\n')
            continue;
    }
    while(xx != 'q');
    
    return 0;
}

char * search_ch(char * ar, char ch)
{
    int i = 0;
    char * find;
    
    while(ar[i] != ch && ar[i] != '\0')
        i++;
    
    if(ar[i] == ch)
        find = ar + i;
    else
        find = NULL;
        
    return find;
}

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

 

#include <stdio.h>

#define SIZE 81

char * get_pos(char *st, char ch);

int main(void)
{
    char st1[SIZE];
    printf("st1: \n");
    fgets(st1, SIZE, stdin);
    char des = ' ';
    char *pos;
    
    printf("please input the dest.\n");
    
    while(des != EOF)
    {    
        printf("intput a ch or EOF to quit\n");
        while((des = getchar()) == '\n')
            continue;
        
        if((pos = get_pos(st1, des)) != NULL)
            printf("%c first in %p\n", *pos, pos);
        
        else
            printf("nothing fonud.\n");
        printf("go on or quit?"); 
        
    }
    return 0;
}

char * get_pos(char *st, char ch)
{
    while(*st != '\0')
    {
        if(*st == ch)
            return st;
        else
            st++;
    }
    return NULL;
}

 

6、

#include <stdio.h>

#define SIZE 100

int find_ch(char ch, char * ar);

int main(void)
{
    char st1[SIZE];
    char target;
    
    printf("please iput the strings: ");
    fgets(st1, SIZE, stdin);
        
    while(1)
    {
        printf("please input target character(EOF to quit): ");
        target = getchar();
        
        if(target == EOF)
            break;
            
        while(getchar() != '\n')
            continue;
            
        if(find_ch(target, st1) != 0)
        {
            printf("find the target!\n\n");
        }
        else
        {
            printf("not fond the target!\n\n");
        }
        puts("go on or quit? ");
    }
}

int find_ch(char ch, char * ar)
{
    while(*ar != '\0')
    {
        if(*ar == ch)
            return 1;
        else
            ar++;
    }
    return 0;
}

 

6、

#include <stdio.h>

#define SIZE 100

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

int main(void)
{
    char st1[SIZE];
    puts("please input the strings.");
    s_gets(st1, SIZE);
    char target;
    
    while(1)
    {
        printf("please input the target character(EOF to quit).\n");
        target = getchar();
        
        if(target == EOF)
            break;
        
        while(getchar() != '\n')
            continue;
        
        if(find(target, st1) != 0)
        {
            puts("find the target!\n");    
        } 
        else
        {
            puts("not find the target!\n");
        }
        puts("go on or quit? ");
    }
    
        
}

int find(char ch, char *ar)
{
    while(*ar)
    {
        if(*ar == ch)
            return 1;
        else
            ar++;
    }
    return 0;
}


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

 

7、

#include <stdio.h>

#define SIZE 80

char * mystrncpy(char * ar1, char * ar2, int n);
char *s_gets(char *st, int n);

int main(void)
{
    char st1[SIZE];
    char st2[SIZE];
    
    printf("input source string: ");
    s_gets(st2, SIZE);
    int num;
    printf("input  copy  number: "); scanf("%d", &num);
    
    mystrncpy(st1, st2, num);
    printf("string copy  result: %s\n", st1);
    
    return 0;
}

char * mystrncpy(char * ar1, char * ar2, int n)
{
    int i = 0;
    
    while(i < n &&  ar2[i] != '\0')
    {
        ar1[i] = ar2[i];
        i++;
    }
    
    if(i < n)
    {
        while(i < n)
        {
            ar1[i] = '\0';
            i++;
        }
    }
    
    return ar1;
}

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

 

 

循环:

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

#define SIZE 80

char * mystrncpy(char * ar1, char * ar2, int n);
char *s_gets(char *st, int n);

int main(void)
{
    char st1[SIZE];
    char st2[SIZE];
    
    while(1)
    {
        printf("input source string(q to quit): ");
        s_gets(st2, SIZE);
        if(strcmp(st2, "q") == 0)
            break;
        int num;
        printf("input  copy  number: "); scanf("%d", &num);
        
        mystrncpy(st1, st2, num);
        printf("string copy  result: %s\n", st1);
        
        while(getchar() != '\n')
            continue;
    }
    
    return 0;
}

char * mystrncpy(char * ar1, char * ar2, int n)
{
    int i = 0;
    
    while(i < n &&  ar2[i] != '\0')
    {
        ar1[i] = ar2[i];
        i++;
    }
    
    if(i < n)
    {
        while(i < n)
        {
            ar1[i] = '\0';
            i++;
        }
    }
    
    return ar1;
}

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

 

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

#define SIZE 80

char * mystrncpy(char * ar1, char * ar2, int n);
char *s_gets(char *st, int n);

int main(void)
{
    char st1[SIZE];
    char st2[SIZE];
    char ch;
    
    while(1)
    {
        printf("input source string(EOF to quit): ");
        if((ch = getchar()) == EOF)
            break;
            
        s_gets(st2, SIZE);
        int num;
        printf("input  copy  number: "); scanf("%d", &num);
        
        mystrncpy(st1, st2, num);
        printf("string copy  result: %s\n", st1);
        
        while(getchar() != '\n')
            continue;
    }
    
    return 0;
}

char * mystrncpy(char * ar1, char * ar2, int n)
{
    int i = 0;
    
    while(i < n &&  ar2[i] != '\0')
    {
        ar1[i] = ar2[i];
        i++;
    }
    
    if(i < n)
    {
        while(i < n)
        {
            ar1[i] = '\0';
            i++;
        }
    }
    
    return ar1;
}

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

 

8、

#include <stdio.h>

#define SIZE 80

char *s_gets(char *st, int n);
char *scan(char *ar1, char *ar2);

int main(void)
{
    char st1[SIZE];
    char st2[SIZE];
    printf("st1: "); s_gets(st1, SIZE);
    printf("st2: "); s_gets(st2, SIZE);
    char * scan_re;
    
    scan_re = scan(st1, st2);
    if(scan_re)
    {
        puts("\noverlapping!");
        puts(scan_re);
    }
    else
        puts("\nnot overlapping!");
        
    return 0;
}

char *scan(char *ar1, char *ar2)
{
    int i = 0;
    while(ar1[i] != '\0')
    {
        if(ar1[i] == ar2[0])
        {
            int j = 0;
            while(ar1[i + j] == ar2[j] && ar2[j] != '\0')
                j++;
            if(ar2[j] == '\0')
                return ar1;
        }
        i++;
    }
    return NULL;
}


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

 

#include <stdio.h>

#define SIZE 100

char * s_gets(char *st, int n);
char *scan(char *ar1, char *ar2);

int main(void)
{
    char st1[SIZE];
    char st2[SIZE];
    char * scan_re;
    
    printf("please input st1: "); s_gets(st1, SIZE);
    printf("please input st2: "); s_gets(st2, SIZE);
    scan_re = scan(st1, st2);
    
    if(scan_re)
    {
        puts("overlapping!");
        puts(scan_re);
    }
    else
    {
        puts("not overlapping!");
    }
    
    return 0;
} 

char *scan(char *ar1, char *ar2)
{
    int i = 0;
    
    while(ar1[i] != '\0')
    {
        if(ar1[i] == ar2[0])
        {
            int j = 0;
            while(ar1[i + j] == ar2[j] && ar2[j] != '\0')
                j++;
            if(ar2[j] == '\0')
                return ar1 + i;
        }
        i++;
    }
    return NULL;
}

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

 

 

#include <stdio.h>

#define SIZE 100

char * s_gets(char *st, int n);
char *scan(char *ar1, char *ar2);

int main(void)
{
    char st1[SIZE];
    char st2[SIZE];
    char * scan_re;
    char temp;
    
    while(1)
    {
        
        printf("please input st1: ");
        s_gets(st1, SIZE);
        
        printf("please input st2: "); s_gets(st2, SIZE);
        scan_re = scan(st1, st2);
        
        if(scan_re)
        {
            puts("overlapping!\n");
            puts(scan_re);
        }
        else
        {
            puts("not overlapping!\n");
        }
        printf("y continue or EOF to quit): ");
        if((temp = getchar()) == EOF)
            break;
        else
            while(getchar() != '\n')
                continue;
    }
    return 0;
} 

char *scan(char *ar1, char *ar2)
{
    int i = 0;
    
    while(ar1[i] != '\0')
    {
        if(ar1[i] == ar2[0])
        {
            int j = 0;
            while(ar1[i + j] == ar2[j] && ar2[j] != '\0')
                j++;
            if(ar2[j] == '\0')
                return ar1 + i;
        }
        i++;
    }
    return NULL;
}

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

 

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

#define SIZE 100

char * scan(char *ar1, char *ar2);
char * s_gets(char *st, int n);

int main(void)
{
    char ar1[SIZE];
    char ar2[SIZE];
    char * result;
    
    printf("ar1: "); s_gets(ar1, SIZE);
    printf("ar2: "); s_gets(ar2, SIZE);
    
     while(*ar1 != '\n')
     {
         result = scan(ar1, ar2);
         
         if(result)
         {
             puts("overlapping!"); 
             printf("result: %p\n", result);    
             printf("result: %s\n", result);
        }
        else
        {
            puts("not overlapping!");
        }
        printf("ar1(blank to quit): "); s_gets(ar1, SIZE);

        if(ar1[0] == '\0')
            break;
        printf("ar2: "); s_gets(ar2, SIZE);
     }
     
     return 0;
}

char *scan(char *ar1, char *ar2)
{
    int count = 0;
    int ar2_len = strlen(ar2);
    
    while(*ar1 != '\0' && count < ar2_len)
    {
        if(*(ar1 + count) == *(ar2 + count))
        {
            count++;
        }
        else
        {
            count = 0;
            ar1++;
        }
    }
    if(count == ar2_len)
    {
        return ar1;
    }
    else
        return NULL;
}

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

 

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

#define SIZE 100

char * scan(char *ar1, char *ar2);
char * s_gets(char *st, int n);

int main(void)
{
    char ar1[SIZE];
    char ar2[SIZE];
    char * result;
    
    printf("ar1: "); s_gets(ar1, SIZE);
    printf("ar2: "); s_gets(ar2, SIZE);
    
     while(*ar1 != '\0')
     {
         result = scan(ar1, ar2);
         
         if(result)
         {
             puts("overlapping!"); 
             printf("result: %p\n", result);    
             printf("result: %s\n", result);
        }
        else
        {
            puts("not overlapping!");
        }
        printf("ar1(blank to quit): "); s_gets(ar1, SIZE);

        if(ar1[0] == '\0')
            break;
        printf("ar2: "); s_gets(ar2, SIZE);
     }
     
     return 0;
}

char *scan(char *ar1, char *ar2)
{
    int count = 0;
    int ar2_len = strlen(ar2);
    
    while(*ar1 != '\0' && count < ar2_len)
    {
        if(*(ar1 + count) == *(ar2 + count))
        {
            count++;
        }
        else
        {
            count = 0;
            ar1++;
        }
    }
    if(count == ar2_len)
    {
        return ar1;
    }
    else
        return NULL;
}

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

 

9、

#include <stdio.h>

#define SIZE 100

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

int main(void)
{
    char st1[SIZE];
    
    printf("please input st1: "); s_gets(st1, SIZE);
    
    
    printf("origin  st1: %s\n", st1);
    
    reverse(st1);
    
    printf("reverse st1: %s\n", st1);
    
    return 0;
        
}

void reverse(char *ar1)
{
    int len = 0;
    
    while(*(ar1 + len))
    {
        len++;
    }
    
    int i;
    char temp;
    
    for(i = 0; i < len / 2; i++)
    {
        temp = ar1[i];
        ar1[i] = ar1[len - 1 - i];
        ar1[len - 1 - i] = temp;
    }
}

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

 

循环

#include <stdio.h>

#define SIZE 100

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

int main(void)
{
    char st1[SIZE];
    
    while(1)
    {
        printf("please input st1(blank to quit): "); s_gets(st1, SIZE);
        if(st1[0] == '\0')
            break;
        printf("origin  st1: %s\n", st1);
        reverse(st1);
        printf("reverse st1: %s\n", st1);
    }
    return 0;    
}

void reverse(char *ar1)
{
    int len = 0;
    
    while(*(ar1 + len))
    {
        len++;
    }
    
    int i;
    char temp;
    
    for(i = 0; i < len / 2; i++)
    {
        temp = ar1[i];
        ar1[i] = ar1[len - 1 - i];
        ar1[len - 1 - i] = temp;
    }
}

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

 

#include <stdio.h>

#define SIZE 100

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

int main(void)
{
    char st1[SIZE];
    
    while(*st1 != '\n')
    {
        printf("please input st1(blank to quit): "); s_gets(st1, SIZE);
        if(st1[0] == '\0')
            break;
        printf("origin  st1: %s\n", st1);
        reverse(st1);
        printf("reverse st1: %s\n", st1);
    }
    return 0;    
}

void reverse(char *ar1)
{
    int len = 0;
    
    while(*(ar1 + len))
    {
        len++;
    }
    
    int i;
    char temp;
    
    for(i = 0; i < len / 2; i++)
    {
        temp = ar1[i];
        ar1[i] = ar1[len - 1 - i];
        ar1[len - 1 - i] = temp;
    }
}

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

 

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

#define SIZE 100

char * invert(char *ar);

int main(void)
{
    char st1[SIZE];
    printf("please input st1: "); fgets(st1, SIZE, stdin);
    
    while(*st1 != '\n')
    {
        printf("origin st1: %s", st1);
        invert(st1);
        printf("invert st1: %s\n", st1);

        printf("please input st1(blank to quit): ");
        fgets(st1, SIZE, stdin);
    }
    
    return 0;
}

char *invert(char *ar)
{
    int len = strlen(ar);
    
    char temp[len];
    
    int i;
    
    for(i = 0; i < len; i++)
    {
        *(temp + i) = *(ar + len - 1 - i);    
    }
    
    for(i = 0; i < len; i++)
    {
        *(ar + i) = *(temp + i);
    }
    
    return ar;
}

 

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

#define SIZE 100

char *invert(char *ar);
char *s_gets(char *st, int n);

int main(void)
{
    char st1[SIZE];
    printf("please input st1: "); s_gets(st1, SIZE);
    
    while(*st1 != '\0')
    {
        printf("origin st1: "); puts(st1);
        invert(st1);
        printf("invert st1: "); puts(st1);
        
        printf("please input st1(blank to quit): ");
        s_gets(st1, SIZE);
    }
    
    return 0;
}

char *invert(char *ar)
{
    int len = strlen(ar);
    
    char temp[len];
    int i;
    
    for(i = 0; i < len; i++)
    {
        *(temp + i) = *(ar + len - 1 - i);
    }
    for(i = 0; i < len; i++)
    {
        *(ar + i) = *(temp + i);
    }
        
    return ar;
}

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

 

10、

#include <stdio.h>

#define SIZE 100

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

int main(void)
{
    char st1[SIZE];
    
    printf("please input st1: "); s_gets(st1, SIZE);
    
    printf("origin st1: "); puts(st1);
    del_space(st1);
    printf("delete st1: "); puts(st1);
    
    return 0;
}

void del_space(char *ar)
{
    int i = 0;
    
    while(ar[i])
    {
        if(ar[i] == ' ')
        {
            int j = i;
            while(ar[i])
            {
                ar[i] = ar[i+1];
                i++; 
            }
            i = j;
        }
        else
        {
            i++;
        }
    }
}

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

 

#include <stdio.h>

#define SIZE 100

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

int main(void)
{
    char st1[SIZE];
    
    printf("please input st1: "); s_gets(st1, SIZE);
    
    while(*st1 != '\0')
    {
        printf("origin st1: "); puts(st1);
        del_space(st1);
        printf("delete st1: "); puts(st1);
        printf("please input st1(blank to quit): "); s_gets(st1, SIZE);
    }
    return 0;
}

void del_space(char *ar)
{
    int i = 0;
    
    while(ar[i])
    {
        if(ar[i] == ' ')
        {
            int j = i;
            while(ar[i])
            {
                ar[i] = ar[i+1];
                i++; 
            }
            i = j;
        }
        else
        {
            i++;
        }
    }
}

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

 

#include <stdio.h>

#define SIZE 100

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

int main(void)
{
    char st1[SIZE];
    
    printf("please input st1: "); s_gets(st1, SIZE);
    
    while(1)
    {
        printf("origin st1: "); puts(st1);
        del_space(st1);
        printf("delete st1: "); puts(st1);
        printf("please input st1(blank to quit): "); s_gets(st1, SIZE);
        if(st1[0] == '\0')
            break;
    }
    return 0;
}

void del_space(char *ar)
{
    int i = 0;
    
    while(ar[i])
    {
        if(ar[i] == ' ')
        {
            int j = i;
            while(ar[i])
            {
                ar[i] = ar[i+1];
                i++; 
            }
            i = j;
        }
        else
        {
            i++;
        }
    }
}

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

 

#include <stdio.h>

#define SIZE 100
void trim_space(char *ar);

int main(void)
{
    char st1[SIZE];
    
    printf("please input st1: "); fgets(st1, SIZE, stdin);
    
    while(*st1 != '\n')
    {
        printf("origin st1: %s", st1);
        trim_space(st1);
        printf("trim_space st1: %s", st1);
        printf("please input st1(blank to quit): "); fgets(st1, SIZE, stdin);
    }
    
    return 0;
}

void trim_space(char *ar)
{
    char *temp;
    
    temp = ar;
    int count = 0;
    
    while(*ar)
    {
        if(*ar != ' ')
        {
            *temp++ = *ar++;
        }
        else
        {
            ar++;
            count++;
        }
    }
    while(count--)
        *temp++ = '\0';
}

 

#include <stdio.h>

#define SIZE 100
void trim_space(char *ar);

int main(void)
{
    char st1[SIZE];
    
    printf("please input st1: "); fgets(st1, SIZE, stdin);
    
    while(*st1 != '\n')
    {
        printf("origin st1: %s", st1);
        trim_space(st1);
        printf("trim_space st1: %s", st1);
        printf("please input st1(blank to quit): "); fgets(st1, SIZE, stdin);
    }
    
    return 0;
}

void trim_space(char *ar)
{
    int i = 0;
    
    while(ar[i] != '\0')
    {
        if(ar[i] != ' ')
        {
            i++;
        }
        else
        {
            int j = i;
            while(ar[i] != '\0')
            {
                ar[i] = ar[i + 1];
                i++;
            }
            i = j;
        }
    }
}

 

 

#include <stdio.h>

#define SIZE 100
void trim_space(char *ar);

int main(void)
{
    char st1[SIZE];
    
    printf("please input st1: "); fgets(st1, SIZE, stdin);
    
    while(*st1 != '\n')
    {
        printf("origin st1: %s", st1);
        trim_space(st1);
        printf("trim_space st1: %s", st1);
        printf("please input st1(blank to quit): "); fgets(st1, SIZE, stdin);
    }
    
    return 0;
}

void trim_space(char *ar)
{
    char *temp = ar;
    int count = 0;
    
    while(*ar != '\0')
    {
        if(*ar != ' ')
        {
            *temp++ = *ar++;
        }
        else
        {
            ar++;
            count++;
        }
    }
    while(count--)
        *temp++ = '\0';
}

 

11、

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

#define SIZE 100
#define LIM 10

char *s_gets(char *st, int n);
char menu(void);
void print_origin(char ar[][100], int n);
void print_sort(char *ar[], int n);
void print_len(char *ar[], int n);
void print_first(char *ar[], int n);
int length(char *ar);

int main(void)
{
    char st1[LIM][SIZE];
    char *pt1[LIM];
    int ct = 0;
    printf("please input the strings, EOF to quit.\n");
    while(ct < LIM && s_gets(st1[ct], SIZE) != NULL && st1[ct][0] != EOF)
    {
        pt1[ct] = st1[ct];
        ct++;
    }
    printf("please choose: \n");
    char ch = menu();
    while(ch != 'q')
    {   
        switch (ch)
        {
            case 'a': print_origin(st1, ct); break;
            case 'b': print_sort(pt1, ct); break;
            case 'c': print_len(pt1, ct); break;
            case 'd': print_first(pt1, ct); break;
            case 'q': break;
            default: printf("a-d or q pleawe");
        }
        ch = menu();
    }
    return 0;
}

char menu(void)
{
    printf("please input your choice!\n");
    printf("a) print origin         b) print sort by ASCII\n");
    printf("c) print by length      d) print by first word length\n");
    printf("q) quit\n");
    
    char ch = getchar();
    
    while(getchar() != '\n')
        continue;
        
    return ch;
}

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

void print_origin(char ar[][100], int n)
{
    int i;
    puts("show the origin: ");
    for(i = 0; i < n; i++)
    {
        puts(ar[i]);
    }
    putchar('\n');
}

void print_sort(char *ar[], int n)
{
    char *temp;
    int i, j;
    
    for(i = 0; i < n - 1; i++)
    {
        for(j = i + 1; j < n; j++)
        {
            if(strcmp(ar[i], ar[j]) > 0)
            {
                temp = ar[i];
                ar[i] = ar[j];
                ar[j] = temp; 
            }
        }
    }
    puts("\nHere is the sorted by ASCII:");
    for(i = 0; i < n; i++)
    {
        puts(ar[i]);
    }
    putchar('\n');
}

void print_len(char *ar[], int n)
{
    char *temp;
    int i, j;
    
    for(i = 0; i < n - 1; i++)
    {
        for(j = i + 1; j < n; j++)
        {
            if(strlen(ar[i]) > strlen(ar[j]))
            {
                temp = ar[i];
                ar[i] = ar[j];
                ar[j] = temp;
            }
        }
    }
    puts("\nHere is show by length: ");
    for(i = 0; i < n; i++)
    {
        puts(ar[i]);
    }
    putchar('\n');
}

void print_first(char *ar[], int n)
{
    char *temp;
    int i, j;
    
    for(i = 0; i < n - 1; i++)
    {
        for(j = i + 1; j < n; j++)
        {
            if(length(ar[i]) > length(ar[j]))
            {
                temp = ar[i];
                ar[i] = ar[j];
                ar[j] = temp;
            }
        }
    }
    puts("show the result sort by first word.");
    for(i = 0; i < n; i++)
    {
        puts(ar[i]);
    }
    putchar('\n');
}

int length(char *ar)
{
    int i = 0;
    int j = 0;
    bool inword = false;
    
    while(ar[i] != '\0')
    {
        if(inword == false && ar[i] != ' ')
            inword = true;
        if(inword == true && ar[i] != ' ')
            j++;
        if(inword == true && ar[i] == ' ')
            break;
        i++;
    }
    
    if(j != 0)
        return j;
    else
        return i;
}

 

 

 

 

 

 

 

12、

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

void basic(void);

int main(void)
{
    printf("please input strings(EOF to quit): \n");
    basic();
    
    return 0;
}

void basic(void)
{
    char ch;
    int words = 0;
    int upper = 0;
    int lower = 0;
    int punct = 0;
    int digit = 0;
    bool inword = false;
    
    while((ch = getchar()) != EOF)
    {
        if(!isspace(ch) && inword == false)
        {
            words++;
            inword = true;
        }
        
        if(isspace(ch) && inword == true)
        {
            inword = false;
        }
        
        if(isupper(ch))
        {
            upper++;
        }
        
        if(islower(ch))
        {
            lower++;    
        } 
        
        if(ispunct(ch))
        {
            punct++;
        }
        
        if(isdigit(ch))
        {
            digit++;
        }
    }
    printf("words: %d\n", words);
    printf("upper: %d\n", upper);
    printf("lower: %d\n", lower);
    printf("punct: %d\n", punct);
    printf("digit: %d\n", digit);
}

 

循环

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

void basic(void);

int main(void)
{
    char ch;
    while(1)
    {
        printf("please input strings(EOF to quit): \n");
        basic();
        
        printf("blank to quit, other to contine: ");
        ch = getchar();
        
        if(ch == '\n')
            break;
            
        while(getchar() != '\n')
            continue; 
        
    }
    return 0;
}

void basic(void)
{
    char ch;
    int words = 0;
    int upper = 0;
    int lower = 0;
    int punct = 0;
    int digit = 0;
    bool inword = false;
    
    while((ch = getchar()) != EOF)
    {
        if(!isspace(ch) && inword == false)
        {
            words++;
            inword = true;
        }
        
        if(isspace(ch) && inword == true)
        {
            inword = false;
        }
        
        if(isupper(ch))
        {
            upper++;
        }
        
        if(islower(ch))
        {
            lower++;    
        } 
        
        if(ispunct(ch))
        {
            punct++;
        }
        
        if(isdigit(ch))
        {
            digit++;
        }
    }
    printf("words: %d\n", words);
    printf("upper: %d\n", upper);
    printf("lower: %d\n", lower);
    printf("punct: %d\n", punct);
    printf("digit: %d\n", digit);
}

 

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

#define SIZE 256

int check_words(char *input);
int check_upper(char *input);
int check_lower(char *input);
int check_punct(char *input);
int check_digit(char *input);

int main(void)
{
    char input[SIZE];
    int i = 0;
    
    while((input[i++] = getchar()) != EOF)
    {
        if(i > SIZE)
        {
            printf("over flowed\n");
            break;    
        }    
    }    
    
    printf("basic static is below: \n");
    printf("input words: %d\n", check_words(input));
    printf("upper   num: %d\n", check_upper(input));
    printf("lower   num: %d\n", check_lower(input));
    printf("punct   num: %d\n", check_punct(input));
    printf("digit   num: %d\n", check_digit(input));
    
    return 0;
} 

int check_words(char *input)
{
    int count = 0;
    int start = 0;
    
    while(*input != EOF)
    {
        if(isalpha(*input) == 0 && start == 0)
        {
            input++;
        }
        else if(isalpha(*input) == 0 && start == 1)
        {
            count++;
            input++;
            start = 0;
        }
        else if(isalpha(*input) != 0)
        {
            start = 1;
            input++; 
        }
    }
    if(start == 1)
        count++;
    return count;
}

int check_upper(char *input)
{
    int count = 0;
    
    while(*input != EOF)
    {
        if(isupper(*input++) != 0)
        {
            count++;
        }
        
    }
    return count;
}

int check_lower(char *input)
{
    int count = 0;
    
    while(*input != EOF)
    {
        if(islower(*input++) != 0)
        {
            count++;
        }
    }
    return count;
}

int check_punct(char *input)
{
    int count = 0;
    
    while(*input != EOF)
    {
        if(ispunct(*input++) != 0)
            count++;
    }
    return count;
}

int check_digit(char *input)
{
    int count = 0;
    
    while(*input != EOF)
    {
        if(isdigit(*input++) != 0)
            count++;
    }
    
    return count;
}

 

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

#define SIZE 500

int words_check(char *ar);
int upper_check(char *ar);
int lower_check(char *ar);
int punct_check(char *ar);
int digit_check(char *ar);

int main(void)
{
    char input[SIZE];
    int i = 0;
    
    puts("please input the strings.");
    while((input[i++] = getchar()) != EOF)
    {
        if(i > SIZE)
        {
            puts("number overflow!");
            break;
        }
    }
    
    puts("basic statics is below: ");
    printf("words num: %d\n", words_check(input));
    printf("upper num: %d\n", upper_check(input));
    printf("lower num: %d\n", lower_check(input));
    printf("punct num: %d\n", punct_check(input));
    printf("digit num: %d\n", digit_check(input));
    
    return 0;
}

int words_check(char *ar)
{
    int count = 0;
    int start = 0;
    
    while(*ar != EOF)
    {
        if(isalpha(*ar) == 0 && start == 0)
        {
            ar++;
        }
        if(isalpha(*ar) == 0 && start == 1)
        {
            count++;
            ar++;
            start = 0;
        }
        if(isalpha(*ar) != 0)
        {
            ar++;
            start = 1;
        }
    }
    if(start == 1)
        count++;
    return count;
}

int upper_check(char *ar)
{
    int count = 0;
    
    while(*ar != EOF)
    {
        if(isupper(*ar++) != 0)
            count++;
    }
    return count;
}

int lower_check(char *ar)
{
    int count = 0;
    
    while(*ar != EOF)
    {
        if(islower(*ar++) != 0)
        {
            count++;
        }
    }
    return count;
}

int punct_check(char *ar)
{
    int count = 0;
    
    while(*ar != EOF)
    {
        if(ispunct(*ar++) != 0)
        {
            count++;
        }
    }
    return count;
}

int digit_check(char *ar)
{
    int count = 0;
    
    while(*ar != EOF)
    {
        if(isdigit(*ar++) != 0)
        {
            count++;
        }
    }
    return count;
}

 

13、

#include <stdio.h>

void revert(char *ar);

int main(int argc, char *argv[])
{
    int i = 0;
    
    for(i = 1; i < argc; i++)
    {
        printf("%s ", argv[argc - i]);
    }
    putchar('\n');
    
    return 0;
}

 

#include <stdio.h>

void revert(char *ar);

int main(int argc, char *argv[])
{
    int i = 0;
    if(argc < 2)
    {
        puts("two few arguments");
    }
    else
    {
        for(i = 1; i < argc; i++)
        {
            printf("%s ", argv[argc - i]);
        }
        putchar('\n');
    }
        
    return 0;
}

 

#include <stdio.h>

void revert(char *ar);

int main(int argc, char *argv[])
{
    int i = 0;
    if(argc < 2)
    {
        puts("two few arguments");
    }
    else
    {
        for(i = argc; i > 1; i--)
        {
            printf("%s ", argv[i - 1]);
        }
        putchar('\n');
    }
        
    return 0;
}

 

14、

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char * argv[])
{
    int i;
    double power = 1.0;
    
    double argv_1 = atof(argv[1]);
    int argv_2 = atoi(argv[2]);
    
    if(argc != 3)
    {
        printf("argument error!");    
    } 
    else
    {
        for(i = 0; i < argv_2; i++ )
            power *= argv_1;
    }
    
    printf("power: %.2f\n", power);
    
    return 0;
}

 

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char * argv[])
{
    int i;
    double power = 1.0;
    
    double argv_1 = atof(argv[1]);
    int argv_2 = atoi(argv[2]);
    
    if(argc != 3)
    {
        printf("argument error! only %d argument!\n", argc);
        return 0;
    } 
    else
    {
        for(i = 0; i < argv_2; i++ )
            power *= argv_1;
    }
    
    printf("power: %.2f\n", power);
    
    return 0;
}

 

15、

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

#define SIZE 100

int convert_int(char *ar);
char *s_gets(char *st, int n);

int main(void)
{
    int result;
    char st1[SIZE];
    
    printf("st1: "); s_gets(st1, SIZE);
    
    printf("result: %d\n",convert_int(st1));
    
    return 0;
}

int convert_int(char *ar)
{
    int i = 0;
    
    while(ar[i] != '\0' && isdigit(ar[i]))
        i++;
    
    if(ar[i] != '\0')
    {
        puts("not all digits!");
        return 0;
    }
    else
    {
        int len = 0;
        while(ar[len] != '\0')
            len++;
        
        i = 0;
        int j;
        int temp; 
        int result = 0;
        
        while(ar[i] != '\0')
        {
            temp = 1;
            int num = ar[i] - '0';
            for(j = 0; j < len - 1 - i; j++)
            {
                temp *= 10;
            }
            result = result + num * temp;
            i++;
        }
        return result;
    }
}

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

 

 

 

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

#define SIZE 100

int convert_int(char *ar);
char *s_gets(char *st, int n);

int main(void)
{
    int result;
    char st1[SIZE];
    
    printf("st1: "); s_gets(st1, SIZE);
    
    while(st1[0] != '\0')
    {
        printf("result: %d\n",convert_int(st1));
        printf("st1(blank to quit): "); s_gets(st1, SIZE);
    }
    
    return 0;
}

int convert_int(char *ar)
{
    int i = 0;
    
    while(ar[i] != '\0' && isdigit(ar[i]))
        i++;
    
    if(ar[i] != '\0')
    {
        puts("not all digits!");
        return 0;
    }
    else
    {
        int len = 0;
        while(ar[len] != '\0')
            len++;
        
        i = 0;
        int j;
        int temp; 
        int result = 0;
        
        while(ar[i] != '\0')
        {
            temp = 1;
            int num = ar[i] - '0';
            for(j = 0; j < len - 1 - i; j++)
            {
                temp *= 10;
            }
            result = result + num * temp;
            i++;
        }
        return result;
    }
}

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

 

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

#define SIZE 100

int str_int(char *ar);

int main(void)
{
    char st1[SIZE];
    printf("please input st1: "); scanf("%s", st1);
    
    printf("convert result: %d\n", str_int(st1));
    
    return 0;
}

int str_int(char *ar)
{
    int len = 0;
    len = strlen(ar);
    int result = 0;
    int i;
    int temp = 1;
    
    
    for(i = len - 1; i >= 0; i--)
    {
        if(isdigit(ar[i]) == 0)
        {
            puts("not all digit!");
            return 0;    
        } 
        result =  result + (ar[i] - '0') * temp;
        temp *= 10;
    }
    
    return result;
}

 

 

 

 

循环:

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

#define SIZE 100

int str_int(char *ar);

int main(void)
{
    char st1[SIZE];
    
    printf("please input st1: "); scanf("%s", st1);
    while(1)
    {
        printf("convert result: %d\n", str_int(st1));
        printf("please input st1(q to quit): "); scanf("%s", st1);
        if(st1[0] == 'q')
            break;
    }
    return 0;
}

int str_int(char *ar)
{
    int len = 0;
    len = strlen(ar);
    int result = 0;
    int i;
    int temp = 1;
    
    
    for(i = len - 1; i >= 0; i--)
    {
        if(isdigit(ar[i]) == 0)
        {
            puts("not all digit!");
            return 0;    
        } 
        result =  result + (ar[i] - '0') * temp;
        temp *= 10;
    }
    
    return result;
}

 

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

#define SIZE 100

int convert(char *ar);

int main(void)
{
    char st1[SIZE];
    printf("please input st1: "); scanf("%s", st1);
    
    printf("convert result: %d\n", convert(st1));
    
    return 0;
}

int convert(char *ar)
{
    int len = strlen(ar);
    
    int i;
    int temp = 1;
    int result = 0;
    
    for(i = len; i > 0; i--)
    {
        if(isdigit(*(ar + i - 1)) == 0)
        {
            puts("not all digit!");
            return 0;
        }
        result = result + (*(ar + i - 1) - '0') * temp;
        temp *= 10;
    }
    return result;
}

 

 

 

 

循环:

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

#define SIZE 100

int convert(char *ar);

int main(void)
{
    char st1[SIZE];
    
    printf("please input st1: "); scanf("%s", st1);
    while(1)
    {
        printf("convert result: %d\n", convert(st1));
        printf("please input st1(q to break): "); scanf("%s", st1);
        if(st1[0] == 'q')
            break;
    }
    return 0;
}

int convert(char *ar)
{
    int len = strlen(ar);
    
    int i;
    int temp = 1;
    int result = 0;
    
    for(i = len; i > 0; i--)
    {
        if(isdigit(*(ar + i - 1)) == 0)
        {
            puts("not all digit!");
            return 0;
        }
        result = result + (*(ar + i - 1) - '0') * temp;
        temp *= 10;
    }
    return result;
}

 

16、

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


void to_upper(char *ar);
void to_lower(char *ar);

int main(void)
{
    char st1[100] = "abCDEF";
    
    puts("upper: ");
    to_upper(st1);
    
    puts("\nlower: ");
    to_lower(st1);
    
    return 0; 
}

void to_upper(char *ar)
{
    int i;
    
    while(*(ar + i))
    {
        *(ar + i) = toupper(*(ar + i));
        i++;
    }
    puts(ar);
}

void to_lower(char *ar)
{
    int i;
    
    while(*(ar + i))
    {
        *(ar + i) = tolower(*(ar + i));
        i++;
    }
    
    puts(ar);
}

 

16、

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

#define SIZE 256

void printf_orig(char *st);
void printf_upper(char *st);
void printf_lower(char *st);

int main(int argc, char *argv[])
{
    char c;
    if(argc < 2)
    {
        c = 'p';
    }
    else
        c = argv[1][1];
    
    char test[SIZE];
    
    printf("please input the string: ");
    fgets(test, SIZE, stdin);
    
    switch(c)
    {
        case 'p':
        case 'P': printf_orig(test); break;
        
        case 'u': 
        case 'U': printf_upper(test); break;
        
        case 'l':
        case 'L': printf_lower(test); break;
    }
    
    return 0;
}

void printf_orig(char *st)
{
    puts("result: ");
    puts(st);
}

void printf_upper(char *st)
{
    int i = 0;
    
    while(*(st + i))
    {
        *(st + i) = toupper(*(st + i));
        i++;
    }
    puts("result: ");
    puts(st);
}

void printf_lower(char *st)
{
    int i = 0;
    
    while(*(st + i))
    {
        *(st + i) = tolower(*(st + i));
        i++;
    }
    puts("result: ");
    puts(st);
}

 

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

#define SIZE 256

void printf_orig(char *st);
void printf_upper(char *st);
void printf_lower(char *st);

int main(char argc, char *argv[])
{
    char c;
    if(argc < 2)
    {
        c = 'p';
    }
    else
    {
        c = argv[1][1];
    }
    
    char test[SIZE];
    printf("please input the string: "); fgets(test, SIZE, stdin);
    
    switch(c)
    {
        case 'p':
        case 'P': printf_orig(test); break;
        
        case 'u':
        case 'U': printf_upper(test); break;
        
        case 'l':
        case 'L': printf_lower(test); break; 
    }
    return 0;
}

void printf_orig(char *st)
{
    printf("origin: %s\n", st);
}

void printf_upper(char *st)
{
    printf("upper: ");
    while(*st != EOF && *st != '\0')
    {
        putchar(toupper(*st++));
    }
    putchar('\n');
}

void printf_lower(char *st)
{
    printf("lower: ");
    
    while(*st != EOF && *st != '\0')
    {
        putchar(tolower(*st++));
    }
    putchar('\n');
}

 

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

#define SIZE 100

void print_orig(char *ar);
void print_upper(char *ar);
void print_lower(char *ar);

int main(int argc, char *argv[])
{
    char ch;
    
    
    if(argc < 2)
    {
        ch = 'p';
    }
    else
    {
        ch = argv[1][1];
    }
    
    char st1[SIZE];
    printf("please input the string: "); fgets(st1, SIZE, stdin);
    
    switch(ch)
    {
        case 'p':
        case 'P': print_orig(st1); break;
        
        case 'u':
        case 'U': print_upper(st1); break;
        
        case 'l':
        case 'L': print_lower(st1); break;
    }
    return 0;    
} 

void print_orig(char *st)
{
    printf("origin: %s\n", st);
}

void print_upper(char *st)
{
    printf("upper: ");
    
    while(*st != '\0')
    {
        putchar(toupper(*st++));
    }
}

void print_lower(char *st)
{
    printf("lower: ");
    
    while(*st != '\0')
    {
        putchar(tolower(*st++));
    }
}

 

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

#define SIZE 80
#define NUMBER 5

void show_menu(void);
void input_string(int number, char st[][SIZE]);
void print_original(int number, char st[][SIZE]);
void print_ascii(int number, char st[][SIZE]);
void print_length(int number, char st[][SIZE]);
void print_words(int number, char st[][SIZE]);
void sort_order(int number, int order[][2]);
int get_word_length(char *input);

int main(void)
{
    char test[NUMBER][SIZE];
    int selected;
    input_string(NUMBER, test);
    show_menu();
    
    while(scanf("%d", &selected) != 1)
    {
        printf("please input intiger.\n");
        while(getchar() != '\n')
            continue;
    }
    
    while(selected != 5)
    {
        switch(selected)
        {
            case 1: print_original(NUMBER, test); break;
            case 2: print_ascii(NUMBER, test); break;
            case 3: print_length(NUMBER, test); break;
            case 4: print_words(NUMBER, test); break;
            case 5: break;
            default: printf("Error select, retry!\n");
        }
        show_menu();
        while(scanf("%d", &selected) != 1)
        {
            printf("please input intiger.\n");
            while(getchar() != '\n')
                continue;
        }
    }
    printf("All done, bye.");
}

void show_menu()
{
    puts("================================================================\n");
    printf("1) print original strings            2) print string by ascii order\n");
    printf("3) print string by length            4) print string by word length\n");
    printf("5) quit\n");
    puts("\n==============================================================\n");
}

void input_string(int number, char st[][SIZE])
{
    printf("please intput 5 strings seperate by enter.\n");
    int i;
    for(i = 0; i < number; i++)
    {
        fgets(st[i], SIZE, stdin);
    }
}

void print_original(int number, char st[][SIZE])
{
    printf("print 5 strings in original mode.\n");
    int i;
    for(i = 0; i < number; i++)
    {
        printf("%d  %s", i, st[i]);
    }
}

void print_ascii(int number, char st[][SIZE])
{
    printf("print 5 strings in ascii order.\n");
    int order[number][2];
    int i;
    
    for(i = 0; i < number; i++)
    {
        order[i][0] = i;
        order[i][1] = st[i][0];
    }
    
    sort_order(number, order);
    
    for(i = 0; i <number; i++)
    {
        printf("ASCII  NO. %d. %s", i, st[order[i][0]]);
    }
    
}

void print_length(int number, char st[][SIZE])
{
    printf("print 5 strings in length mode.\n");
    int order[number][2];
    int i;
    
    for(i = 0; i < number; i++)
    {
        order[i][0] = i;
        order[i][1] = strlen(st[i]);
    }
    
    sort_order(number, order);
    
    for(i = 0; i < number; i++)
    {
        printf("length NO. %d. %s", i, st[order[i][0]]);
    }
}

void print_words(int number, char st[][SIZE])
{
    printf("print 5 strings in words mode\n");
    int order[number][2];
    int i;
    
    for(i = 0; i < number; i++)
    {
        order[i][0] = i;
        order[i][1] = get_word_length(st[i]);
    }
    
    sort_order(number, order);
    
    for(i = 0; i < number; i++)
    {
        printf("words NO. %d. %s", i, st[order[i][0]]);
    }
}

void sort_order(int number, int order[][2])
{
    int temp[2];
    int i, j;
    
    for(i = 0; i < number - 1; i++)
    {
        for(j = 0; j < number - 1 - i; j++)
        {
            if(order[j][1] > order[j + 1][1])
            {
                temp[0] = order[j][0];
                temp[1] = order[j][1];
                order[j][0] = order[j + 1][0];
                order[j][1] = order[j + 1][1];
                order[j + 1][0] = temp[0];
                order[j + 1][1] = temp[1];
            }
        }
    }
}

int  get_word_length(char *input)
{
    char *in = input;
    int length = 0;
    
    while(isalpha(*in) == 0)
    {
        in++;
    }
    
    while(isalpha(*in) != 0)
    {
        in++;
        length++;    
    }
    
    return length;
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


一.C语言中的static关键字 在C语言中,static可以用来修饰局部变量,全局变量以及函数。在不同的情况下static的作用不尽相同。 (1)修饰局部变量 一般情况下,对于局部变量是存放在栈区的,并且局部变量的生命周期在该语句块执行结束时便结束了。但是如果用static进行修饰的话,该变量便存
浅谈C/C++中的指针和数组(二) 前面已经讨论了指针和数组的一些区别,然而在某些情况下,指针和数组是等同的,下面讨论一下什么时候指针和数组是相同的。C语言标准对此作了说明:规则1:表达式中的数组名被编译器当做一个指向该数组第一个元素的指针; 注:下面几种情况例外 1)数组名作为sizeof的操作数
浅谈C/C++中的指针和数组(一)指针是C/C++的精华,而指针和数组又是一对欢喜冤家,很多时候我们并不能很好的区分指针和数组,对于刚毕业的计算机系的本科生很少有人能够熟练掌握指针以及数组的用法和区别。造成这种原因可能跟现在大学教学以及现在市面上流行的很多C或者C++教程有关,这些教程虽然通俗易懂,
从两个例子分析C语言的声明 在读《C专家编程》一书的第三章时,书中谈到C语言的声明问题,《C专家编程》这本书只有两百多页,却花了一章的内容去阐述这个问题,足以看出这个问题的重要性,要想透彻理解C语言的声明问题仅仅看书是远远不够的,需要平时多实践并大量阅读别人写的代码。下面借鉴《C专家编程》书中的两个
C语言文件操作解析(一)在讨论C语言文件操作之前,先了解一下与文件相关的东西。一.文本文件和二进制文件 文本文件的定义:由若干行字符构成的计算机文件,存在于计算机系统中。文本文件只能存储文件中的有效字符信息,不能存储图像、声音等信息。狭义上的二进制文件则指除开文本文件之外的文件,如图片、DOC文档。
C语言文件操作解析(三) 在前面已经讨论了文件打开操作,下面说一下文件的读写操作。文件的读写操作主要有4种,字符读写、字符串读写、块读写以及格式化读写。一.字符读写 字符读写主要使用两个函数fputc和fgetc,两个函数的原型是: int fputc(int ch,FILE *fp);若写入成功则
浅谈C语言中的位段 位段(bit-field)是以位为单位来定义结构体(或联合体)中的成员变量所占的空间。含有位段的结构体(联合体)称为位段结构。采用位段结构既能够节省空间,又方便于操作。 位段的定义格式为: type [var]:digits 其中type只能为int,unsigned int,s
C语言文件操作解析(五)之EOF解析 在C语言中,有个符号大家都应该很熟悉,那就是EOF(End of File),即文件结束符。但是很多时候对这个理解并不是很清楚,导致在写代码的时候经常出错,特别是在判断文件是否到达文件末尾时,常常出错。1.EOF是什么? 在VC中查看EOF的定义可知: #def
关于VC+ʶ.0中getline函数的一个bug 最近在调试程序时,发现getline函数在VC+ʶ.0和其他编译器上运行结果不一样,比如有如下这段程序:#include &lt;iostream&gt;#include &lt;string&gt;using namespace std;int
C/C++浮点数在内存中的存储方式 任何数据在内存中都是以二进制的形式存储的,例如一个short型数据1156,其二进制表示形式为00000100 10000100。则在Intel CPU架构的系统中,存放方式为 10000100(低地址单元) 00000100(高地址单元),因为Intel CPU
浅析C/C++中的switch/case陷阱 先看下面一段代码: 文件main.cpp#includeusing namespace std;int main(int argc, char *argv[]){ int a =0; switch(a) { case ...
浅谈C/C++中的typedef和#define 在C/C++中,我们平时写程序可能经常会用到typedef关键字和#define宏定义命令,在某些情况下使用它们会达到相同的效果,但是它们是有实质性的区别,一个是C/C++的关键字,一个是C/C++的宏定义命令,typedef用来为一个已有的数据类型
看下面一道面试题:#include&lt;stdio.h&gt;#include&lt;stdlib.h&gt;int main(void) { int a[5]={1,2,3,4,5}; int *ptr=(int *)(&amp;aʱ); printf(&quot;%d,%d&quot;,*(
联合体union 当多个数据需要共享内存或者多个数据每次只取其一时,可以利用联合体(union)。在C Programming Language 一书中对于联合体是这么描述的: 1)联合体是一个结构; 2)它的所有成员相对于基地址的偏移量都为0; 3)此结构空间要大到足够容纳最&quot;宽&quo
从一个程序的Bug解析C语言的类型转换 先看下面一段程序,这段程序摘自《C 专家编程》:#include&lt;stdio.h&gt;int array[]={23,34,12,17,204,99,16};#define TOTAL_ELEMENTS (sizeof(array)/sizeof(ar
大端和小端 嵌入式开发者应该对大端和小端很熟悉。在内存单元中数据是以字节为存储单位的,对于多字节数据,在小端模式中,低字节数据存放在低地址单元,而在大端模式中,低字节数据存放在高地址单元。比如一个定义一个short型的变量a,赋值为1,由于short型数据占2字节。在小端模式中,其存放方式为0X40
位运算和sizeof运算符 C语言中提供了一些运算符可以直接操作整数的位,称为位运算,因此位运算中的操作数都必须是整型的。位运算的效率是比较高的,而且位运算运用好的话会达到意想不到的效果。位运算主要有6种:与(&amp;),或(|),取反(~),异或(^),左移(&gt;)。1.位运算中的类型转换位
C语言文件操作解析(四)在文件操作中除了打开操作以及读写操作,还有几种比较常见的操作。下面介绍一下这些操作中涉及到的函数。一.移动位置指针的函数 rewind函数和fseek函数,这两个函数的原型是:void rewind(FILE *fp); 将位置指针移动到文件首 int fseek(FILE
结构体字节对齐 在用sizeof运算符求算某结构体所占空间时,并不是简单地将结构体中所有元素各自占的空间相加,这里涉及到内存字节对齐的问题。从理论上讲,对于任何变量的访问都可以从任何地址开始访问,但是事实上不是如此,实际上访问特定类型的变量只能在特定的地址访问,这就需要各个变量在空间上按一定的规则排
C语言文件操作解析(二)C语言中对文件进行操作必须首先打开文件,打开文件主要涉及到fopen函数。fopen函数的原型为 FILE* fopen(const char *path,const char *mode) 其中path为文件路径,mode为打开方式 1)对于文件路径,只需注意若未明确给出绝