String initialization with C++

Question about C++ string initialization.

Page 1 of 1

11 Replies - 4004 Views - Last Post: 01 August 2010 - 06:14 PM Rate Topic: ***-- 1 Votes

#1 sriramrajan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 31-July 10

String initialization with C++

Posted 31 July 2010 - 08:44 PM

I am a C developer and am practicing C++ while working on the book "Programming Interviews Exposed"
Is This A Good Question/Topic? 0
  • +

Replies To: String initialization with C++

#2 sriramrajan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 31-July 10

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:

    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

Was This Post Helpful? 0
  • +
  • -

#3 trixt.er  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 52
  • View blog
  • Posts: 426
  • Joined: 28-September 08

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

Was This Post Helpful? 0
  • +
  • -

#4 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

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?

memset(out, 0, sizeof(string) *length);



What's the size of a pointer to a character? Wouldn't this be more appropriate?

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

Was This Post Helpful? 2
  • +
  • -

#5 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

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.
Was This Post Helpful? 3
  • +
  • -

#6 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

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.
Was This Post Helpful? 0
  • +
  • -

#7 trixt.er  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 52
  • View blog
  • Posts: 426
  • Joined: 28-September 08

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++.

#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!
Was This Post Helpful? 0
  • +
  • -

#8 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5191
  • View blog
  • Posts: 26,897
  • Joined: 10-May 07

Re: String initialization with C++

Posted 31 July 2010 - 09:37 PM

** Topics merged **
Was This Post Helpful? 0
  • +
  • -

#9 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1628
  • View blog
  • Posts: 3,090
  • Joined: 30-May 10

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
Was This Post Helpful? 1
  • +
  • -

#10 ENG.Rehab  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 01-August 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++
Was This Post Helpful? 1
  • +
  • -

#11 sriramrajan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 31-July 10

Re: String initialization with C++

Posted 01 August 2010 - 05:55 PM

View Posttaylorc8, 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?

memset(out, 0, sizeof(string) *length);



What's the size of a pointer to a character? Wouldn't this be more appropriate?

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.

View PostSalem_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


I wish I could code in C for the interviews, would make my life easier. Thanks, I will check out this book.
Was This Post Helpful? 0
  • +
  • -

#12 sriramrajan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 31-July 10

Re: String initialization with C++

Posted 01 August 2010 - 06:14 PM

View Postn8wxs, 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.


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();
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1