Advertisement
nguyenhappy92

Tính tổng các ký tự số có trong chuỗi

Dec 19th, 2015
593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. // Tinh tong cac ki tu so co trong chuoi
  2. // Tim gia tri ki tu so sau do cong lai
  3. // Khai bao cac ham thu vien neu co
  4. #include<stdio.h>
  5. #include<conio.h>
  6. #include<string.h>
  7. int GetValue(char c)// lay gia tri so tu 0 den 9
  8. {
  9. int i = c - 48;
  10. if (i > 9 || i < 0) return 0;// kiem tra ki tu
  11. else return i;
  12. }
  13.  
  14. int TongKyTuSo(char str[])
  15. {
  16. int result, temp;
  17. result = temp = 0;
  18. for (int i=0; str[i] != '\0'; i++)// tu dau chuoi den cuoi chuoi
  19. {
  20. temp = GetValue(str[i]);
  21. result += temp;// tong ki tu
  22. }
  23. return result;
  24. }
  25.  
  26. void main()
  27. {
  28. char str[200+1];
  29. gets(str);
  30. printf("%d", TongKyTuSo(str));
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement