473,385 Members | 1,829 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,385 software developers and data experts.

<CR> into string

Hi,

I would like to insert <CR> which is 0x0D into a string at certain
position, any string function that I can use ?
Thanks.
Nov 14 '05 #1
13 4398

"Magix" <ma***@asia.com> wrote in message
news:41**********@news.tm.net.my...
Hi,

I would like to insert <CR> which is 0x0D into a string at certain
position, any string function that I can use ?
Thanks.


There is no need for a function to add/insert a character to a string.

#include <stdio.h>
#include <string.h>

int main ()
{

char arr[10] = "Hello";

/*Now suppose you need to add 0x0D at the end*/

int i = strlen(arr);/* Get the length of the string */

arr[i] = 0x0d;
arr[i+1] = '\0';

return 0;
}

HTH,
-Ravi


Nov 14 '05 #2
Ravi Uday wrote on 02/08/04 :
I would like to insert <CR> which is 0x0D into a string at certain
position, any string function that I can use ?
Thanks.


There is no need for a function to add/insert a character to a string.

#include <stdio.h>
#include <string.h>

int main ()
{

char arr[10] = "Hello";

/*Now suppose you need to add 0x0D at the end*/

int i = strlen(arr);/* Get the length of the string */

arr[i] = 0x0d;
arr[i+1] = '\0';

return 0;
}


If you want to add a at the end, you want strcat() or sprintf(). No
need to reinvent the weel.

char arr[10] = "Hello";

strcat (arr, "\r");
or
strcat (arr, "\xd");

But the OP wanted to 'insert' a character. And this is not trivial.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #3
Magix wrote on 02/08/04 :
I would like to insert <CR> which is 0x0D into a string at certain
position, any string function that I can use ?


There is no standard function for that. You can write your own, but be
careful. Don't overflow the output array, and bear in mind that string
literals can neither be written nor resized.

Do you best and post your attempts.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #4
Ravi Uday wrote:

"Magix" <ma***@asia.com> wrote in message
news:41**********@news.tm.net.my...
Hi,

I would like to insert <CR> which is 0x0D into a string at certain
position, any string function that I can use ?
Thanks.
There is no need for a function to add/insert a character to a string.

#include <stdio.h>
#include <string.h>

int main ()
{

char arr[10] = "Hello";

/*Now suppose you need to add 0x0D at the end*/

int i = strlen(arr);/* Get the length of the string */

arr[i] = 0x0d;


Could also try:
arr[i] = '\r';
instead.
arr[i+1] = '\0';

return 0;
}


--
pete
Nov 14 '05 #5
(supersedes <mn***********************@YOURBRAnoos.fr>)

Ravi Uday wrote on 02/08/04 :
I would like to insert <CR> which is 0x0D into a string at certain
position, any string function that I can use ?
Thanks.


There is no need for a function to add/insert a character to a string.

#include <stdio.h>
#include <string.h>

int main ()
{

char arr[10] = "Hello";

/*Now suppose you need to add 0x0D at the end*/

int i = strlen(arr);/* Get the length of the string */

arr[i] = 0x0d;
arr[i+1] = '\0';

return 0;
}


If you want to add a character at the end, you want strcat() or
sprintf(). No need to reinvent the weel.

char arr[10] = "Hello";

strcat (arr, "\r");
or
strcat (arr, "\xd");

But the OP wanted to 'insert' a character. And this is not trivial.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #6
pete wrote on 02/08/04 :
arr[i] = 0x0d;


Could also try:
arr[i] = '\r';
instead.
arr[i+1] = '\0';


It depends on the specs. If the exact binary value 0xD is required, I
suggest to use

enum {ASCII_CR = 0x0D};

or

#define S_ASCII_CR "\xd"

If the semantic of \r (Carriage return) is required, whatever is actual
value, '\r' is fine and portable.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #7
"Magix" <ma***@asia.com> wrote:
# Hi,
#
# I would like to insert <CR> which is 0x0D into a string at certain
# position, any string function that I can use ?

There's no standard function that would change the length of a string.
Originally libc functions did not call malloc internally, using arrays
passed in as arguments or static arrays. And because you cannot in
general guess the allocated size of an array with just a pointer to
the beginning, any library to do so would need a way to pass allocation
size in and out. (Always allocating exactly strlen()+1 results in
very inefficient code.)

There are many nonstandard libraries that are available on most
systems you're likely to run on.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
If your job was as meaningless as theirs, wouldn't you go crazy too?
Nov 14 '05 #8
"Magix" <ma***@asia.com> wrote in message
news:41**********@news.tm.net.my...
Hi,

I would like to insert <CR> which is 0x0D into a string at certain
position, any string function that I can use ?
Thanks.


Point to the spot and assign 0d to it...

char *str_ptr, str[]="Hello, World.";

str_ptr = strchr (str, ',');
if (str_ptr) *str_ptr = 0x0d;

--
Mabden
Nov 14 '05 #9
Mabden wrote on 02/08/04 :
I would like to insert <CR> which is 0x0D into a string at certain
position, any string function that I can use ?


Point to the spot and assign 0d to it...

char *str_ptr, str[]="Hello, World.";

