m0_68362664 2022-04-16 10:02 采纳率: 68.2%
浏览 16
已结题

识别years前有几个数字当字符,将这些数字转换成整数,再把整数转换成字符串,与years连接后显示出来


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

int main()
{
char ori_str[] = "You will pay $687.71 every month for 15 years to payoff the debt";
char *str1 = NULL;
char *str2 = NULL;
double value = 0.0;

str1 = ori_str;
str2 = strchr(str1, '$');
printf("$的位置是:\n");
printf("%d\n\n",(int)(str2 - str1+1));
 
value = atof(str2 + 1);
printf("double value:\n\n %.2f",value);
 
 
char *s="You will pay $687.71 every month for 15 years to payoff the debt";
char *l="years";
char *p;
p=strstr(s,l);
if(p)
    printf("\n%d\n",p-s+1);
else
    printf("NotFound!");
}


  • 写回答

1条回答 默认 最新

  • 一只蚂蝼 2022-04-16 10:19
    关注
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main()
    {
        char ori_str[] = "You will pay $687.71 every month for 15 years to payoff the debt";
        char *str1 = NULL;
        char *str2 = NULL;
        double value = 0.0;
        
        str1 = ori_str;
        str2 = strchr(str1, '$');
        printf("$的位置是:\n");
        printf("%d\n\n",(int)(str2 - str1+1));
        
        value = atof(str2 + 1);
        printf("double value:\n\n %.2f",value);
        
        
        char s[] = "You will pay $687.71 every month for 15 years to payoff the debt";
        char l[] = "years";
        char num_str[100] = {'\0'};
        char *p = NULL;
        int num_addr = 0;
        
        p = strstr(s, l);
        if(p){
            printf("\n%d\n",(int)(p - s + 1));
        }else{
            printf("NotFound!");
            return -1;
        }
        
        for(int i = 0; i <= (int)(p - s); i++){
            
            if(s[i] >= '0' && s[i] <= '9'){
                num_str[num_addr] = s[i];
                num_addr++;
            }
    
        }
        
        if(num_addr){
            printf("%s%s",num_str,l);
        }else{
            printf("NotFound");
            return -1;
        }
        
        return 0;
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月24日
  • 已采纳回答 4月16日
  • 创建了问题 4月16日

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等