I
Ian Stanley
Hi,
Continuing my strcat segmentation fault posting-
I have a problem which occurs when appending two sting literals using
strcat.
I have tried to fix it by writing my own function that does the strcat
(mystract). Program below.
However this appears not to have fixed the problem and I don't know why it
shouldn't ?
Any further help as to what else I am doing wrong will be appreciated
regards
Ian.
The idea of the program is to:
#enter a integer to convert to words
#99 -- input
#ninety nine --output
Ps tried returning sprintf and using macros with no luck.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* convertLessThanOneThousand(int number);
char* convert(int number);
char* mystrcat(char* dest, char* source);
static int num;
/*static char dest[1024];*/
static char *numNames[] = {
"",
" one",
" two",
" three",
" four",
" five",
" six",
" seven",
" eight",
" nine",
" ten",
" eleven",
" twelve",
" thirteen",
" fourteen",
" fifteen",
" sixteen",
" seventeen",
" eighteen",
" nineteen"
};
static char *tensNames[] = {
"",
" ten",
" twenty",
" thirty",
" forty",
" fifty",
" sixty",
" seventy",
" eighty",
" ninety"
};
char *majorNames[] = {
"",
" thousand",
" million",
" billion",
" trillion",
" quadrillion",
" quintillion"
};
int main(){
printf("enter a integer to convert to words ");
scanf("%d", &num);
printf("converted to words: %s\n", convert(num));
return 0;
}
char* convertLessThanOneThousand(int number) {
char* soFar;
/*char buffer[10000];
char* soFar;
soFar = buffer;*/
/* char result[256];*/
/*char* hundred = "hundred";*/
char hundred[] = "hundred";
if (number % 100 < 20){
soFar = numNames[number % 100];
number /= 100;
}
else {
soFar = numNames[number % 10];
number /= 10;
soFar = mystrcat(tensNames[number % 10], soFar);
number /= 10;
}
if (number == 0)
return soFar;
/*return numNames[number] + " hundred" + soFar;*/
/*sprintf(result, "%s%s%s", numNames[number], hundred, soFar);*/
return mystrcat(numNames[number], mystrcat(hundred, soFar));
}
char* convert(int number) {
char* zero = "zero";
if (number == 0) {
return zero; }
char* prefix = "";
char* soFar = "";
int place = 0;
do {
int n = number % 1000;
if (n != 0){
char* s = convertLessThanOneThousand(n);
/*soFar = s + majorNames[place] + soFar;*/
soFar = mystrcat(s, mystrcat(majorNames[place], soFar));
}
place++;
number /= 1000;
} while (number > 0);
/*return (prefix + soFar).trim();*/
return (mystrcat(prefix, soFar));
}
char* mystrcat(char* dest, char* source){
while(*dest){}
dest--;
while(*dest++ = *source++){}
return dest;
}
Continuing my strcat segmentation fault posting-
I have a problem which occurs when appending two sting literals using
strcat.
I have tried to fix it by writing my own function that does the strcat
(mystract). Program below.
However this appears not to have fixed the problem and I don't know why it
shouldn't ?
Any further help as to what else I am doing wrong will be appreciated
regards
Ian.
The idea of the program is to:
#enter a integer to convert to words
#99 -- input
#ninety nine --output
Ps tried returning sprintf and using macros with no luck.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* convertLessThanOneThousand(int number);
char* convert(int number);
char* mystrcat(char* dest, char* source);
static int num;
/*static char dest[1024];*/
static char *numNames[] = {
"",
" one",
" two",
" three",
" four",
" five",
" six",
" seven",
" eight",
" nine",
" ten",
" eleven",
" twelve",
" thirteen",
" fourteen",
" fifteen",
" sixteen",
" seventeen",
" eighteen",
" nineteen"
};
static char *tensNames[] = {
"",
" ten",
" twenty",
" thirty",
" forty",
" fifty",
" sixty",
" seventy",
" eighty",
" ninety"
};
char *majorNames[] = {
"",
" thousand",
" million",
" billion",
" trillion",
" quadrillion",
" quintillion"
};
int main(){
printf("enter a integer to convert to words ");
scanf("%d", &num);
printf("converted to words: %s\n", convert(num));
return 0;
}
char* convertLessThanOneThousand(int number) {
char* soFar;
/*char buffer[10000];
char* soFar;
soFar = buffer;*/
/* char result[256];*/
/*char* hundred = "hundred";*/
char hundred[] = "hundred";
if (number % 100 < 20){
soFar = numNames[number % 100];
number /= 100;
}
else {
soFar = numNames[number % 10];
number /= 10;
soFar = mystrcat(tensNames[number % 10], soFar);
number /= 10;
}
if (number == 0)
return soFar;
/*return numNames[number] + " hundred" + soFar;*/
/*sprintf(result, "%s%s%s", numNames[number], hundred, soFar);*/
return mystrcat(numNames[number], mystrcat(hundred, soFar));
}
char* convert(int number) {
char* zero = "zero";
if (number == 0) {
return zero; }
char* prefix = "";
char* soFar = "";
int place = 0;
do {
int n = number % 1000;
if (n != 0){
char* s = convertLessThanOneThousand(n);
/*soFar = s + majorNames[place] + soFar;*/
soFar = mystrcat(s, mystrcat(majorNames[place], soFar));
}
place++;
number /= 1000;
} while (number > 0);
/*return (prefix + soFar).trim();*/
return (mystrcat(prefix, soFar));
}
char* mystrcat(char* dest, char* source){
while(*dest){}
dest--;
while(*dest++ = *source++){}
return dest;
}