I
Ian Stanley
Hi,
Having not done any C programming for a while I am trying to get back into
it by converting an old java assignment into C.
The problem is I am getting a segmentation fault at runtime which I am
having trouble fixing(program below)
The idea of the program is to convert input(integers) into words eg:
# 101
returns
#one hundred one
I have found the problem is with the strcat of the strings & literals --
tried redef
strcat to allow for enough memory--
#define STRCAT(d,s) d=( char *)malloc(d, strlen(d)+strlen(s)+1);strcat(d,s)
then replaced strcat with STRCAT but end up with more errors? Also tried
using a buffer with enough memory and strcat to that - doesn't seem to work
either.
Not sure how to fix it from here.
Any help appreciated, regards
Ian
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRCAT(d,s) d=( char *)malloc(d, strlen(d)+strlen(s)+1);strcat(d,s)
char* convertLessThanOneThousand(int number);
char* convert(int number);
static int num;
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",
" fourty",
" fifty",
" sixty",
" seventy",
" eighty",
" ninety"
};
char *majorNames[] = {
"",
" thousand",
" million",
" billion",
" trillion",
" quadrillion",
" quintillion"
};
int main(){
printf("integer to convert: ");
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* hundred = "hundred";
if (number % 100 < 20){
soFar = numNames[number % 100];
number /= 100;
}
else {
soFar = numNames[number % 10];
number /= 10;
soFar = strcat(tensNames[number % 10], soFar);
number /= 10;
}
if (number == 0)
return soFar;
/*return numNames[number] + " hundred" + soFar;*/
return strcat(numNames[number], strcat(hundred, soFar));
}
char* convert(int number) {
char* zero = "zero";
if (number == 0) {
return zero; }
char* prefix = "";
/*if (number < 0) {
number = -number;
prefix = "negative";
}*/
char* soFar = "";
int place = 0;
do {
int n = number % 1000;
if (n != 0){
char* s = convertLessThanOneThousand(n);
/*soFar = s + majorNames[place] + soFar;*/
soFar = strcat(s, strcat(majorNames[place], soFar));
}
place++;
number /= 1000;
} while (number > 0);
/*return (prefix + soFar).trim();*/
return (strcat(prefix, soFar));
}
Having not done any C programming for a while I am trying to get back into
it by converting an old java assignment into C.
The problem is I am getting a segmentation fault at runtime which I am
having trouble fixing(program below)
The idea of the program is to convert input(integers) into words eg:
# 101
returns
#one hundred one
I have found the problem is with the strcat of the strings & literals --
tried redef
strcat to allow for enough memory--
#define STRCAT(d,s) d=( char *)malloc(d, strlen(d)+strlen(s)+1);strcat(d,s)
then replaced strcat with STRCAT but end up with more errors? Also tried
using a buffer with enough memory and strcat to that - doesn't seem to work
either.
Not sure how to fix it from here.
Any help appreciated, regards
Ian
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRCAT(d,s) d=( char *)malloc(d, strlen(d)+strlen(s)+1);strcat(d,s)
char* convertLessThanOneThousand(int number);
char* convert(int number);
static int num;
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",
" fourty",
" fifty",
" sixty",
" seventy",
" eighty",
" ninety"
};
char *majorNames[] = {
"",
" thousand",
" million",
" billion",
" trillion",
" quadrillion",
" quintillion"
};
int main(){
printf("integer to convert: ");
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* hundred = "hundred";
if (number % 100 < 20){
soFar = numNames[number % 100];
number /= 100;
}
else {
soFar = numNames[number % 10];
number /= 10;
soFar = strcat(tensNames[number % 10], soFar);
number /= 10;
}
if (number == 0)
return soFar;
/*return numNames[number] + " hundred" + soFar;*/
return strcat(numNames[number], strcat(hundred, soFar));
}
char* convert(int number) {
char* zero = "zero";
if (number == 0) {
return zero; }
char* prefix = "";
/*if (number < 0) {
number = -number;
prefix = "negative";
}*/
char* soFar = "";
int place = 0;
do {
int n = number % 1000;
if (n != 0){
char* s = convertLessThanOneThousand(n);
/*soFar = s + majorNames[place] + soFar;*/
soFar = strcat(s, strcat(majorNames[place], soFar));
}
place++;
number /= 1000;
} while (number > 0);
/*return (prefix + soFar).trim();*/
return (strcat(prefix, soFar));
}