C语言试题五十七之假定输入的字符串中只包含字母和*号。请编写函数function,它的功能是:删除字符串中所有*号。在编写函数时,不得使用c语言提供的字符串函数。

这是一篇关于C语言试题的文章,题目要求编写一个函数,功能是删除输入字符串中所有的*号,且在编写过程中不能使用C语言自带的字符串函数。文章提供了源代码实现及运行结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

📃个人主页:个人主页
🔥系列专栏:C语言试题200例目录
💬推荐一款刷算法、笔试、面经、拿大公司offer神器 👉 点击跳转进入网站
✅作者简介:大家好,我是码莎拉蒂,CSDN博客专家(全站排名Top 50),阿里云博客专家、51CTO博客专家、华为云享专家

1、题目

假定输入的字符串中只包含字母和*号。请编写函数function,它的功能是:删除字符串中所有*号。在编写函数时,不得使用c语言提供的字符串函数。 

2 、温馨提示

      C语言试题汇总里可用于

在C语言中,可以使用`strtol()`函数`strchr()`函数配合来提取整数部分。这里是一个简单的示例,假设输入字符串已经在变量`input_str`中: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> // Function to extract the integer part of a string representation of a number char* extract_integer(const char* input_str) { int sign = 1; // Check for negative sign if (input_str[0] == '-') { sign = -1; ++input_str; // Skip the '-' } char* end_ptr = NULL; // Pointer to the end of the whole number long long num = strtoll(input_str, &end_ptr, 10); // Convert string to long long // If no non-digit characters found after the first digit or the entire input is not a number if (*end_ptr == '\0' || *end_ptr == '.') { return (sign > 0 && num == LLONG_MIN) ? "0" : "0LL"; // Handle special case where it's just "-" } else if (num == 0 && strchr(end_ptr, '.') != NULL) { // Special case when there's only a decimal point return "0"; } // Trimming leading zeros and format as string char result[50]; // Assuming no more than 9 digits in integer part snprintf(result, sizeof(result), "%lld", num); while (result[0] == '0') { result++; // Move pointer to remove leading zeros } if (result[0] == '-') { result++; // Remove '-' if present } return result; } int main() { const char* input = "123.456"; // Example input char* integer_part = extract_integer(input); printf("Integer part: %s\n", integer_part); free(integer_part); // Don't forget to free the memory allocated by `strtol` return 0; } ``` 在这个程序中,我们首先检查字符串是否以负开始,处理这种情况。然后,尝试将字符串转换为`long long`类型的数值。如果转换成功且没有找到非数字字符,那么就返回这个数值;如果有非数字字符,如小数点或额外的零,我们会特殊处理。 最后,我们创建一个新的字符串结果,去掉多余的零格式化为只包含整数部分的形式。注意,这个程序假定输入字符串最多有99个有效数字加上一个小数点,实际应用中可能需要更严格的边界检查。
评论 30
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码莎拉蒂 .

你的鼓励是我最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值