I am a C developer and am practicing C++ while working on the book "Programming Interviews Exposed"
String initialization with C++Question about C++ string initialization.
Page 1 of 1
11 Replies - 3165 Views - Last Post: 01 August 2010 - 06:14 PM
Replies To: String initialization with C++
#2
Re: String initialization with C++
Posted 31 July 2010 - 08:50 PM
I am a C developer and am practicing C++ while working on the book "Programming Interviews Exposed - Second Edition". This code is for the problem to print all "Permutations of a String".
Unfortunately, I am having to initialize the string as a char * and use memset:
Please suggest alternative means of declaring the string. The source code is shown below.
Ram
-----------
Unfortunately, I am having to initialize the string as a char * and use memset:
char *out = (char *)malloc (sizeof(char) * length);
//string *out = new string[length];
if ( !out ) {
fprintf(stderr, "No memory for output buffer");
}
memset(out, 0, sizeof(string) *length);
Please suggest alternative means of declaring the string. The source code is shown below.
Ram
-----------
/*
* Program to print permutations of a string
* Input : string
* Output : permutations of a string
*/
#include <iostream>
#include <stdlib.h>
using namespace std;
/* Recursive permutation, if last char print string
* Else for each letter in string , place in current
* Mark letter as used
* Permute through remaining letters.
* Mark letter as unused
*/
void doPermute (const char *in, string out, \
bool used[], int length, int level)
{
int i =0, curlength = length;
//printf ("In do Permute: %d level: %d\n", level, length);
if (level == length) {
cout <<"Output :"<<out<<endl;
//cin.ignore();
//out.resize(0);
//exit(0);
return;
}
for (i =0; i < length; i++) {
// Check flag for current character
//cout <<"Used: "<< in[i]<<" Used:"<<used[i]<<endl;
if (used[i])
continue;
out += in[i];
//cout<<"Out: "<<out<<endl;
used[i] = true;
// Recurse until last letter for all permutations
doPermute (in, out, used, length, level +1);
// Reset used flag
used[i] = false;
//REset the length of the out variable
out.resize(out.size() - 1);
//cout <<"New out:"<<out<<endl;
}
}
/* Permute over the string */
void Permute (char *str) {
int length = strlen(str);
bool used[length];
char *out = (char *)malloc (sizeof(char) * length);
//string *out = new string[length];
if ( !out ) {
fprintf(stderr, "No memory for output buffer");
}
memset(out, 0, sizeof(string) *length);
memset(used, false, sizeof(bool) * length);
doPermute(str, out, used, length, 0);
cout << "waiting for user "<<endl;
cin.ignore();
free(out);
}
int main ()
{
Permute("abcd");
return 0;
}
This post has been edited by sriramrajan: 31 July 2010 - 08:54 PM
#3
Re: String initialization with C++
Posted 31 July 2010 - 09:25 PM
Excuse me for my ignorance but why are you not allowed to initialize a string? As part of the STL a string object has a set of iterators that can allow developers to have direct access to its index. What is a string in c++? It is simply a glorified c-string in c. I don't know why you can't initialize the string. The only thing I can think of is you have not include the string library; #include <string>
This post has been edited by trixt.er: 31 July 2010 - 09:27 PM
#4
Re: String initialization with C++
Posted 31 July 2010 - 09:27 PM
I'm not entirely sure you're using memset correctly, shouldn't you be zeroing the number of bytes (char's) you've allocated?
What's the size of a pointer to a character? Wouldn't this be more appropriate?
You're mixing C and C++ which is always going to end badly, IMO.
memset(out, 0, sizeof(string) *length);
memset( out, 0, sizeof(char) * length );
You're mixing C and C++ which is always going to end badly, IMO.
This post has been edited by taylorc8: 31 July 2010 - 09:29 PM
#5
Re: String initialization with C++
Posted 31 July 2010 - 09:27 PM
You need to get a better book.

