Showing posts with label java parseInt() function implemenation in c language programme. Show all posts
Showing posts with label java parseInt() function implemenation in c language programme. Show all posts
on Saturday, 13 April 2013

 /* parseint() c version */


#include<stdio.h>
void remove_white_space(char *source) {  // including first sign(+/-) if exist
int iffc=0; //iffc=index_find_first_character after first sign
int ic=0; // index copy

/* find first character */
while(source[iffc]!='\0'){

if(source[iffc]==' '){ // seek for space
iffc++;
continue;
}

if(source[iffc]=='+' || source[iffc]=='-'){
iffc++;
break;
}

break;
}

if(iffc==0) // no whitespace found
return;


/* copy string part onto white space */


while(source[iffc]!='\0'){
source[ic]=source[iffc];
ic++;
iffc++;

}
source[ic]='\0'; // insert null at the end of string

 }


 int parseInt(char *source){
  remove_white_space(source);
  int index=0;
  int result=0;

while(source[index]!='\0'){

switch(source[index]){

case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
result=result*10+(source[index++]-48);
break;

default:
return result;

}


}
return result;

 }


void main(){
char source[]="456"; // can supply any input ,,
printf("%d",parseInt(source));


}