473,796 Members | 2,669 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

adding chars to a string

Hi,

This is probably simple byt when you never did pointers and being used to
luxery strings like in Delphi or Visual Basic, C can get though. What I'm
trying to do is to add chars to a string. I looked in the string.h file but
I didn't find that kind of function (that string library is more than 7
years old) so I decided to write that function on myself. It seems to work
although when I'm trying to do printf's between those actions I get strange
messages. Anyway, now I'm trying to add chars to 2 strings. The first string
is ok but the second one seems to get overwrited. Here's my code :

/* the function to add 1 char to a string */

char *strAddChar(cha r *s1, char c){
char *s;
s = s1;
s = s + strlen(s1); // the position to write
*s = c; // this should be on the place where the null char
first
// was.
s++;
*s = 0; // add a new null string
return(s1);
} // strAddChar

/* this is how I use the function */
main(){
char *res1 = ""; // empty string 1
char *res2 = ""; // empty string 2
strAddChar( res1, 'a' );
strAddChar( res1, 'b' );
strAddChar( res1, 'c' );
strAddChar( res2, '1' );
strAddChar( res2, '2' );
strAddChar( res2, '3' );
printf( "%s\n",res1 ); // test
printf( "%s\n",res2 );

Ok, the output should be res1="abc" and res2="123" but this is my result :
res1="abc" res2="bc123"
How does "bc" come in res2?? How to fix the function?

Greetings,
Rick
Nov 13 '05
23 31094
In <bn**********@c hessie.cirr.com > Christopher Benson-Manica <at***@nospam.c yberspace.org> writes:
Kevin Goodsell <us************ *********@never box.com> spoke thus:
Unless you are using a C99 compiler (extremely unlikely), the above
"comment" is a syntax error. Use /* */ comments instead.


Well, it isn't conforming, but he could easily have a compiler that supports
this extension...


Which requires a diagnostic, once he's using his compiler as a conforming
C89 compiler, because, with one exception (//*) // is a syntax error in
C89.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #21
In <9u************ *************** *****@4ax.com> Irrwahn Grausewitz <ir*******@free net.de> writes:
David Rubin <fu******@warpm ail.net> wrote:
[...] Also, starting a function with 'str' violates the
implementatio n namespace.


But only if followed by a lowercase letter, thus the OP is safe
declaring a function named strAddChar.


External identifiers need not be case sensitive in C89!

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #22
"L.J. Buitinck" wrote:
.... snip ...
if your system has them, consider using strlcpy()/strlcat().
http://www.openbsd.org/papers/strlcpy-paper.ps


if your system doesn't have them, you can get source for them at:

<http://cbfalconer.home .att.net/download/>

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 13 '05 #23
Da*****@cern.ch (Dan Pop) wrote:
In <9u************ *************** *****@4ax.com> Irrwahn Grausewitz <ir*******@free net.de> writes:
David Rubin <fu******@warpm ail.net> wrote:
[...] Also, starting a function with 'str' violates the
implementati on namespace.


But only if followed by a lowercase letter, thus the OP is safe
declaring a function named strAddChar.


External identifiers need not be case sensitive in C89!


Umph, right, I was referring to C99 but didn't say so.
--
Irrwahn
(ir*******@free net.de)
Nov 13 '05 #24

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3612
by: David | last post by:
I have an array that contains numbers in string elements , i want to convert this to integers, i have made a for loop that uses the number of elements in the array and then adds 0, thereby converting it to an integer. //$chars is the array of strings for ($i=0; $i<$numpoints; $i++) { $array = array($chars + 0); } I dont know though how to add all these elements to the 1 array, it
4
7415
by: Neil W. | last post by:
I'm coming back to Perl after a long absence. I am trying to add up the ascii values of the characters in a string. Can anyone refresh my memory? Thanks.
4
8828
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where a * occurs in the string). This split function should allocate a 2D array of chars and put the split results in different rows. The listing below shows how I started to work on this. To keep the program simple and help focus the program the...
9
6280
by: Durgesh Sharma | last post by:
Hi All, Pleas help me .I am a starter as far as C Language is concerned . How can i Right Trim all the white spaces of a very long (2000 chars) Charecter string ( from the Right Side ) ? or how can i make a fast Right Trim Function in c,using Binary search kind of fast algorithm ? Offcourse...I can use the classical approach too. like : Start from the right most charecter of the string to the left of the
0
2309
by: William Stacey [MVP] | last post by:
Had a method that got some string info from mp3 tags in N files and serializes this class and deserializes at other side. Works ok except sometimes get chars that choke the XmlSerializer. After some digging, I found XmlSerializer chokes on 0x03 chars. It probably chokes on many others, but this one I found. It serializes ok, but chokes on deserialize on "<Field1>&#x3;</Field1>". So the questions are: 1) Why does serializer produce...
6
2626
by: guy | last post by:
if a string contains surrogate chars (i.e. Unicode characters that consiste of more than 1 char) do functions that use an indexer or a string length into the string e.g. Mid, Len work correctly? guy
21
2315
by: Hitesh | last post by:
Hi, I get path strings from a DB like: \\serverName\C:\FolderName1\FolderName2\example.exe I am writing a script that can give me access to that exe file. But problem is that string is not universal path, I need to add C$. Any idea how I can add $ char in that string. ServerName is not fixed length. It could be any chars length.
6
2355
by: Paulers | last post by:
Hello, I have a string that I am trying to add each char to a datatable row. for example if I have a string that looks like "abcdefg", I would like to break it up into an array of characters so I can do this: myDataTable.Rows.Add(array()) instead of myDataTable.Rows.Add("a","b","c","d","e","f","g")
13
3755
by: Hongyu | last post by:
Hi, I have a datetime char string returned from ctime_r, and it is in the format like ""Wed Jun 30 21:49:08 1993\n\0", which has 26 chars including the last terminate char '\0', and i would like to remove the weekday information that is "Wed" here, and I also would like to replace the spaces char by "_" and also remove the "\n" char. I didn't know how to truncate the string from beginning or replace some chars in a string with another...
0
9684
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
10459
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...
0
10236
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10182
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
10017
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
5445
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
5577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4120
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.