str_ptr = strchr (str, ',');
if (str_ptr) *str_ptr = 0x0d;


This is not 'insert'. It's 'replace'....

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #10
"Emmanuel Delahaye" <em***@YOURBRAnoos.fr> wrote in message
news:mn***********************@YOURBRAnoos.fr...
Mabden wrote on 02/08/04 :
I would like to insert <CR> which is 0x0D into a string at certain
position, any string function that I can use ?


Point to the spot and assign 0d to it...

char *str_ptr, str[]="Hello, World.";

str_ptr = strchr (str, ',');
if (str_ptr) *str_ptr = 0x0d;


This is not 'insert'. It's 'replace'....


Oops, I misread that. Well, that's harder! I guess if you knew the buffer
size you could do that in a loop that steps through the string. I would like
to know WHY the OP wants to do this. It doesn't even seem like homework,
unless it's supposed to be done in C++. Probably, the problem has not been
analyzed properly.

Here's a way, without error checking:
===============================
// user must guarantee buffer is at least 1 char longer than string,
// and that pointers are valid.
void strins (char *str, char *str_end, char c)
{
++str_end; // move null also
while (str_end > str)
{
*str_end = *(str_end-1);
--str_end;
}
*str_end = c;
return;
}
===============================
--
Mabden

Nov 14 '05 #11
"Magix" <ma***@asia.com> wrote:
I would like to insert <CR> which is 0x0D into a string at certain
position, any string function that I can use ?


Yeah, memmove to move the rest of the characters out of the way, then store
the value into the newly freed position.

#include <string.h>

void inschar(char *str, size_t pos, char ch)
{
memmove(str + pos + 1, str + pos, strlen(str + pos) + 1);
str[pos] = ch;
}

With a call like:
inschr(array, 3, 0x0D);
this will insert a 0x0D at position 3.

--
Simon.
Nov 14 '05 #12
Ralmin wrote:
"Magix" <ma***@asia.com> wrote:
I would like to insert <CR> which is 0x0D into a string at
certain position, any string function that I can use ?


Yeah, memmove to move the rest of the characters out of the way,
then store the value into the newly freed position.

#include <string.h>

void inschar(char *str, size_t pos, char ch)
{
memmove(str + pos + 1, str + pos, strlen(str + pos) + 1);
str[pos] = ch;
}

With a call like:
inschr(array, 3, 0x0D);
this will insert a 0x0D at position 3.


The other commonly desired function is delchar. It could be coded
as:

char delchar(char *str, size_t pos)
{
if (str[pos])
memmove(str + pos, str + pos + 1, 1 + strlen(str + pos));
return str[pos]
}

and I would have inschar return str[pos] (i.e. ch) for symettry.
The return value could be useful in such coding as:

while (' ' == delchr(str, pos)) continue;

--
"I'm a war president. I make decisions here in the Oval Office
in foreign policy matters with war on my mind." - Bush.
"Churchill and Bush can both be considered wartime leaders, just
as Secretariat and Mr Ed were both horses." - James Rhodes.
Nov 14 '05 #13
CBFalconer <cb********@yahoo.com> spoke thus:
and I would have inschar return str[pos] (i.e. ch) for symettry.


That would be "symmetry" :)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #14

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

Similar topics

1
by: Paul Scotchford | last post by:
Env : SLQSERVER2000 - DTS I want to import a text file with 3 fields in it ... F1: Ident char(3) F2: Note (Text) char(32,000) yeah big eh! F3: Date Field F2: has...
0
by: Sparko | last post by:
My Xml is as follows; <root> <st id="1"> <rt id="1"> <cr id="1" ca_w="" ca_x="" ca_y="" ca_z="" other attribs> <ct some attribs> <te some attribs /> <te some attribs />
1
by: Billy N. Patton | last post by:
-------- Original Message -------- Subject: <string>.replace Date: Fri, 15 Oct 2004 11:07:19 -0500 From: Billy N. Patton <b-patton@ti.com> Organization: Texas Instruments Newsgroups:...
4
by: Don Wash | last post by:
Hi All! I'm getting the following Error: No DLLs has been compiled yet and nothing in the \bin directory. So it is not the versioning problem or anything like that. And here are the...
2
by: Winshent | last post by:
I have a multi line text in an admin page on my cms. I am trying to capture carriage returns as and replace them with <p></p> bfore the string gets written to the database. I have tried all...
1
by: Winshent | last post by:
I have a multi line text in an admin page on my cms. I am trying to capture carriage returns as and replace them with <p></p> bfore the string gets written to the database. I have tried all...
9
by: Eric Lindsay | last post by:
I can't figure how to best display little snippets of shell script using <pre>. I just got around to organising to bulk validate some of my web pages, and one of the problems occurs with Bash...
1
by: bobh | last post by:
Hi All, I have a process in AccessXP that compiles a list of names from the user table that is used as a distribution list which gets passed to Groupwise. The vba process separates each name...
4
by: spiralfire | last post by:
I wrote a translator, that reads a DIMACS graph format and writes to a simpler format... basically DIMACS format is: c comment p type nodes edges //type is alwats edge on my problems,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...

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.