Connecting Tech Pros Worldwide Forums | Help | Site Map

getting a section of a char[]

Newbie
 
Join Date: Jul 2007
Posts: 3
#1: Jul 18 '07
I am an experienced PHP coder and am trying my hand at C++

I need to:
>get the first 4 characters of a char variable
>get the characters before a certain set of characters
>raise a char to upper case
>lower a char to lower case

in PHP i would just use substr(), strtok(), strtoupper() and strtolower()

How do i do these in C++? (i am using gcc on linux)

Sorry for the extremely basic questions, google hasn't been very good at answering them..



sicarie's Avatar
Moderator
 
Join Date: Nov 2006
Location: USA
Posts: 3,929
#2: Jul 18 '07

re: getting a section of a char[]


Those are pretty much the same functions in C++. (Include the string.h library).

As you've named it a char, you can also get the first 4 by referencing char[0-3]

Sorry, got caught up in something.

This is cplusplus.com's string page, which is a pretty good reference, and you can search for strtok() and substr() and the others in there as well.
sicarie's Avatar
Moderator
 
Join Date: Nov 2006
Location: USA
Posts: 3,929
#3: Jul 18 '07

re: getting a section of a char[]


Just realized the strtolower() and upper weren't in there, but I found this nifty little thing in the STL

std::transform (s.begin(),s.end(), s.begin(), tolower)

(or toupper in place of tolower....)
Newbie
 
Join Date: Jul 2007
Posts: 3
#4: Jul 18 '07

re: getting a section of a char[]


The functions on cplusplus.com's string page only work on string variables, i need to perform the functions on char[] variables as i need to printf them and printf will only accept char[] variables..

I'm also having a weird issue where when i call a char with numbers in the brackets (char[0-3] etc) it claims it returns an int?

i have something like
Expand|Select|Wrap|Line Numbers
  1. printf("Output: %s", buf[0-3]);
and the compiler returns that:
'Format '%s' expects type 'char*' but argument 2 has type 'int''

I have just tried
Expand|Select|Wrap|Line Numbers
  1. printf("Output: %i", buf[0-3]);
and it outputs '18' rather than the text it should be outputting??
sicarie's Avatar
Moderator
 
Join Date: Nov 2006
Location: USA
Posts: 3,929
#5: Jul 18 '07

re: getting a section of a char[]


Quote:

Originally Posted by Mudface

The functions on cplusplus.com's string page only work on string variables, i need to perform the functions on char[] variables as i need to printf them and printf will only accept char[] variables..

I'm also having a weird issue where when i call a char with numbers in the brackets (char[0-3] etc) it claims it returns an int?

i have something like

Expand|Select|Wrap|Line Numbers
  1. printf("Output: %s", buf[0-3]);
and the compiler returns that:
'Format '%s' expects type 'char*' but argument 2 has type 'int''

I have just tried
Expand|Select|Wrap|Line Numbers
  1. printf("Output: %i", buf[0-3]);
and it outputs '18' rather than the text it should be outputting??

Oh, sorry about that last one - that was more pseudocode, buf[0] is your first bit, and buf[3] is your last, if you wanted to copy in a for loop

Expand|Select|Wrap|Line Numbers
  1. for (int i =0; i <4; i++) {
  2.    buf2[i] = buf[i];
  3. }
sicarie's Avatar
Moderator
 
Join Date: Nov 2006
Location: USA
Posts: 3,929
#6: Jul 18 '07

re: getting a section of a char[]


Here is the strtok() page.

C++ is interesting in the way its strings are created. There are actually two types, the main difference is that c style are null terminated. But both can be manipulated as arrays of chars - you can pass the reference to the array (try using the dereferencing '&' operator if it won't take buf )

Edit:: you will most likely either use buf as the reference or &buf, maybe with braces ( [ ] ), I'm kinda on my way out the door. That site has good tutorials http://www.cplusplus.com/doc/tutorial/, I found them very useful when using new functions.

Here's the one on char sequences

And here's the intro to strings (most of the way down)
Newbie
 
Join Date: Jul 2007
Posts: 3
#7: Jul 18 '07

re: getting a section of a char[]


thank you :)

even buf[0] etc returned integers rather than char variables, the for loop was what i tried first but always produced an integer for some weird reason...

ill give all those links a go..
Reply