C语言基础题目10道

  1. 输入一个字符串,统计不同字符的个数:

int countUniqueChars(const char* str){

        int UniqueChars = 0;

        int map[256] = {0};

        while (*str) {

            if (map[*str] == 0) {

                map[*str] ++;

                UniqueChars++;

             }

             str++;

             }

        return UniqueChars;

}

2.检查一个字符串中是否包含另一个字符串作为子序列:

bool isSubsequence(const char* mainStr, const char* subStr) {

    int mainIndex = 0, subIndex = 0;

    while (mainStr[mainIndex] != '\0' && subStr[subIndex] != '\0') {

        if ((mainStr[mainIndex] == subStr[subIndex]) {

             subIndex++; 

         }

         mainIndex++; 

    } 

    return subStr[subIndex] == '\0'; 

}

3.将给定的整数数组进行排序:

void bubbleSort(int arr[], int n) {

     for (int i = 0; i < n - 1; i++) {

         for (int j = 0; j < n - 1 - i; j++) {

            if (arr[j] > arr[j+1]) {

                 int temp = arr[j];

                 arr[j] = arr[j + 1];

                 arr[j + 1] = temp;

                } 

           }

    }

}

4.在给定的整数数组中找到第二大的数字:

int findSecondLargest(int arr[], int n) { 

    int max = INT_MIN, secondMax = INT_MIN; 

    for (int i = 0; i < n; i++) { 

        if (arr[i]

        secondMax = max; 

max = arr[i]; 

} else if (arr[i] > secondMax && arr[i] < max) { 

secondMax = arr[i];

if (secondMax == INT_MIN) { 

return -1; 

return secondMax; 

}

5.输入一个字符串,将其中的字母按照字母表顺序排序后输出:

int compareChars(const void *a, const void *b) {

return (*(char *)a) - (*(char *)b);

}

void sotrString(char *str) {

int len = strlen(str);

qsort(str, len, sizeof(char), compareChars);

}

6.将给定的整数分解为质因数的乘积:

(2是唯一一个偶质数)

void primeFactors(int n) { 

while (n % 2 == 0) { 

printf("%d ", 2); 

n = n / 2; 

for (int i = 3; i * i <= n; i = i + 2) { 

while (n % i == 0) {

printf("%d ", i); 

}

7.找到给定数组中的众数(出现次数最多的元素):

同样是使用哈希表

8.输入两个字符串,将它们连接:

 void connectChars(str1, str2) {

char result[200];

strcpy(result, str1);

strcat(result, str2);

return result;

9.将给定的整数数组中的偶数元素和奇数元素分开存放:

int separateNumber(int *arr) {

int arr1[100] = {0}, arr2[100] = {0};

int evenCount = 0, oddCount = 0

int n = sizeof(arr) / sizeof(arr[0]);

for (int i = 0; i < n; i++) {

if (arr[i] % 2 == 0) {

arr1[evenCount++] = arr[i];

} else {

arr2[oddCount++] = arr[i];

}

}

9.输入一个字符串和一个指定字符,删除字符串中所有该指定字符后输出:

void removeChar(char* str, char c) { 

    int j = 0; 

    int n = strlen(str); 

    for (int i = 0; i < n; i++) { 

        if (str[i] != c) { 

            str[j++] = str[i]; 

        } 

    }

    str[j] = '\0'; 

}

在原字符串基础上改动,然后把最后一个设置成‘\0’,就可以输出一个完成的截断字符串,不需要管后面的字符了。

10.输入一个字符串和一个指定字符集,统计字符串中指定字符集中字符的个数:

int countCharsInCharset(const char *str, const char *charset) {

int count = 0;

for (int i = 0; str[i] != '\0'; ++i) {

const char *c = strchr(charset, str[i]);

补充知识:char *strchr(const char *str, int c);

  • str:要搜索的字符串。
  • c:要查找的字符(以其 int 形式的 ASCII 值传递,但在调用时通常会传递一个 char 类型的值,它会自动转换为 int

返回值:

  • 如果找到字符 c,则 strchr 返回一个指向该字符在 str 中第一次出现的位置的指针。
  • 如果未找到字符 c,则返回 NULL

if (c != NULL) {

++count;

}

}

return count;

}

11.输入一个字符串,将其中的连续字符进行压缩:

char* compressString(const char* str) {

    if (str == NULL) return NULL;

    size_t max_length = strlen(str) * 2 + 1; // 每个字符可能变成"1c"形式,最后加一个'\0'

    char* compressed = (char*)malloc(max_length); 

    if (compressed == NULL) return NULL;

    size_t j = 0; 

    char prev = str[0];

    int count = 1;

    for (size_t i = 1; str[i] != '\0'; i++) {

        if (str[i] == prev) {

            count++;

        } else {

            size_t num_len = snprintf(compressed + j, max_length - j, "%d%c", count, prev);

            j += num_len;

            count = 1;

            prev = str[i];

        }

    }

    size_t num_len = snprintf(compressed + j, max_length - j, "%d%c", count, prev);

    j += num_len;

    compressed[j] = '\0'; // 确保字符串以null结尾

    return compressed; 

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值