Advertisement
Nusrat_Ullah

string changling

Dec 1st, 2020
558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.45 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. char x[5],y[5],str[110];
  4. void value(int length)
  5. {
  6.     int i,j,a,b,res;
  7.     for(i=0;i<length;i++){
  8.         if(str[i]=='+'){
  9.             a=str[i-1]-'0';
  10.             b=str[i+1]-'0';
  11.             res=a+b;
  12.             printf("%d\n",res);
  13.             return;
  14.         }
  15.         if(str[i]=='-'){
  16.             a=str[i-1]-'0';
  17.             b=str[i+1]-'0';
  18.             res=a-b;
  19.             printf("%d\n",res);
  20.             return;
  21.         }
  22.         if(str[i]=='*'){
  23.             a=str[i-1]-'0';
  24.             b=str[i+1]-'0';
  25.             res=a*b;
  26.             printf("%d\n",res);
  27.             return;
  28.         }
  29.         if(str[i]=='/'){
  30.             a=str[i-1]-'0';
  31.             b=str[i+1]-'0';
  32.             res=a/b;
  33.             printf("%d\n",res);
  34.             return;
  35.         }
  36.         if(str[i]=='^'){
  37.             a=str[i-1]-'0';
  38.             b=str[i+1]-'0';
  39.             res=1;
  40.             for(j=1;j<=b;j++){
  41.                 res*=a;
  42.             }
  43.             printf("%d\n",res);
  44.             return;
  45.         }
  46.     }
  47. }
  48. int main()
  49. {
  50.     int i,length;
  51.     gets(str);
  52.     length=strlen(str);
  53.     printf("Letter to change : ");
  54.     scanf("%s",&x);
  55.     printf("New letter : ");
  56.     scanf("%s",&y);
  57.     for(i=0;i<length;i++){
  58.         if(x[0]==str[i]){
  59.             str[i]=y[0];
  60.         }
  61.     }
  62.     if(y[0]>='0'&&y[0]<='9'){
  63.         value(length);
  64.     }
  65.     else{
  66.         printf("%s\n",str);
  67.     }
  68.     return 0;
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement