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

getting a section of a char[]

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..
Jul 18 '07 #1
6 1916
sicarie
4,677 Expert Mod 4TB
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.
Jul 18 '07 #2
sicarie
4,677 Expert Mod 4TB
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....)
Jul 18 '07 #3
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??
Jul 18 '07 #4
sicarie
4,677 Expert Mod 4TB
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. }
Jul 18 '07 #5
sicarie
4,677 Expert Mod 4TB
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)
Jul 18 '07 #6
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..
Jul 18 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

29
by: zoltan | last post by:
Hi, The scenario is like this : struct ns_rr { const u_char* rdata; }; The rdata field contains some fields such as :
6
by: arnuld | last post by:
this is the example programme from that section. it does not print the longest line otherwise it works fine, my version has only 1 extra line of "printf". i want to know why it is not printing the...
13
by: theronnightstar | last post by:
I seem to be having a problem with getline(). I have tried to find it on google, but I'm not seeing the answer. The goal of this section of code is simply to read in a line from a file to a...
12
by: arnuld | last post by:
this is exercise 2-3 from the mentioned section. i have created a solution for it but i see errors and i tried to correct them but still they are there and mostly are out of my head: ...
8
by: Cerian | last post by:
Hi there, I'm having trouble getting my 3rd dependent combo box to work in Access 2003. I have three dependent boxes named cbogroup, cbosection and cbofunction. Each one is dependent on the...
6
by: Ws | last post by:
Ok, so I have a question reguarding compilers and placing strings into the .rdata section of the PE. I was working on a program and couldn't figure out why I was getting a 0xc0000005 error...
185
by: jacob navia | last post by:
Hi We are rewriting the libc for the 64 bit version of lcc-win and we have added a new field in the FILE structure: char *FileName; fopen() will save the file name and an accessor function will...
10
by: Pranav | last post by:
#include "stdafx.h" #include<conio.h> #include<stdio.h> #include<string.h> int main() { char *str1 = "United"; char *str2 = "Front";
9
Catalyst159
by: Catalyst159 | last post by:
I have a form which is used to calculate residential Floor Area Ratio (FAR). The form is structured into seven parts as follows: Part A: Maximum FAR and Floor Area: Part B: Gross Floor Area of...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.