By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,219 Members | 1,110 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,219 IT Pros & Developers. It's quick & easy.

How to truncate char string fromt beginning and replace chars instring by other chars in C or C++?

P: n/a
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 chars without using a loop through one char
by one char of the string. I used the below code to achieve
replacement of " " by "_" and also removed the last "\n" char, without
considering removing the first 4 chars i.e. weekday information yet.
But even this I still didn't get what i like. Below is the code i
wrote:
#include <time.h>
#include <stdio.h>

int main(void)
{
time_t ltime;
char buf[50];

// Get the time
time(&ltime);

// The datetime string returned by ctime_r is in the format
// of "Wed Jun 30 21:49:08 1993\n\0"
printf("The time is: %s", ctime_r(&ltime, buf));

// replace the " " and ":" in the datetime string
// by "_"
buf[7] = "_"; // " ", line 18
buf[10] = "_"; // " ", line 19
buf[13] = "_"; // ":", line 20
buf[16] = "_"; // ":", line 21
buf[19] = "_"; // " ", line 22
buf[24] = "\0"; // remove the last \n char, line 23

// printf the new datetimestring
printf("The time is: %s", buf);

}

When I complied it with gcc, i got the below warning:

test_ctimer.c: In function `main':
test_ctimer.c:18: warning: assignment makes integer from pointer
without a cast
and same warning for line 19 to 23 too.

When I run it, I got:

The time is: Thu Aug 7 15:02:32 2008
The time is: Thu AugX 7X15X02X32X2008Z

Can anyone kindly help me? I searched on the internet and it seems C
library doesn't have a function to truncate from the beginning? and it
also doesn't have a function for characher replacement. Should I have
to use a loop?

Thanks a lot for the help in advance.

Hongyu
Aug 7 '08 #1
Share this Question
Share on Google+
13 Replies


P: n/a
Hongyu wrote:
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 chars without using a loop through one char
by one char of the string. I used the below code to achieve
replacement of " " by "_" and also removed the last "\n" char, without
considering removing the first 4 chars i.e. weekday information yet.
But even this I still didn't get what i like. Below is the code i
wrote:
#include <time.h>
#include <stdio.h>

int main(void)
{
time_t ltime;
char buf[50];

// Get the time
time(&ltime);

// The datetime string returned by ctime_r is in the format
// of "Wed Jun 30 21:49:08 1993\n\0"
printf("The time is: %s", ctime_r(&ltime, buf));

// replace the " " and ":" in the datetime string
// by "_"
buf[7] = "_"; // " ", line 18
For a single symbol you need to use single quotes:

buf[7] = '_';

(same everywhere).
buf[10] = "_"; // " ", line 19
buf[13] = "_"; // ":", line 20
buf[16] = "_"; // ":", line 21
buf[19] = "_"; // " ", line 22
buf[24] = "\0"; // remove the last \n char, line 23

// printf the new datetimestring
printf("The time is: %s", buf);

}
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 7 '08 #2

P: n/a
On Aug 7, 3:31*pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Hongyu wrote:
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 chars without using a loop through one char
by one char of the string. I used the below code to achieve
replacement of " " by "_" and also removed the last "\n" char, without
considering removing the first 4 chars i.e. weekday information yet.
But even this I still didn't get what i like. Below is the code i
wrote:
#include <time.h>
#include <stdio.h>
int main(void)
{
* *time_t ltime;
* *char buf[50];
* *// Get the time
* *time(&ltime);
* *// The datetime string returned by ctime_r is in the format
* *// of "Wed Jun 30 21:49:08 1993\n\0"
* *printf("The time is: %s", ctime_r(&ltime, buf));
* *// replace the " " and ":" in the datetime string
* *// by "_"
* *buf[7] = "_"; *// " ", line 18

For a single symbol you need to use single quotes:

* * *buf[7] = '_';

(same everywhere).
* *buf[10] = "_"; // " ", line 19
* *buf[13] = "_"; // ":", line 20
* *buf[16] = "_"; // ":", line 21
* *buf[19] = "_"; // " ", line 22
* *buf[24] = "\0"; // remove the last \n char, line 23
* *// printf the new datetimestring
* *printf("The time is: %s", buf);
}
[..]

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Hide quoted text -

- Show quoted text -
Thanks a lot for the prompty help, Victor. It worked! The compiler
errors disappered and the space was replaced by '-'. Can you also tell
me how to remove the chars in the beginning of the string? and how to
remove a char inside the string, because i have one more space inside
the string and would like to remove it. I tried to use like:
buf[0]='', but got compiler errors like: test_ctimer.c:18:12: empty
character constant

Thanks a lot.
Aug 7 '08 #3

P: n/a
Hongyu wrote:
[..] Can you also tell
me how to remove the chars in the beginning of the string? and how to
remove a char inside the string, because i have one more space inside
the string and would like to remove it. I tried to use like:
buf[0]='', but got compiler errors like: test_ctimer.c:18:12: empty
character constant
RTFM about 'memmove' function. It should work with overlapping ranges.
You can 'memmove' part of the string over itself, IIRC.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 7 '08 #4

P: n/a
Hongyu wrote:

Thanks a lot for the prompty help, Victor. It worked! The compiler
errors disappered and the space was replaced by '-'. Can you also tell
me how to remove the chars in the beginning of the string? and how to
remove a char inside the string, because i have one more space inside
the string and would like to remove it. I tried to use like:
buf[0]='', but got compiler errors like: test_ctimer.c:18:12: empty
character constant
If you want to delete the contents and move the rest into place, you
either need to use memmove() or move the characters yourself.

See the example here:

http://www.cplusplus.com/reference/c...g/memmove.html
If you just want to overwrite them with spaces, you use ' '.


Brian
Aug 7 '08 #5

P: n/a
On Aug 7, 4:45*pm, "Default User" <defaultuse...@yahoo.comwrote:
Hongyu wrote:
Thanks a lot for the prompty help, Victor. It worked! The compiler
errors disappered and the space was replaced by '-'. Can you also tell
me how to remove the chars in the beginning of the string? and how to
remove a char inside the string, because i have one more space inside
the string and would like to remove it. I tried to use like:
buf[0]='', but got compiler errors like: test_ctimer.c:18:12: empty
character constant

If you want to delete the contents and move the rest into place, you
either need to use memmove() or move the characters yourself.

See the example here:

http://www.cplusplus.com/reference/c...g/memmove.html

If you just want to overwrite them with spaces, you use ' '.

Brian
Thank you very much, Brian, "memmove can be very very useful", as the
link you provided mentioned, which is true. I will look at in more
detail and try it.
If you just want to overwrite them with spaces, you use ' '.
No, I would like to remove them. I will use memmove as you suggested.

Have a good rest of the day, everyone.
Aug 7 '08 #6

P: n/a
On Aug 7, 4:36*pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Hongyu wrote:
[..] *Can you also tell
me how to remove the chars in the beginning of the string? and how to
remove a char inside the string, because i have one more space inside
the string and would like to remove it. I tried to use like:
buf[0]='', but got compiler errors like: test_ctimer.c:18:12: empty
character constant

RTFM about 'memmove' function. *It should work with overlapping ranges.
* You can 'memmove' part of the string over itself, IIRC.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thank you very much for the help again, Victor. I will try it.
Aug 7 '08 #7

P: n/a
On 2008-08-07 16:56:42 -0400, Hongyu <ho*******@yahoo.comsaid:
>
No, I would like to remove them. I will use memmove as you suggested.
You don't need to move them at all. Just point to the first one you care about:

char text[] = "abcdefg";
char *last4 = text + 3;
std::cout << last4 << ;\n';

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Aug 7 '08 #8

P: n/a
On Aug 7, 5:20*pm, Pete Becker <p...@versatilecoding.comwrote:
On 2008-08-07 16:56:42 -0400, Hongyu <hongyu...@yahoo.comsaid:
No, I would like to remove them. I will use memmove as you suggested.

You don't need to move them at all. Just point to the first one you care about:

char text[] = "abcdefg";
char *last4 = text + 3;
std::cout << last4 << ;\n';

--
* Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)
Thanks a lot, Pete. You are right, I see it works in the way you
shown. But since I am a entry level person, so I am not quite sure if
I will need to use the truncated string somewhere else, like use it as
an argument to invocate a function, or return this truncated string as
my void truncatedString(char * string) function, or char*
truncatedString(char* inputString) function so that it can be used
somewhere else, will it still work? Please forgive my silly questions.
Aug 7 '08 #9

P: n/a
On 2008-08-07 17:48:54 -0400, Hongyu <ho*******@yahoo.comsaid:
On Aug 7, 5:20Â*pm, Pete Becker <p...@versatilecoding.comwrote:
>On 2008-08-07 16:56:42 -0400, Hongyu <hongyu...@yahoo.comsaid:
>>No, I would like to remove them. I will use memmove as you suggested.

You don't need to move them at all. Just point to the first one you care
about:
>>
char text[] = "abcdefg";
char *last4 = text + 3;
std::cout << last4 << ;\n';

--
Â* Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Thanks a lot, Pete. You are right, I see it works in the way you
shown. But since I am a entry level person, so I am not quite sure if
I will need to use the truncated string somewhere else, like use it as
an argument to invocate a function, or return this truncated string as
my void truncatedString(char * string) function, or char*
truncatedString(char* inputString) function so that it can be used
somewhere else, will it still work? Please forgive my silly questions.
Yes, it will work. A C-style string is just an array of char teminated
by a nul character. You can use a char* to point to the first character
that you're interested in: everything from that character out to the
nul character is still a char.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Aug 7 '08 #10

P: n/a
On Aug 7, 6:03*pm, Pete Becker <p...@versatilecoding.comwrote:
On 2008-08-07 17:48:54 -0400, Hongyu <hongyu...@yahoo.comsaid:


On Aug 7, 5:20*pm, Pete Becker <p...@versatilecoding.comwrote:
On 2008-08-07 16:56:42 -0400, Hongyu <hongyu...@yahoo.comsaid:
>No, I would like to remove them. I will use memmove as you suggested.
You don't need to move them at all. Just point to the first one you care
about:
char text[] = "abcdefg";
char *last4 = text + 3;
std::cout << last4 << ;\n';
--
* Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)
Thanks a lot, Pete. You are right, I see it works in the way you
shown. But since I am a entry level person, so I am not quite sure if
I will need to use the truncated string somewhere else, like use it as
an argument to invocate a function, or return this truncated string as
my void truncatedString(char * string) function, or char*
truncatedString(char* inputString) function so that it can be used
somewhere else, will it still work? Please forgive my silly questions.

Yes, it will work. A C-style string is just an array of char teminated
by a nul character. You can use a char* to point to the first character
that you're interested in: everything from that character out to the
nul character is still a char.

--
* Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)- Hide quoted text -

- Show quoted text -
Thanks Pete for the explanation and Glad to know that.

Have a good evening.
Aug 7 '08 #11

P: n/a
Victor Bazarov wrote:
Hongyu wrote:
// by "_"
> buf[7] = "_"; // " ", line 18

For a single symbol you need to use single quotes:
Seems like unnecceary redundancy though... 'a' == "a" x == y[0] AFAICS

regards
Andy Little
Aug 7 '08 #12

P: n/a
In article <97a84565-03c7-4c12-8214-8f7bb8ef6d89
@i76g2000hsf.googlegroups.com>, ho*******@yahoo.com says...
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 chars without using a loop through one char
by one char of the string. I used the below code to achieve
replacement of " " by "_" and also removed the last "\n" char, without
considering removing the first 4 chars i.e. weekday information yet.
But even this I still didn't get what i like. Below is the code i
wrote:
You've already gotten a number of answers to your original question, but
I think it's worth pointing out that if you want a time formatted in a
specific fashion, it may be easier to use strftime instead of time. If I
understand your requirement correctly, what you want looks something
like this:

char buf[50];
time_t ltime = time(NULL);

strftime(buf, sizeof(buf), "%b_%d_%H_%M_%S_%Y", localtime(&ltime);

--
Later,
Jerry.

The universe is a figment of its own imagination.
Aug 8 '08 #13

P: n/a
On Aug 8, 7:09 am, Jerry Coffin <jcof...@taeus.comwrote:
In article <97a84565-03c7-4c12-8214-8f7bb8ef6d89
@i76g2000hsf.googlegroups.com>, hongyu...@yahoo.com says...
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 chars without using a loop through one
char by one char of the string. I used the below code to
achieve replacement of " " by "_" and also removed the last
"\n" char, without considering removing the first 4 chars
i.e. weekday information yet. But even this I still didn't
get what i like. Below is the code i
wrote:
You've already gotten a number of answers to your original
question, but I think it's worth pointing out that if you want
a time formatted in a specific fashion, it may be easier to
use strftime instead of time. If I understand your requirement
correctly, what you want looks something like this:
char buf[50];
time_t ltime = time(NULL);
strftime(buf, sizeof(buf), "%b_%d_%H_%M_%S_%Y", localtime(&ltime);
I was going to suggest that myself, if he's got access to the
tm (which may not be the case in his real code). Otherwise:

std::string result ;
std::replace_copy( buf + 4,
buf + strlen( buf ),
std::back_inserter( result ),
' ', '_' ) ;

will do everything he wants in one go. (If his input is a
string, of course, the first two arguments are
source.begin() + 4, source.end(). After checking that he has
at least 4 characters, of course.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Aug 8 '08 #14

This discussion thread is closed

Replies have been disabled for this discussion.