Created
November 25, 2011 13:33
-
-
Save elderica/1393534 to your computer and use it in GitHub Desktop.
AOJ#0002 incorrected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
int itoa(int, char*, const int); | |
void reverseString(char*); | |
int main(void) | |
{ | |
char buf[100]; | |
int a, b; | |
while(scanf("%d %d", &a, &b)) | |
{ | |
printf("%d\n", itoa(a + b, buf, 100)); | |
} | |
return 0; | |
} | |
/* | |
itoa :: int -- target | |
-> char* -- string output buffer | |
-> int -- string output buffer size | |
-> int -- string length | |
*/ | |
int itoa(int num, char* buf, const int limit) | |
{ | |
int buflen = 0; | |
int nextnum; | |
const int lastindex = limit - 1; | |
while( num != 0) | |
{ | |
// buffer over flow protection | |
if ( buflen < lastindex ) | |
{ | |
buflen++; // for put '\0' on last of buffer | |
break; | |
} | |
nextnum = ((num / 10) * 10); | |
buf[ buflen++ ] = (char)(num - nextnum); | |
num = nextnum; | |
} | |
buf[ buflen ] = '\0'; | |
return --buflen; // string length without '\0' | |
} | |
/* | |
reverse string | |
*/ | |
void reverseString(char* string) | |
{ | |
const unsigned int length = strlen(string); | |
const unsigned int onc = length / 2; // offset nearly center | |
unsigned int ofsl, ofsr; // offset from left, offset from right | |
char *lp, *rp; // left pointer, right pointer | |
int i; | |
for ( i = 0; i <= onc; i++) | |
{ | |
ofsl = i; | |
ofsr = 1 + i; // be carefuly to '\0' | |
lp = string + ofsl; | |
rp = string + (length - ofsr); | |
// XOR exchange | |
*lp ^= *rp; | |
*rp ^= *lp; | |
*lp ^= *rp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment