程序代码:
#include<stdio.h> #include<stdlib.h> #include<string.h> int main() { char str[100]={0},tmp[100]={0}; char *temp1; int len=0,num=0; printf("请输入字符串:"); scanf("%s",str); temp1=strchr(str,'+'); //判断字符串是加还是减 if(temp1==NULL) { temp1=strchr(str,'-'); len=(temp1-str)/sizeof(char); //判断-号前的字符长度 while((*(++temp1))!='\0') { num=num*10+*temp1-48; //计算-号后的数字 } if((len-num)>0) strncpy(tmp,str,len-num); //将字符串前len-num的字符赋给tmp else strncpy(tmp,str,0); printf("删除%d个字符后的字符串:%s\n",num,tmp); } else { strncpy(tmp,str,(temp1-str)/sizeof(char)); //如果是加,将+前的字符付给tmp strcat(tmp,temp1+1); //将+后的字符付给tmp printf("字符串和:%s\n",tmp); } return 0; }