473,756 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)+strle n(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)+strle n(s)+1);strcat( d,s)
char* convertLessThan OneThousand(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("convert ed to words: %s\n", convert(num));
return 0;
}

char* convertLessThan OneThousand(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(tensName s[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 = convertLessThan OneThousand(n);
/*soFar = s + majorNames[place] + soFar;*/
soFar = strcat(s, strcat(majorNam es[place], soFar));
}
place++;
number /= 1000;
} while (number > 0);
/*return (prefix + soFar).trim();*/
return (strcat(prefix, soFar));
}

Nov 13 '05 #1
5 4643
Ian Stanley wrote [almost exactly what he wrote in
alt.comp.lang.l earn.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.l earn.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.ne t
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #3
Ian
Joe Wright <jo********@ear thlink.net> wrote in message news:<3F******* ****@earthlink. net>...
Tom Zych wrote:

Ian Stanley wrote [almost exactly what he wrote in
alt.comp.lang.l earn.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
5456
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"; //This outputs "GET /path/test.htm HTTP/1.1" happily //Send packet (I know there's no error checking, this is just an example)
6
2459
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
6174
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 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
23
11613
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 "c is abcd" . #include <stdio.h> #include <string.h> void main() { char a="ab"; char b="cd";
8
539
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 one field. To do this I tried to concatenate those 3 numbers into one using strcat. A piece of code is as follows /*function to enter the date*/ char * date (void) {
87
5149
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 the library, and other resources related to managed strings are available for download from the CERT web site at: http://www.cert.org/secure-coding/managedstring.html
4
4710
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
1762
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 target_path="/tmp/abc"; command=strtok(buf,":\n\r"); argument=strtok(NULL,"\n\r");
28
3903
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
9455
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10031
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9838
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9708
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8709
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7242
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5140
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.