470,626 Members | 2,153 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,626 developers. It's quick & easy.

strcat problem

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));
}

Nov 13 '05 #1
5 4339
Ian Stanley wrote [almost exactly what he wrote in
alt.comp.lang.learn.c-c++]

Did you read my reply in the other group? You're using strcat() to
append to string literals. It doesn't work. You can't change
literals.

BTW, it's "forty", not "fourty".

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
Nov 13 '05 #2
Tom Zych wrote:

Ian Stanley wrote [almost exactly what he wrote in
alt.comp.lang.learn.c-c++]

Did you read my reply in the other group? You're using strcat() to
append to string literals. It doesn't work. You can't change
literals.

BTW, it's "forty", not "fourty".

Yes, but it's only a spilling error. :-)
--
Joe Wright mailto:jo********@earthlink.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #3
Ian
Joe Wright <jo********@earthlink.net> wrote in message news:<3F***********@earthlink.net>...
Tom Zych wrote:

Ian Stanley wrote [almost exactly what he wrote in
alt.comp.lang.learn.c-c++]

Did you read my reply in the other group? You're using strcat() to
append to string literals. It doesn't work. You can't change
literals.

BTW, it's "forty", not "fourty".

Yes, but it's only a spilling error. :-)


Thanks Tom,
Is there a way to fix it?
I have never used sprintf before- are there any good examples out there?
Thanks again for your reply
Ian
Nov 13 '05 #4
Ian wrote:
Is there a way to fix it?
I have never used sprintf before- are there any good examples out there?


It's just like printf except the output goes into a string instead
of to stdout. So it's handy for building up a string from other
strings:

char a[] = "Like ";
char b[] = "this, ";
char c[] = "see?";
char result[30];

sprintf(result, "%s%s%s", a, b, c);
// check return value

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
Nov 13 '05 #5
Tom Zych <tz******@pobox.com> wrote:
Ian wrote:
Is there a way to fix it?
I have never used sprintf before- are there any good examples out there?


It's just like printf except the output goes into a string instead
of to stdout. So it's handy for building up a string from other
strings:

char a[] = "Like ";
char b[] = "this, ";
char c[] = "see?";
char result[30];

sprintf(result, "%s%s%s", a, b, c);
// check return value


My two cents: make sure that 'result' is big enough to hold the
resulting string to protect against buffer overrun.

In C99 one could use snprintf() which takes the size of the buffer
as an additional argument.

Irrwahn
--
do not write: void main(...)
do not use gets()
do not cast the return value of malloc()
do not fflush( stdin )
read the c.l.c-faq: http://www.eskimo.com/~scs/C-faq/top.html
Nov 13 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Patrick Coleman | last post: by
6 posts views Thread by Jon | last post: by
18 posts views Thread by Ian Stanley | last post: by
23 posts views Thread by JC | last post: by
8 posts views Thread by ctara_shafa | last post: by
87 posts views Thread by Robert Seacord | last post: by
4 posts views Thread by nick048 | last post: by
28 posts views Thread by Mahesh | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.