i wish to get different combination from a String. For example: String^ str = "a,b,c,d,e";
i will wan to separate the string into individual letters and form all different combination like (a,b), (a,c,d) or (a,b,c,d) etc..
is there any simple methods to do so juz using loop?? im still a beginner in C++ so i dun really noe any built in methods. But if haf i will wish to learn too. pls help!! thx
Combination of String in C++?
I know of no easy way of doing the task you want to accomplish using a built in library function of C/C++. Basically, there are a few things you will need to do:
1. Parse out each of the comma separated characters and create a set containing each of those characters (this set could be stored in an array of chars).
2. Print the power set of the character set you have. The power set is the formal name for what you are looking to print. A simple algorithm for this would be to start with the first element in your list and combine that with each of the characters after it. Once all combinations involving the first character have been exhausted, move on to the second but only do combinations of the second not involving the first character which came before it. Do this for each character in the set and your task will be accomplished.
This problem is ripe for recursion if you are so inclined.
Tuesday, July 14, 2009
String manipulation C++?
Hi i am working on a small program where a user enters a string which should be more than 8 characters. Then once they have entered a string, the program should do the following:
1) output the size of the sentence
2) output the 1st 3rd 5th and 7th letter and append it to the end of the inputted sentence.
This is what i have done so far but i am getting errors and i am new to c++:
#include %26lt;iostream%26gt;
#include %26lt;string.h%26gt;
#include %26lt;sstream%26gt;
using namespace std;
int main()
{
int iQuit;
char str[7];
cout%26lt;%26lt; "Enter sentence \n";
getline(cin,str);
cout%26lt;%26lt; "Size of the sentence" %26lt;%26lt; str.length() %26lt;%26lt; endl;
cin %26gt;%26gt; iQuit;
return 0;
}
Can someone help on this and the second part please? i could probably do this in java but i need to do in C++.
I would appreciate it if someone can help me on tell me they have done in such a way?
String manipulation C++?
There are two main kinds of string in C++, the null terminated strings inherited from C and the C++ string object. Using the latter makes your assignment a cinch. Unless you are instructed otherwise you should begin to use string objects and move away from null terminated strings whenever possible.
#include %26lt;iostream%26gt;
#include %26lt;string%26gt;
using namespace std;
int main()
{
string input;
while (input.length() %26lt;= 8)
{
cout%26lt;%26lt; "Enter sentence at least 8 characters in length\n";
getline(cin, input);
}
cout%26lt;%26lt; "Size of the sentence = " %26lt;%26lt; input.length() %26lt;%26lt; endl;
input += input[2];
input += input[4];
input += input[6];
cout %26lt;%26lt; "appended sentence is: " %26lt;%26lt; input %26lt;%26lt; endl;
return(0);
}
Reply:Try this code for starters. Can you spot the changes?
#include %26lt;iostream%26gt;
#include %26lt;string.h%26gt;
#include %26lt;sstream%26gt;
using namespace std;
int main()
{
int iQuit;
string input;
cout%26lt;%26lt; "Enter sentence \n";
getline(cin, input);
cout%26lt;%26lt; "Size of the sentence " %26lt;%26lt; input.length() %26lt;%26lt; endl;
cin %26gt;%26gt; iQuit;
return 0;
}
Reply:You can't say str.length(). You have to use the function strlen(), like this:
cout%26lt;%26lt; "Size of the sentence" %26lt;%26lt; strlen(str) %26lt;%26lt; endl;
To output the 1st, 3rd, 5th and 7th letters you have to access them from the string/character-array. This is done like so: str[0] is the 1st. str[2] is the 3rd letter, etc.
Appending is easy. Just make a new character array that is bigger, copy the inputted string to it (using the strcpy function) and then assign the letters that you want to the new string.
forsythia
1) output the size of the sentence
2) output the 1st 3rd 5th and 7th letter and append it to the end of the inputted sentence.
This is what i have done so far but i am getting errors and i am new to c++:
#include %26lt;iostream%26gt;
#include %26lt;string.h%26gt;
#include %26lt;sstream%26gt;
using namespace std;
int main()
{
int iQuit;
char str[7];
cout%26lt;%26lt; "Enter sentence \n";
getline(cin,str);
cout%26lt;%26lt; "Size of the sentence" %26lt;%26lt; str.length() %26lt;%26lt; endl;
cin %26gt;%26gt; iQuit;
return 0;
}
Can someone help on this and the second part please? i could probably do this in java but i need to do in C++.
I would appreciate it if someone can help me on tell me they have done in such a way?
String manipulation C++?
There are two main kinds of string in C++, the null terminated strings inherited from C and the C++ string object. Using the latter makes your assignment a cinch. Unless you are instructed otherwise you should begin to use string objects and move away from null terminated strings whenever possible.
#include %26lt;iostream%26gt;
#include %26lt;string%26gt;
using namespace std;
int main()
{
string input;
while (input.length() %26lt;= 8)
{
cout%26lt;%26lt; "Enter sentence at least 8 characters in length\n";
getline(cin, input);
}
cout%26lt;%26lt; "Size of the sentence = " %26lt;%26lt; input.length() %26lt;%26lt; endl;
input += input[2];
input += input[4];
input += input[6];
cout %26lt;%26lt; "appended sentence is: " %26lt;%26lt; input %26lt;%26lt; endl;
return(0);
}
Reply:Try this code for starters. Can you spot the changes?
#include %26lt;iostream%26gt;
#include %26lt;string.h%26gt;
#include %26lt;sstream%26gt;
using namespace std;
int main()
{
int iQuit;
string input;
cout%26lt;%26lt; "Enter sentence \n";
getline(cin, input);
cout%26lt;%26lt; "Size of the sentence " %26lt;%26lt; input.length() %26lt;%26lt; endl;
cin %26gt;%26gt; iQuit;
return 0;
}
Reply:You can't say str.length(). You have to use the function strlen(), like this:
cout%26lt;%26lt; "Size of the sentence" %26lt;%26lt; strlen(str) %26lt;%26lt; endl;
To output the 1st, 3rd, 5th and 7th letters you have to access them from the string/character-array. This is done like so: str[0] is the 1st. str[2] is the 3rd letter, etc.
Appending is easy. Just make a new character array that is bigger, copy the inputted string to it (using the strcpy function) and then assign the letters that you want to the new string.
forsythia
C++ programing, this is my study guide can you help me get the right answers?
1. Making a variable behave like a variable of a different data type is known as
a.typecasting
b.brute force
c.intimidation
d.variable casting
2. To redirect the input for a C++ program (theprog.exe) to come from a file rather than the keyboard you would:
a.theprog %26lt; infile.txt
b.theprog %26lt;%26lt; infile.txt
c.theprog %26gt; infile.txt
d.theprog %26gt;%26gt; infile.txt
3. if a do/while structure is used,
a) an infinite loop will not take place
b) counter controlled repetition is not possible
c) the body of the loop will execute at least once
d) an off-by-one error will not occur
4. A function prototype does not have to
a)include parameter names
b)terminate with a semicolon
c)agree with the function definition
d)match with all calls to the function
5. A function prototype does not have to
a)include parameter names
b)terminate with a semicolon
c)agree with the function definition
d)match with all calls to the function
6. For command line arguments you write your main function:
int main(int argc, char *argv[])
The argc refers to:
a.the count of the arguments passed
b.an array of pointers to strings
c.the number of people using the program
d.the size in bytes of the program
C++ programing, this is my study guide can you help me get the right answers?
1. a.
2. d.
3. c.
4. d.
5. isn't this #4?
6. a.
Reply:1. a
2. a -- I think
3. c
4.a - i think
5. same as 4 ?
6. a
Number 4 - example of prototype
void function(int number) will work if you do this as well
void function(int) I don't think poster above said it right
I know this will work.
Man whoever gave you the study guide -- really likes a answers :)
number 2 - site below answers the question. And thats what I thought before I went and looked, so I am right
a.typecasting
b.brute force
c.intimidation
d.variable casting
2. To redirect the input for a C++ program (theprog.exe) to come from a file rather than the keyboard you would:
a.theprog %26lt; infile.txt
b.theprog %26lt;%26lt; infile.txt
c.theprog %26gt; infile.txt
d.theprog %26gt;%26gt; infile.txt
3. if a do/while structure is used,
a) an infinite loop will not take place
b) counter controlled repetition is not possible
c) the body of the loop will execute at least once
d) an off-by-one error will not occur
4. A function prototype does not have to
a)include parameter names
b)terminate with a semicolon
c)agree with the function definition
d)match with all calls to the function
5. A function prototype does not have to
a)include parameter names
b)terminate with a semicolon
c)agree with the function definition
d)match with all calls to the function
6. For command line arguments you write your main function:
int main(int argc, char *argv[])
The argc refers to:
a.the count of the arguments passed
b.an array of pointers to strings
c.the number of people using the program
d.the size in bytes of the program
C++ programing, this is my study guide can you help me get the right answers?
1. a.
2. d.
3. c.
4. d.
5. isn't this #4?
6. a.
Reply:1. a
2. a -- I think
3. c
4.a - i think
5. same as 4 ?
6. a
Number 4 - example of prototype
void function(int number) will work if you do this as well
void function(int) I don't think poster above said it right
I know this will work.
Man whoever gave you the study guide -- really likes a answers :)
number 2 - site below answers the question. And thats what I thought before I went and looked, so I am right
C++ prog. Problems with a String class. (the same as in my previous question): what's wrong with it? copy fct?
.h::::::::::::::::::::::::::::::::::::::...
#ifndef STRING_H
#define STRING_H
#include %26lt;iostream%26gt;
namespace HomeMadeString
{
class String
{
unsigned int elementsNum;
char*pData;
public:
String()
{
elementsNum=0;
char*pData=NULL;
}
String(String %26amp;theOther);
String(const char* c1);
String(char c, unsigned int times);
~String()
{
delete[] pData;
}
void getStr(char * pBuff);
unsigned int getLength(){return elementsNum;}
void print(std::ostream%26amp; os);
char getChar(unsigned int pos);
static String concatenate(String string1, String string2);
static bool compare(String string1, String string2);
static void copy(String%26amp; string1, String string2);//it copies the second string into the first
};
}
#endif
.cpp::::::::::::::::::::::::::::::::
How to write the "copy" function?
The "print" function?
Please help!
Domonkos
C++ prog. Problems with a String class. (the same as in my previous question): what's wrong with it? copy fct?
You get that char*pData inside your constructor is a separate declaration, it does not reference the class-scope variable with the same name (and it falls out of scope when the constructor returns.
Also, your code is conspicuously lacking any allocations -- don't rely on memory allocated by the caller, it could go away at any time.
More, avoid implementation inside of the .h file, it will bite you in the butt when your projects become complex.
Copying an object is usually done as a self-referential overload of the constructor, accepts parameter of its own class.
Not sure what you want print() to do, print to a printer? Doesn't seem to make much sense. If you mean print to console, what's wrong with printf() or stream output?
Good Luck
#ifndef STRING_H
#define STRING_H
#include %26lt;iostream%26gt;
namespace HomeMadeString
{
class String
{
unsigned int elementsNum;
char*pData;
public:
String()
{
elementsNum=0;
char*pData=NULL;
}
String(String %26amp;theOther);
String(const char* c1);
String(char c, unsigned int times);
~String()
{
delete[] pData;
}
void getStr(char * pBuff);
unsigned int getLength(){return elementsNum;}
void print(std::ostream%26amp; os);
char getChar(unsigned int pos);
static String concatenate(String string1, String string2);
static bool compare(String string1, String string2);
static void copy(String%26amp; string1, String string2);//it copies the second string into the first
};
}
#endif
.cpp::::::::::::::::::::::::::::::::
How to write the "copy" function?
The "print" function?
Please help!
Domonkos
C++ prog. Problems with a String class. (the same as in my previous question): what's wrong with it? copy fct?
You get that char*pData inside your constructor is a separate declaration, it does not reference the class-scope variable with the same name (and it falls out of scope when the constructor returns.
Also, your code is conspicuously lacking any allocations -- don't rely on memory allocated by the caller, it could go away at any time.
More, avoid implementation inside of the .h file, it will bite you in the butt when your projects become complex.
Copying an object is usually done as a self-referential overload of the constructor, accepts parameter of its own class.
Not sure what you want print() to do, print to a printer? Doesn't seem to make much sense. If you mean print to console, what's wrong with printf() or stream output?
Good Luck
C++ study guide help?
1.Which of the following can contain data items of different types?
a.an array
b.a structure
c.a string
d.none of the above; they can only contain different values
2.For a main function defined as following:
void main(int argc, char * argv[])
Which statement below is NOT correct?
a.The first argument tells how many separate arguments were passed.
b.The second argument represents an array of strings.
c.The first (i.e. the 0th) argument is the name of the program
d.The first (i.e. the 1st) argument is the name of the program
e.none of the above
3.In C++, all variables
a.take up the same amount of storage on a certain computer type
b.take up the same amount of storage on all computer types
c.take up different amounts of storage depending on the data type
d.take up different amounts of storage depending on the length of the variable name
4.Every element of any array takes up one byte in memory
a.true
b.false
5.In object oriented programming the constructor
a.is the method called when you create an instance of that class
b.has the same name as the name of the class
c.is used to initialize variables
d.all of the above
C++ study guide help?
1-a , 2-e(maybe) 3-c 4-b 5-d
a.an array
b.a structure
c.a string
d.none of the above; they can only contain different values
2.For a main function defined as following:
void main(int argc, char * argv[])
Which statement below is NOT correct?
a.The first argument tells how many separate arguments were passed.
b.The second argument represents an array of strings.
c.The first (i.e. the 0th) argument is the name of the program
d.The first (i.e. the 1st) argument is the name of the program
e.none of the above
3.In C++, all variables
a.take up the same amount of storage on a certain computer type
b.take up the same amount of storage on all computer types
c.take up different amounts of storage depending on the data type
d.take up different amounts of storage depending on the length of the variable name
4.Every element of any array takes up one byte in memory
a.true
b.false
5.In object oriented programming the constructor
a.is the method called when you create an instance of that class
b.has the same name as the name of the class
c.is used to initialize variables
d.all of the above
C++ study guide help?
1-a , 2-e(maybe) 3-c 4-b 5-d
ASP 2.0 C# Access Connection String?
Can someone post a sample connection string to connect to an Access mdb file (not using SQL) in C#? I am using ASP.NET 2.0, but can only find SQL connection strings. Thanks.
ASP 2.0 C# Access Connection String?
http://www.carlprothman.net/Default.aspx...
You will always use a SQL statement to gather your info from the db.
Reply:Here is a sample.
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\data\rebuilt.MDB
You can click on the top menu "tools" selection and select
"Connect to Database..." Select "Microsoft Access Database File", click Continue, browse to the database to open, and the connection will be added to your project.
jasmine
ASP 2.0 C# Access Connection String?
http://www.carlprothman.net/Default.aspx...
You will always use a SQL statement to gather your info from the db.
Reply:Here is a sample.
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\data\rebuilt.MDB
You can click on the top menu "tools" selection and select
"Connect to Database..." Select "Microsoft Access Database File", click Continue, browse to the database to open, and the connection will be added to your project.
jasmine
Is there any situation in which it would be useful to create a matrix of bools or string in C++?
Lets say we have a Matrix program in C++, and we are using template, I want to know if you have an example of a situation in which it would be useful to create a matrix of bools. How about a matrix of strings?
Is there any situation in which it would be useful to create a matrix of bools or string in C++?
mmm.. yeah... think about it. name yes/no
Reply:a matrix of bools could represent a very simple bitmap (in black/white), or possibly a map of some kind that indicates specific points of interest.
if it's a sparse-matrix, then you can get away with simulating it using a chained hashtable, which will save memory %26amp; time.
Is there any situation in which it would be useful to create a matrix of bools or string in C++?
mmm.. yeah... think about it. name yes/no
Reply:a matrix of bools could represent a very simple bitmap (in black/white), or possibly a map of some kind that indicates specific points of interest.
if it's a sparse-matrix, then you can get away with simulating it using a chained hashtable, which will save memory %26amp; time.
Subscribe to:
Posts (Atom)