C Primer Plus 第十一章 字符串 编程练习答案(自作)

11.12.10

// 11.12.10

#include <stdio.h>
int string(char *c);
int main()
{
    printf("Please input the string that you want: \n");
    char c[30];
    scanf("%s", c);
    printf("The length of string is %d\n", string(c));
    
    return 0;
    
}
int string(char *c)
{
    int i = 0;
    int cnt = 0;
    while(c[i] != '\0'){
        cnt++;
        i++;
    }
    return cnt;
}

11.12.12

//11.12.12

#include <stdio.h>
#define num 30
char* c(char *c);
int main()
{
    char string[num];
    fgets(string, num, stdin);
    char* p = c(string);
    
    return 0;
}
char* c(char *c)
{
    int i = 0;
    char *p = 0;
    while(c[i] != '\0'){
        if(c[i] = ' '){
            p = &c[i];
            break;
        }
        i++;
    }
    
    return p;
}
// 11.13.01

#include <stdio.h>
#define NUM 30
void func(char *c);
int main()
{
  char str[NUM];
  printf("Please enter the string that you want: \n");
  fgets(str, NUM, stdin);
  fputs(str, stdout);
  func(str);

  return 0;
}
void func(char *c)
{
  fputs(c, stdout);
  
  return;
}

11.13.02

// 11.13.02

#include <stdio.h>
#include <string.h>
#define NUM 30
void func(char *c);
int main()
{
  char str[NUM];
  printf("Please enter the string that you want: \n");
  func(str);
  fputs(str, stdout);
  putchar('\n');

  return 0;
}
void func(char *c)
{
  fgets(c, NUM, stdin);
  int i;
  for(i = 0; i < NUM; i++){
    if(c[i] == ' ' || c[i] == '\n' || c[i] == '\t')
      c[i] = '\0';
  }
  
  return;
}

11.13.05

// 11.13.05

#include <stdio.h>
#define NUM 30
char str1(char* c, char n);
int main()
{
  char str[NUM];
    fgets(str, NUM, stdin);
  char* ptr;
  ptr = str1(str, 'a');
  printf("%p\n", ptr);

  return 0;
}
char str1(char* c, char n)
{
  int i;
  char* ptr = 0;
  for(i = 0; i < NUM; i++){
    if(c[i] == 'a'){
      ptr = &c[i];
      break;
    }}

  return ptr;
}

11.13.06

//11.13.06

#include <stdio.h>
#define NUM 30
int is_within(char *c, char n);
int main()
{
    char str[NUM];
    printf("Please enter the string that you want:\n");
    fgets(str, NUM, stdin);
    printf("%d\n", is_within(str, 'a'));
    
    return 0;
}
int is_within(char *c, char n)
{
    int i;
    int ret = 0;
    for(i = 0; i < NUM; i++){
        if(c[i] == n)
            ret = 1;
    }
    
    return ret;
}

11.13.07

// 11.13.07

#include <stdio.h>
#define NUM 30
char* mystrncpy(char* str1, char* str2, int n);
int main()
{
    char str2[NUM];
    fgets(str2, NUM, stdin);
    char str1[NUM];
    
    fputs(mystrncpy(str1, str2, 5), stdout);
    putchar('\n');
    
    return 0;
}
char* mystrncpy(char* str1, char* str2, int n)
{
    int i;
    for(i = 0; i < n; i++){
        str1[i] = str2[i];
    }
    str1[n] = '\0';
    
    return str1;
}

11.13.08 这个题不确定是不是造轮子 还是用strstr()

// 11.13.08

#include <stdio.h>
#include <string.h>
#define NUM 30
int main()
{
    char str1[NUM];
    scanf("%s", str1);
    char str2[NUM];
    scanf("%s", str2);
    
    
    int i;
    for(i = 0; i < NUM; i++){
        if(str1[i] == str2[0]){
            printf("str2[0] = %p\n", &str1[i]);
        }
    }
    
    char* ptr;
    ptr = strstr(str1, str2);
    
    printf("%p\n", ptr);
           
    return 0;
}

11.13.09

// 11.13.09

