SHARE
TWEET

Untitled

a guest Nov 19th, 2019 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. int ch_str(char* str,char ch);
  3. int sum_of_digits(char* str);
  4. int sum_of_decimal(char* str);
  5. int main(){
  6.     char ch='A';
  7.     char str[10]={'A','b','2','4','f','5','g'};
  8.     printf("%d\n",ch_str(str,ch));
  9.     printf("%d\n",sum_of_digits(str));
  10.     printf("%d\n",sum_of_decimal(str));
  11.  
  12.  
  13. return 0;
  14. }
  15. int ch_str(char* str,char ch){
  16.     int i=0;
  17.     while(str[i]){
  18.         if(str[i]==ch)
  19.             return i;
  20.            else i++;
  21.     }
  22. return -1;
  23. }
  24. int sum_of_digits(char* str){
  25.     int i=0;
  26.     int sum=0;
  27.     while(str[i]){
  28.         if (str[i]>='0' && str[i]<='9')
  29.             sum+=str[i]-'0';
  30.         i++;
  31.     }
  32. return sum;
  33. }
  34. int sum_of_decimal(char* str){
  35.     int i=0;
  36.     int sum,current=0;
  37.         while(str[i]){
  38.             if (str[i]>='0' && str[i]<='9'){
  39.                 current=current*10 + str[i] - '0';
  40.                 i++;
  41.             }
  42.         i++;
  43.         sum+=current;
  44.         current=0;
  45.         }
  46.  
  47.     return sum;
  48. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top