<CR> into string

M

Magix

Hi,

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

Ravi Uday

Magix said:
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 = 0x0d;
arr[i+1] = '\0';

return 0;
}

HTH,
-Ravi
 
E

Emmanuel Delahaye

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 = 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.
 
E

Emmanuel Delahaye

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.
 
P

pete

Ravi said:
Magix said:
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 = 0x0d;


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

return 0;
}
 
E

Emmanuel Delahaye

(supersedes <[email protected]>)

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 = 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.
 
E

Emmanuel Delahaye

pete wrote on 02/08/04 :
arr = 0x0d;


Could also try:
arr = '\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.
 
S

SM Ryan

# 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.
 
M

Mabden

Magix said:
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;
 
E

Emmanuel Delahaye

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'....
 
M

Mabden

Emmanuel Delahaye said:
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;
}
===============================
 
R

Ralmin

Magix said:
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.
 
C

CBFalconer

Ralmin said:
Magix said:
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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,372
Latest member
LucretiaFo

Latest Threads

Top