In view of my better understanding of c-style string and C++ string, this is an update of some codes posted by namehere05. Some of the new things I learnt are as follows:
1. Add the line "using namespace std;" in order to obviate the need to use std:: before cout and endl, etc., like std::cout <<... to save typing std::
2. C-style strings are arrays. Their elements can be accessed and manipulated, like C++ string.
3. When pointers are used for char* data type, elements of a C-style string, say c can be accessed, but can not be replaced with, say c[3]='h'. However for normal C-Style string declared like chax[]="123.456", elements can be replaced with, say chax[3]='0';
4. strtod() function can be used for the conversion of C-Style string variable to numeric variable for C-Style string declared normally with [] or as pointer type C-Style string. See updated codes below:
Please note that #include <string> is only necessary for Borland. CODE::BLOCK and Dev-C++ do not need it.
// string5 originally written by namehere05.
#include <iostream>
#include <string>
using namespace std;
int main() {
system ("color 0a"); // green text on black bkgrd
double numeric;
string str;
char *endptr;
char * c = "No cover!"; // declaration of c-style string
char chax[] = "123.45"; // Declaraition of c-style string
string ch = "123.45"; // Decalration of c-style string
string* pc;
str = chax; //you can assign a c-string to a string
cout << "\nSTRING MANPULATIONS AND CONVERSION TO NUMERIC VARIABLE\n\n";
cout << "\nC-STYLE STRING chax";
char *cstr = "Hello";
printf( "\nThe second character of %s is %c.\n",
cstr, cstr[1] );
cout << "\nBefore replacement of 3rd char with X, chax : " << chax;
chax[2]='X';
cout << "\nAfter replacement of 3rd char with X,chax : " << chax;
chax[2]='3';
cout << "\nAfter replacement of 3rd char with 3, chax : " << chax;
cout << "\n\nC++ STRING str and C-STYLE STRING chax MANIPULATION ";
cout << "\n\nstr's initial value : "<< str;
str[3]='0';//you can access the string as if it were an array
cout << "\nstr after str[3]='0' : " << str;
pc = &ch; // pc is assigned address of c
cout << "\n\nch is assigned value : " << ch;
cout << "\nAddress of &ch assigned to pc : " << pc;
cout << "\nContent pointed by *pc : " << *pc;
cout << "\n\nchax c-style = " << chax ;
str=chax;
cout << "\nstr = chax = " << str << endl ;
//cout << "\nc2 from conv = " << c2 << std::endl ;
// C-style string manipulation
chax[3] = 'h';
cout << "\nafter chax[3] = 'h', content of chax is : " << chax;
str[3]='0';
cout << "\nafter str[3] = '0', content of str is : " << str;
char const * c2 = str.c_str(); //it has to be const
numeric = strtod(chax,&endptr);
cout << "\n\nnumeric from strtod conversion of chax ignoring illegal char onwards : " << numeric;
numeric = strtod(c2,&endptr);
cout << "\n\nnumeric from strtod conversion of str where all numbers are converted : " << numeric;
std::cout << "\n\n";
system("pause");
}
Some notes on pointer type string courtesy of
www.cplusplus.com is reproduced below:
Declaring variables of pointer types
Due to the ability of a pointer to directly refer to the value that it points to, it becomes necessary to specify in its declaration which data type a pointer is going to point to. It is not the same thing to point to a char as to point to an int or a float.
The declaration of pointers follows this format:
type * name;
where type is the data type of the value that the pointer is intended to point to. This type is not the type of the pointer itself! but the type of the data the pointer points to. For example:
int * number;
char * character;
float * greatnumber;
These are three declarations of pointers. Each one is intended to point to a different data type, but in fact all of them are pointers and all of them will occupy the same amount of space in memory (the size in memory of a pointer depends on the platform where the code is going to run). Nevertheless, the data to which they point to do not occupy the same amount of space nor are of the same type: the first one points to an int, the second one to a char and the last one to a float. Therefore, although these three example variables are all of them pointers which occupy the same size in memory, they are said to have different types: int*, char* and float* respectively, depending on the type they point to.
I want to emphasize that the asterisk sign (*) that we use when declaring a pointer only means that it is a pointer (it is part of its type compound specifier), and should not be confused with the dereference operator that we have seen a bit earlier, but which is also written with an asterisk (*). They are simply two different things represented with the same sign.
Comments please if any.
Regards,