473,320 Members | 1,948 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 4592
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Patrick Coleman | last post by:
Hi, I have the following code: char request = "GET "; strcat(request, path); //Path is the path section of a url ie. "/path/test.htm" strcat(request, " HTTP/1.1"); cout<<request<<"\n";...
6
by: Jon | last post by:
using Borland Compiler. x = "this is a string" strcat(x,x) strcat(x,x) will produce "this is a stringthis is a stringthis is a stringthis is a stringt"
18
by: Ian Stanley | last post by:
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...
23
by: JC | last post by:
hi, i want to combine two string together.. and put in to another string. how can i do . i try myself.. with the follow code. but seem can't get the result i want.. i want to get the result with...
8
by: ctara_shafa | last post by:
Hi, I have a following problem: I'm creating a list and one of the fields should contain the date. Firstly I ask the user for the year, month and day and then I'd like to collect all this data in...
87
by: Robert Seacord | last post by:
The SEI has published CMU/SEI-2006-TR-006 "Specifications for Managed Strings" and released a "proof-of-concept" implementation of the managed string library. The specification, source code for...
4
by: nick048 | last post by:
Hi, I have this problem: int n; char nToChar; char firstString = "The number is: "; char resp; /* HERE MAIN WITH THE INPUT OF n*/
3
by: sail0r | last post by:
Perhaps this is obvious but I am not sure what is going on... Here is the relevant code: char *command; char *argument; char url="file:///usr/u/myname/Project/cats/"; char...
28
by: Mahesh | last post by:
Hi, I am looking for efficient string cancatination c code. I did it using ptr but my code i think is not efficient. Please help. Thanks a lot
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.