字符串数组的定义:

char *str[MAXLEN];

字符串数组的遍历:

#include <stdio.h>  
  
#define MAXLEN 10  
  
int main() {  
    char *str[MAXLEN] = {"apple", "banana", "cherry", "date", "elderberry", "fig", "grape", "honeydew", "kiwi", "lemon"};  
  
    // 遍历 str 字符串数组  
    for (int i = 0; i < MAXLEN; i++) {  
        printf("%s\n", str[i]);  
    }  
  
    return 0;  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

字符串的复制:strcpy()

#include <stdio.h>  
#include <string.h>  
int main() {  
    char source[] = "Hello, world!";  
    char destination[50];  
  
    strcpy(destination, source); // 将源字符串复制到目标字符串  
  
    printf("源字符串:%s\n", source);  
    printf("目标字符串:%s\n", destination);  
    return 0;  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

字符串的比较:strcmp()

#include <stdio.h>  
#include <string.h>  
int main() {  
    char str1[] = "Hello";  
    char str2[] = "World";  
    int result;  
    result = strcmp(str1, str2); // 比较 str1 和 str2  
    if (result == 0) {  
        printf("字符串相等\n");  
    } else if (result < 0) {  
        printf("字符串不相等,str1 小于 str2\n");  
    } else {  
        printf("字符串不相等,str1 大于 str2\n");  
    }  
    return 0;  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.