zoukankan      html  css  js  c++  java
  • 字串简介

    20.字元阵列

    • 字元与字元阵列
    1. 一个字元型别变数可以储放一个字元 char ch = 'H';
    2. 一个字元阵列型别变数可以储放一到多个字元   如:char str [ ] = {'h', 'e', 'l', 'l', 'o',''};
    • 字串与字元阵列
    1. 字串是以 '' 表示结尾的字元阵列 
    2. 字元阵列可以用 “字串内容” 进行初始化, 会自动加上 '' 字元 如:char str[] = ''Hello'';
     1 #include <stdio.h>
     2 
     3 void str_print(char str[])
     4 /*
     5  {
     6     int i;
     7     for (i = 0; str[i] != ''; i++) {
     8         printf("%c", str[i]);
     9     }
    10     printf("
    ");
    11 } */
    12 
    13 {
    14     printf("%s
    ", str);
    15 }
    16 int main() {
    17     char str[] = "Hello world";
    18     str_print(str);
    19     return 0;
    20 }
    21 
    22 
    23 Hello world
    24 
    25 Process returned 0 (0x0)   execution time : 11.821 s
    26 Press any key to continue.

     20.1 计算字串长度的练习

     1 #include <stdio.h>
     2 
     3 int str_len (char str[]) {
     4     int i = 0;
     5     while (str[i] != '') {
     6         i++;
     7     }
     8     return i;
     9 }
    10 
    11 
    12 int main() {
    13     char str[11] = "Hello world"; //C 语言允许字元阵列大小刚好只包含有文字内容的部分,但这样的字元阵列不能直接拿来给一般字串处理函式使用
    14     printf("Length: %lu
    ", sizeof(str));
    15     printf("Length: %d
    ", str_len(str));// 字串长度
    16     return 0;
    17 }
    18 
    19 Length: 11
    20 Length: 12
    21 
    22 Process returned 0 (0x0)   execution time : 1.246 s
    23 Press any key to continue.
     1 #include <stdio.h>
     2 
     3 int str_len (char str[]) {
     4     int i = 0;
     5     while (str[i] != '') {
     6         i++;
     7     }
     8     return i;
     9 }
    10 
    11 
    12 int main() {
    13     char str[12] = "Hello world"; 
    14     printf("Length: %lu
    ", sizeof(str));
    15     printf("Length: %d
    ", str_len(str));// 字串长度
    16     return 0;
    17 }
    18 
    19 Length: 12
    20 Length: 11
    21 
    22 Process returned 0 (0x0)   execution time : 1.217 s
    23 Press any key to continue.

     20.2 从键盘输入读入一行字的练习

     1 #include <stdio.h>
     2 void str_read(char[]);
     3 
     4 int main() {
     5     char str[15];
     6     str_read(str);
     7     printf("%s
    ", str);
     8     return 0;
     9 }
    10 
    11 
    12 void str_read(char str[]) {
    13         int i = 0;
    14         while (1) {
    15             scanf("%c", &str[i]);
    16             if (str[i] == '
    ')
    17                 break;
    18             i++;
    19         }
    20         str[i] = '';
    21 }
    22 
    23 
    24 panfangxu
    25 panfangxu
    26 
    27 Process returned 0 (0x0)   execution time : 9.760 s
    28 Press any key to continue.

     22.3 关于读入字串的缓冲区溢位问题

     1 #include <stdio.h>
     2 
     3 void str_read(char[], int);
     4 
     5 int main() {
     6     char str[15];
     7     str_read(str, 7);
     8     printf("%s
    ", str);
     9     return 0;
    10 }
    11 
    12 void str_read(char str[], int n) {
    13     int i;
    14     for (i = 0; i < n; i++) {
    15         scanf("%c", &str[i]);
    16         if (str[i] == '
    ')
    17             break;
    18     }
    19     str[i] = '';
    20 }
    21 
    22 qwertyuiopasdfg
    23 qwertyu
    24 
    25 Process returned 0 (0x0)   execution time : 7.086 s
    26 Press any key to continue.

     阵列大小在宣告定义后就不能改变

    资源有限,不能改变大小就能解决问题

    20.4使用scanf读入资料的问题

     1 #include <stdio.h>
     2 
     3 int main() {
     4     int number;
     5     if (scanf("%d", &number) == 1) {
     6     printf("%d
    ", number);
     7 
     8     }else {
     9         printf("Error: Invalid input
    ");
    10 
    11     }
    12     return 0;
    13 }
    14 
    15 abc
    16 Error: Invalid input
    17 
    18 Process returned 0 (0x0)   execution time : 2.767 s
    19 Press any key to continue.
    20 
    21 1
    22 1
    23 
    24 Process returned 0 (0x0)   execution time : 3.133 s
    25 Press any key to continue.
     1 #include <stdio.h>
     2 
     3 int main() {
     4     int number;
     5     while (scanf("%d", &number) != 1) {
     6          printf("Error: Invalid input
    ");
     7 
     8     }
     9     printf("%d
    ", number);
    10     return 0;
    11 }
    12 
    13 abc
    14 Error: Invalid input
    15 Error: Invalid input
    16 ...
    17 
    18 // scanf 函式读入资料失败的后遗症
  • 相关阅读:
    洛谷P2762 太空飞行计划问题
    网络流24题 gay题报告
    洛谷P1712 区间
    洛谷P2480 古代猪文
    10.9zuoye
    面向对象类编程,计算分数
    请输入验证码优化版
    面向对象式开发程序
    直接选择排序与反转排序
    随机数产生原理
  • 原文地址:https://www.cnblogs.com/pxxfxxxx/p/10867528.html
Copyright © 2011-2022 走看看