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..
6 1916
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.
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....)
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 - 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 - printf("Output: %i", buf[0-3]);
and it outputs '18' rather than the text it should be outputting??
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 - 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 - 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 -
for (int i =0; i <4; i++) {
-
buf2[i] = buf[i];
-
}
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)
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..
Sign in to post your reply or Sign up for a free account.
Similar topics
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 :
|
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...
|
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...
|
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:
...
|
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...
|
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...
|
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...
|
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";
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |