• 字符串处理总结


    一、字符串输入

    1. 输入单个字符串  

      可以使用 scanf 函数,以空格为分割输入字符串,代码如下:

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 int main() {
     5     char str[100];
     6     // 一个一个输入字符串,以空格为结尾 
     7     while(scanf("%s", str) != EOF) {
     8         printf("%s
    ", str);
     9     } 
    10 
    11     return 0;
    12 }

     

    2. 输入整行字符串

      可以使用 gets 函数,以 ' ' 为分割输入整行字符串,代码如下:

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 int main() {
     5     char str[100];
     6     // 以 '
    ' 为分割输入整行字符串 
     7     while(gets(str) != NULL) {
     8         printf("%s
    ", str);
     9     } 
    10 
    11     return 0;
    12 }

    二、字符串处理

    1. 字符串拷贝

      可以使用 strcpy 函数,代码如下:

    #include <stdio.h>
    #include <string.h>
    
    int main() {
        char str1[100], str2[100];
        // 一个一个输入字符串,以空格为结尾 
        while(scanf("%s", str1) != EOF) {
            strcpy(str2, str1);            // 将 str1 拷贝到 str2 
            printf("%s %s
    ", str1, str2);
        }
    
        return 0;
    }

      注意:1. str1 会覆盖 str2 内容;2. 定义数组是,str2 长度要大于或等于 str1。

      也可以使用 strncpy 函数,代码如下:

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 int main() {
     5     char str1[100], str2[100];
     6     // 一个一个输入字符串,以空格为结尾 
     7     while(scanf("%s %s", str1, str2) != EOF) {
     8         strncpy(str2, str1, 3);            // 将 str1的前3个字符 拷贝到 str1 
     9         printf("%s
    ", str2);
    10     }
    11 
    12     return 0;
    13 }

      注意:str2 的前 n 个字符会被 str1 的前 n 个字符覆盖。

     

    2. 字符串连接

      可以使用 strcat 函数,代码如下:

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 int main() {
     5     char str1[100], str2[100];
     6     // 一个一个输入字符串,以空格为结尾 
     7     while(scanf("%s %s", str1, str2) != EOF) {
     8         strcat(str2, str1);            // 将 str1 接到 str2 后面 
     9         printf("%s
    ", str2);
    10     }
    11 
    12     return 0;
    13 }

      注意:要注意 str2 的长度为 str1 与原 str2 长度之和,str2 最后的 '' 字符会自动消失。

      也可以使用 strncat 函数,将字符串的前 n 个字符连接到另一个字符后面,代码如下:

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 int main() {
     5     char str1[100], str2[100];
     6     // 一个一个输入字符串,以空格为结尾 
     7     while(scanf("%s %s", str1, str2) != EOF) {
     8         strncat(str2, str1, 3);            // 将 str1 的前3个字符接到 str2 后面 
     9         printf("%s
    ", str2);
    10     }
    11 
    12     return 0;
    13 }

    3.  字符串比较

      可以使用 strcmp 函数,代码如下:

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 int main() {
     5     char str1[100], str2[100];
     6     // 一个一个输入字符串,以空格为结尾 
     7     while(scanf("%s %s", str1, str2) != EOF) {
     8         int ptr = strcmp(str1, str2);            // 比较 str1,str2 
     9         if(ptr < 0) {                            // 返回值小于0 
    10             printf("%s < %s
    ", str1, str2);    // str1<str2 
    11         } else if(ptr == 0) {                    // 返回值等于0
    12             printf("%s == %s
    ", str1, str2);    // str1=str2 
    13         } else {                                // 返回值大于0
    14             printf("%s > %s
    ", str1, str2);    // str1>str2
    15         }
    16     }
    17 
    18     return 0;
    19 }

    4. 字符串长度

      可以使用 strlen 函数,代码如下:

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 int main() {
     5     char str1[100];
     6     // 一个一个输入字符串,以空格为结尾 
     7     while(scanf("%s", str1) != EOF) {
     8         // 输出字符串长度 
     9         printf("strlen = %d
    ", strlen(str1));
    10     }
    11 
    12     return 0;
    13 }
  • 相关阅读:
    ubuntu9.04 解决关机beep声音
    『转』饯行:理想主义终结年代的七种兵器
    尼康数码单反DX Nikkor镜头介绍
    Nikkor镜头介绍
    [转]IDL中全局变量的处理
    APSC画幅
    开心时刻1
    C# 相对路径
    使用C#语言,从Excel2007中读取数据,并显示到Form中的DataGridView。
    C# 讀取Excel、xlsx文件Excel2007
  • 原文地址:https://www.cnblogs.com/coderJiebao/p/Csummary01.html
Copyright © 2020-2023  润新知