Warning: fopen(/www/wwwroot/www.wendadaohang.com/data/md5_content_title/78/78bc21c366cc5ef83ac8548ba17f4b60.txt): failed to open stream: No space left on device in /www/wwwroot/www.wendadaohang.com/inc/function.php on line 2468

Warning: flock() expects parameter 1 to be resource, bool given in /www/wwwroot/www.wendadaohang.com/inc/function.php on line 2469

Warning: fclose() expects parameter 1 to be resource, bool given in /www/wwwroot/www.wendadaohang.com/inc/function.php on line 2475
几道C语言的函数补全程序题,求大佬们解答 - www问答网

几道C语言的函数补全程序题,求大佬们解答

如题所述

当然,非常乐意为您解答C语言函数补全程序题。以下是一些练习题和参考答案:

    编写一个函数,输入一个字符串,将其中的所有小写字母转换为大写字母并返回结果字符串。

#include <stdio.h>
#include <string.h>
#include <ctype.h>

void toUpperCase(char *str) {
for(int i = 0; i < strlen(str); i++) {
if(islower(str[i])) {
str[i] = toupper(str[i]);
}
}
}

int main() {
char str[100];
printf("请输入一个字符串:");
scanf("%s", str);
toUpperCase(str);
printf("转换后的字符串为:%s\n", str);
return 0;
}

编写一个函数,输入一个字符串和一个字符,统计该字符在字符串中出现的次数并返回次数值。

#include <stdio.h>
#include <string.h>

int countChar(char *str, char c) {
int count = 0;
for(int i = 0; i < strlen(str); i++) {
if(str[i] == c) {
count++;
}
}
return count;
}

int main() {
char str[100];
char c;
printf("请输入一个字符串和一个字符,用空格隔开:");
scanf("%s %c", str, &c);
int count = countChar(str, c);
printf("字符 %c 在字符串 %s 中出现了 %d 次\n", c, str, count);
return 0;
}

c
#include <stdio.h>#include <math.h>int isPrime(int n) {    if(n < 2) {        return 0;
}    int i;    for(i = 2; i <= sqrt(n); i++) {        if(n % i == 0) {            return 0;
}
}    return 1;
}void printPrimes(int n) {    for(int i = 2; i <= n; i++) {        if(isPrime(i)) {            printf("%d ", i);
}
}
}int main() {    int n;    printf("请输入一个整数:");    scanf("%d", &n);    printf("1到%d的所有素数为:\n", n);
printPrimes(n);    printf("\n");    return 0;
}

希望以上答案可以帮到您。如果您还有其他的问题,欢迎继续向我提问。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2023-05-09

    编写一个函数,输入一个字符串,将其中的所有小写字母转换为大写字母并返回结果字符串。

#include <stdio.h>

#include <ctype.h>

#include <stdlib.h>

#include <string.h>

char* convertToLowerToUpper(const char* str) {

// 计算字符串的长度

size_t length = strlen(str);

 

    // 创建一个新的字符串,用于存储转换后的结果

char* result = (char*)malloc((length + 1) * sizeof(char));

if (result == NULL) {

printf("内存分配失败!\n");

return NULL;

}

// 逐个字符处理并转换为大写字母

for (size_t i = 0; i < length; i++) {

result[i] = toupper(str[i]);

}

// 添加字符串结束标志

result[length] = '\0';

return result;

}

int main() {

const char* input = "Hello, World!";

char* output = convertToLowerToUpper(input);

if (output != NULL) {

printf("转换后的字符串:%s\n", output);

free(output);

}

return 0;

}

使用C语言编写一个函数,输入一个字符串和一个字符,统计该字符在字符串中出现的次数并返回次数值

#include <stdio.h>

int countCharacter(const char* str, char ch) {

int count = 0;

// 遍历字符串中的每个字符

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

// 如果当前字符与指定字符相等,则增加计数器

if (str[i] == ch) {

count++;

}

}

return count;

}

int main() {

const char* input = "Hello, World!";

char character = 'o';

int count = countCharacter(input, character);

printf("字符 '%c' 在字符串中出现的次数为:%d\n", character, count);

return 0;

}

本回答被提问者采纳
相似回答