BVB Source Codes

Algorithm-Implementations Show string_to_int.c Source code

Return Download Algorithm-Implementations: download string_to_int.c Source code - Download Algorithm-Implementations Source code - Type:.c
  1. #include <stdio.h>
  2. // Size of the array
  3. #define N 6
  4.  
  5. /**
  6.  * Count the length of an Array of characters
  7.  * wich is a String,
  8.  *
  9.  * @param   char    *string The array of chaacters
  10.  * @return  int     count   The length of the array
  11.  */
  12. int length(char *string){
  13.     int count  = 0;
  14.     while(string[count++] != '\0');
  15.     return --count;
  16. }
  17.  
  18. /**
  19.  * Exponentiation without a library
  20.  *
  21.  * @param   int base    the number base
  22.  * @param   int exp     the exponentiation number
  23.  * @return  int total   the total of the exponentiation
  24.  */
  25. int exponentiation(int base, int exp){
  26.     if(!exp){
  27.         return 1;
  28.     }
  29.  
  30.     int total = base;
  31.     int i = 1;
  32.  
  33.     while(i++ < exp){
  34.         total *= base;
  35.     }
  36.     return total;
  37. }
  38.  
  39. /**
  40.  * Parse the string to a number type
  41.  *
  42.  * @param   char    *number the array of numbers
  43.  * @param   int     total   the total number
  44.  */
  45. int parse(char *number){
  46.     int size = length(number);
  47.     int exp = size - 1;
  48.     int total = 0;
  49.     int i;
  50.    
  51.     for(i = 0; i < size; i++){
  52.         if(number[i] >= '0' && number[i] <= '9'){
  53.             total += (number[i] - 48) * exponentiation(10, exp--);
  54.         }
  55.     }
  56.     return total;
  57. }
  58.  
  59. int main(){
  60.  
  61.     char a[N] = "12345";
  62.     char b[N] = "32442";
  63.  
  64.     /**
  65.      * Test for the parser
  66.      * 1 pass
  67.      * 0 Failure
  68.      */
  69.     printf("%d\n", 12345 == parse(a));
  70.     printf("%d\n", 32442 == parse(b));
  71.  
  72.     return 0;
  73. }
  74.  
downloadstring_to_int.c Source code - Download Algorithm-Implementations Source code
Related Source Codes/Software:
raty - 2017-04-22
RDVTabBarController - Highly customizable tabBar and tabBarController fo... 2017-04-22
material-icon-lib - Library containing over 1500 material vector icons... 2017-04-21
httpdiff - Perform the same request against two HTTP servers ... 2017-04-21
jquerytools - The missing UI library for the Web ... 2017-04-21
mcrouter - Mcrouter is a memcached protocol router for scalin... 2017-04-22
dynomite - A generic dynamo implementation for different k-v ... 2017-04-22
kityminder - Baidu brain figure 2017-04-22
llvm - Mirror of official llvm git repository located at ... 2017-04-22
RBBAnimation - Block-based animations made easy, comes with easin... 2017-04-22
ied - 2017-04-29
Nimble - A Matcher Framework for Swift and Objective-C 2017-04-29
MHVideoPhotoGallery - A Photo and Video Gallery 2017-04-29
shoulda-matchers - Collection of testing matchers extracted from Shou... 2017-04-29
Android-SlideExpandableListView - A better ExpandableListView, with animated expanda... 2017-04-29
AppSales-Mobile - App Sales allows iPhone and Mac App Store develope... 2017-04-29
react-templates - Light weight templates for react ... 2017-04-28
afterglow-theme - A minimal dark Theme for Sublime Text 2 and 3 2017-04-28
jwt-go - Golang implementation of JSON Web Tokens (JWT) 2017-04-28
DeerResume - Tool MarkDown online resume, online preview, edit,... 2017-04-28

 Back to top