#include <stdio.h>
#include <ctype.h>
#define NUM 10
int main()
{
    char str[NUM];
    fgets(str, NUM, stdin);
    int i;
    for(i = NUM; i >= 0; i--){
        if(isalpha(str[i]))
            putchar(str[i]);
    }
    putchar('\n');
    
    return 0;
}

11.13.10

// 11.13.10

#include <stdio.h>
#include <string.h>
#define NUM 30
void del(char* str);
int main()
{
    char str[NUM];
    fgets(str, NUM, stdin);
    del(str);
    fputs(str, stdout);
    
    return 0;
}
void del(char* str)
{
    char* ptr = 0;
    ptr = strchr(str, ' ');
    while(*ptr != '\0'){
        *ptr = *(ptr+1);
        ptr++;
    }
   
    return;
}

11.13.12

// 11.13.12

#include <stdio.h>
#include <ctype.h>
#define NUM 30
void func1(char* s);
void func2(char* s);
void func3(char* s);
void func4(char* s);
void func5(char* s);
int main()
{
    char str[NUM];
    printf("Please enter the string that you want: \n");
    fgets(str, NUM, stdin); // 包含 '\n' 和 '\0'
    func1(str); //单词数
    func2(str); //大写字母
    func3(str); //小写字母
    func4(str); //标点符号
    func5(str); //数字

    return 0;
}
void func1(char* s) // isalpha
{
    int cnt = 0;
    int i;
    for(i = 0; i < NUM; i++){
        if(isalpha(s[i]) != 0 && isalpha(s[i]) != '\0')
            cnt++;
    }
    printf("字符串内含字符数 %d 个\n", cnt);
    
    return;
}
void func2(char* s) // isupper
{
    int cnt = 0;
    int i;
    for(i = 0; i < NUM; i++){
        if(isupper(s[i]) != 0 && isupper(s[i]) != '\0')
            cnt++;
    }
    printf("字符串内含大写字母 %d 个\n", cnt);

    return;
}
void func3(char* s) // islower
{
    int cnt = 0;
    int i;
    for(i = 0; i < NUM; i++){
        if(islower(s[i]) != 0 && islower(s[i]) != '\0')
            cnt++;
    }
    printf("字符串内含小写字母 %d 个\n", cnt);

    return;
}
void func4(char* s) //ispunct
{
    int cnt = 0;
    int i;
    for(i = 0; i < NUM; i++){
        if(ispunct(s[i]) != 0 && ispunct(s[i]) != '\0')
            cnt++;
    }
    printf("字符串内含标点符号 %d 个\n", cnt);

    return;
}
void func5(char* s) // isdigit
{
      int cnt = 0;
    int i;
    for(i = 0; i < NUM; i++){
        if(isdigit(s[i]) != 0 && isdigit(s[i]) != '\0')
            cnt++;
    }
    printf("字符串内含数字 %d 个\n", cnt);

    return;
}

11.13.13

// 11.13.13

#include <stdio.h>
#include <string.h>
#define NUM 20
int main()
{
    char str[3][NUM];
    int i;
    for(i = 0; i < 3; i++)
        scanf("%s", str[i]);
    
    for(i = 2; i >= 0; i--)
        printf("%s ", str[i]);
    
    putchar('\n');
    return 0;
}

11.13.14

// 11.13.14

#include <stdio.h>
int main()
{
    double base;
    scanf("%lf", &base);
    int n;
    scanf("%d", &n);
    
    int i;
    int t = base;
    for(i = 0; i < n - 1; i++)
        base *= t;
    printf("%lf\n", base);
    
    return 0;
}

11.13.16

// 11.13.16

#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main()
{
    char str[20];
    fgets(str, 20, stdin);
    char n;
    scanf("%c", &n);
    
    if(n == 'p'){
        fputs(str, stdout);
    }
    else if(n == 'u'){
        for(int i = 0; i < strlen(str) ; i++){
            putchar(toupper(str[i]));
        }
        putchar('\n');
    }
    else if(n == 'l'){
        for(int j = 0; j < strlen(str) ; j++){
            putchar(tolower(str[j]));
        }
        putchar('\n');
    }
    else
        fputs(str, stdout);
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/fengqy1996/article/details/123743359