识别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!");
}



#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;
}