Your problem code seems to be a mix of C++ and C rather than teaching the C++ 'way'.
There is a standard string class - String class where you, the user, do not need to malloc() memory.
Your problem code seems to be a mix of C++ and C rather than teaching the C++ 'way'.
There is a standard string class - String class where you, the user, do not need to malloc() memory.
#6
Re: String initialization with C++
Posted 31 July 2010 - 09:35 PM
D: the sizeof an empty C++ string is 32 bytes on my machine.
Please see N8WXS's link to reference material for the C++ string class.
Please see N8WXS's link to reference material for the C++ string class.
#7
Re: String initialization with C++
Posted 31 July 2010 - 09:35 PM
Here are some good examples;
http://www.yolinux.c...tringClass.html
Here's the documentation;
http://www.cpprefere...ki/string/start
Just make sure to include the proper library (which is string).
There are a couple ways to initialize a string variable in c++.
The above code will output the following.
Hello World
Hey World
I love this world!
http://www.yolinux.c...tringClass.html
Here's the documentation;
http://www.cpprefere...ki/string/start
Just make sure to include the proper library (which is string).
There are a couple ways to initialize a string variable in c++.
#include <iostream>
#include <string>
using namespace std;
int main(){
// Below is a dynamically allocated string from the heap.
string *hello = new string("Hello World");
cout << *hello << endl;
delete hello; // Restoring the memory to the heap.
string hey("Hey World");
cout << hey << endl;
string love = "I love this world!";
cout << love << endl;
}
The above code will output the following.
Hello World
Hey World
I love this world!
#8
Re: String initialization with C++
Posted 31 July 2010 - 09:37 PM
** Topics merged **
#9
Re: String initialization with C++
Posted 31 July 2010 - 10:15 PM
> Programming Interviews Exposed
So do you want to learn a bunch of cheap tricks to get the job?
It's another one of those books promising an easy answer, like all those "learn C++ in 7 minutes a day" or other useless measure.
Or do you want to learn C++ to the extent that you would be able to KEEP the job once you've got it.
If you really want to learn C++ well (and not as a C/C++ kludge you seem to be heading towards), then I would suggest you start with "Accelerated C++"
http://rudbek.com/books.html
So do you want to learn a bunch of cheap tricks to get the job?
It's another one of those books promising an easy answer, like all those "learn C++ in 7 minutes a day" or other useless measure.
Or do you want to learn C++ to the extent that you would be able to KEEP the job once you've got it.
If you really want to learn C++ well (and not as a C/C++ kludge you seem to be heading towards), then I would suggest you start with "Accelerated C++"
http://rudbek.com/books.html
#10
Re: String initialization with C++
Posted 01 August 2010 - 05:22 PM
i think you should include the header file <string>
and you will be able to initialize a string without any problems in C++
and you will be able to initialize a string without any problems in C++
#11
Re: String initialization with C++
Posted 01 August 2010 - 05:55 PM
taylorc8, on 31 July 2010 - 08:27 PM, said:
I'm not entirely sure you're using memset correctly, shouldn't you be zeroing the number of bytes (char's) you've allocated?
What's the size of a pointer to a character? Wouldn't this be more appropriate?
You're mixing C and C++ which is always going to end badly, IMO.
memset(out, 0, sizeof(string) *length);
memset( out, 0, sizeof(char) * length );
You're mixing C and C++ which is always going to end badly, IMO.
I agree, I have been addicted to C for quite a while. Thanks.
Salem_c, on 31 July 2010 - 09:15 PM, said:
If you really want to learn C++ well (and not as a C/C++ kludge you seem to be heading towards), then I would suggest you start with "Accelerated C++"
http://rudbek.com/books.html
http://rudbek.com/books.html
I wish I could code in C for the interviews, would make my life easier. Thanks, I will check out this book.
#12
Re: String initialization with C++
Posted 01 August 2010 - 06:14 PM
n8wxs, on 31 July 2010 - 08:27 PM, said:
You need to get a better book.

Your problem code seems to be a mix of C++ and C rather than teaching the C++ 'way'.
There is a standard string class - String class where you, the user, do not need to malloc() memory.
Your problem code seems to be a mix of C++ and C rather than teaching the C++ 'way'.
There is a standard string class - String class where you, the user, do not need to malloc() memory.
This solved my problem with string initialization:
/* Permute over the string */
void Permute (char *str) {
int length = strlen(str);
bool used[length];
string out;
out.resize(length);
//memset(out, 0, sizeof(string) *length);
memset(used, false, sizeof(bool) * length);
doPermute(str, out, used, length, 0);
cout << "waiting for user "<<endl;
cin.ignore();
}
Page 1 of 1
|
|

New Topic/Question
Reply





MultiQuote





|