void append(char str1[], char str2[])
{
int pos1 = 0; // tracks current pos in str1
int pos2 = 0; // tracks current pos in str2
// find the end of str1
while (str1[pos1] != '\0') pos1++;
// copy str2 contents until end of str2
while (str2[pos2] != '\0') {
str1[pos1] = str2[pos2];
pos1++;
pos2++;
}
// end str1
str1[pos1] = '\0';
}
|
void f1(char str1[], int n1, char str2[], int n2)
{
if (n1 < 1) {
printf("%s\n", str2);
} else if (n2 < 1) {
printf("%s\n", str1);
} else {
n1--;
n2--;
printf("%c%c", str1[n1], str2[n2]);
f1(str1, n1, str2, n2);
}
}
|
void f1(char str1[], int n1, char str2[], int n2)
{
if (n1 < 1) {
cout << str2 << endl;
} else if (n2 < 1) {
cout << str1 << endl;
} else {
n1--;
n2--;
cout << str1[n1] << str2[n2];
f1(str1, n1, str2, n2);
}
}
|
string ftoa(float f)
{
const int decimals = 6;
bool neg = false;
if (f < 0) {
neg = true;
f = -f;
}
int d, digit;
long intf = floor(f);
f = f - intf;
for (d = 0; d < decimals; d++) f = f * 10;
long fracf = floor(f);
if ((f - floor(f)) >= 0.5) fracf++;
string rev = "";
for (d = 0; d < decimals; d++) {
digit = fracf % 10;
fracf = fracf / 10;
rev += (char)(digit + '0');
}
rev += ".";
while (intf > 0) {
digit = intf % 10;
intf = intf / 10;
rev += (char)(digit + '0');
}
string result = "";
if (neg) result = "-";
int len = rev.length();
while (len > 0) {
len--;
result += rev[len];
}
return result;
}
string itoa(int i)
{
string rev = "";
bool neg = false;
if (i < 0) {
neg = true;
i = - i;
}
while (i > 0) {
int digit = i % 10;
i = i / 10;
rev += (char)(digit + '0');
}
string result = "";
if (neg) result = "-";
int len = rev.length();
while (len > 0) {
len--;
result += rev[len];
}
return result;
}