Given the following declarations:
char a[ ] = "abc", *p = "def";
What is the effect of the following statement?
p=a
a. It copies the string "abc" into string p
b. It changes p to point to string "abc"
c. It copies the first character of a ton the first character of p
d. It generates an error
C programming question?
haha cheater.
ya D) is looking good.
Reply:Genrates Error
Reply:b.
p is a pointer to char.
The name of the array a is also a pointer to char.
Reply:b. p will point to a which contains "abc".
Reply:The answer is b.
I just wrote a little code to do it, although I should have been smart and just looked at K%26amp;R.
Reply:p will then point to the first position of a, it won't point to the whole string but you can use pointer arithmatic to get the rest of the string. So - E. none of the above.
Reply:Usually it would be b. Because you replaced the adress the pointer points at. But i am not quite sure you can declare a pointer on a string constant (*p="def") - it would probably generate an error.
Monday, May 24, 2010
How do you reverse a string? In other words if I have "It is hot today", I want to get "today hot is It".
How do you reverse a string in C/C++?
How do you reverse a string? In other words if I have "It is hot today", I want to get "today hot is It".
hmmm, sounds like you should read your text, perhaps post some code and then ask for some help. this might lead you in the right way:
#include%26lt;iostream%26gt;
#include%26lt;string%26gt;
int main()
{
using namespace std;
string txt;
cout%26lt;%26lt;"Enter anything, come on, anything...\n";
getline(cin,txt);
string::iterator end = txt.end( ) - 1;
for( ; end != txt.begin( ) - 1; end--)
{
cout %26lt;%26lt; *end;
}
cout %26lt;%26lt; endl;
system("pause");
return 0 ;
}
Reply:just reverse the string i guess
and in arrays if it from 1 to 5 it will count 12345 but if u reverse it as 5 to 1 , it will show 54321
good luck
Reply:Technically you are not reversing a string. You are reversing a subset of items inside a string.
So you would have to create an array of strings. Parse the contents of your original string by some separator, such as a space.
Then just reverse loop through the array of strings.
How do you reverse a string? In other words if I have "It is hot today", I want to get "today hot is It".
hmmm, sounds like you should read your text, perhaps post some code and then ask for some help. this might lead you in the right way:
#include%26lt;iostream%26gt;
#include%26lt;string%26gt;
int main()
{
using namespace std;
string txt;
cout%26lt;%26lt;"Enter anything, come on, anything...\n";
getline(cin,txt);
string::iterator end = txt.end( ) - 1;
for( ; end != txt.begin( ) - 1; end--)
{
cout %26lt;%26lt; *end;
}
cout %26lt;%26lt; endl;
system("pause");
return 0 ;
}
Reply:just reverse the string i guess
and in arrays if it from 1 to 5 it will count 12345 but if u reverse it as 5 to 1 , it will show 54321
good luck
Reply:Technically you are not reversing a string. You are reversing a subset of items inside a string.
So you would have to create an array of strings. Parse the contents of your original string by some separator, such as a space.
Then just reverse loop through the array of strings.
How can i manipulate string to know whether the substring is contained in the string?
how can i manipulate string in c++ to know whether the substring is contained or have a pattern in the string?
but,without using strcmp.
How can i manipulate string to know whether the substring is contained in the string?
Saw this yesterday...
Reply:in C/ C++ i would go for the library function strstr();
in JAVA, str.contains(str1) would do the trick, where str is the string in which str1 has to be searched
800flowers.com
but,without using strcmp.
How can i manipulate string to know whether the substring is contained in the string?
Saw this yesterday...
Reply:in C/ C++ i would go for the library function strstr();
in JAVA, str.contains(str1) would do the trick, where str is the string in which str1 has to be searched
800flowers.com
C++ Pointers to Strings?
I'm writing a C++ program in 3 separate files. Here's some of the pseudocode:
Rectangle.h
_________
...
private *char name;
rectangle
deconstructor
getName
setName
draw
Rectangle.cpp
_________
default constructor: name = "bleh";
constructor: [asks the user to enter a name and prints it back]
draw: [print name]
getName: gets the name
setName: modifies the name
deconstructor: deletes the string
TestRectangle.cpp
___________
...
Rectangle r1;
r1.draw();
r1.getName();
How do I... er... do all of this? I have to allocate memory for the string using the new keyword.
I've been trying to look this stuff up but can't seem to find anything helpful.
C++ Pointers to Strings?
"Algorithms + Data Structures = Programs", by Wirth.
You evidently learned C++ before you learned programming. Take a break and learn programming now.
Rectangle.h
_________
...
private *char name;
rectangle
deconstructor
getName
setName
draw
Rectangle.cpp
_________
default constructor: name = "bleh";
constructor: [asks the user to enter a name and prints it back]
draw: [print name]
getName: gets the name
setName: modifies the name
deconstructor: deletes the string
TestRectangle.cpp
___________
...
Rectangle r1;
r1.draw();
r1.getName();
How do I... er... do all of this? I have to allocate memory for the string using the new keyword.
I've been trying to look this stuff up but can't seem to find anything helpful.
C++ Pointers to Strings?
"Algorithms + Data Structures = Programs", by Wirth.
You evidently learned C++ before you learned programming. Take a break and learn programming now.
C++: Converting unsigned int to int?
I want to perform a little calculation using a length of a string in C++. Im new to this. :(
Example:
string myString = "Hello";
int myCalc;
myCalc = 100-(myString.Length); // is this correct? I want to minus string's length from 100.
But it gives compile error: "error C2440: 'type cast' : cannot convert from 'unsigned int (__thiscall std::basic_string%26lt;char,struct std::char_traits%26lt;char%26gt;,class std::allocator%26lt;char%26gt; %26gt;::*' to 'int'
Conversion is a valid standard conversion, which can be performed implicitly or by use of static_cast, C-style cast or function-style cast"
I tried casting with "int(myString.Length)" but it doesnt work. :(
Please help me out.
C++: Converting unsigned int to int?
length in standard C++ is a function. You need to call it using myString.length()
There shouldn't be a problem putting an unsigned int into an int variable. The compiler is probably complaining that you're trying to convert a function (length) into an int, since you left out the parentheses.
Reply:thanx others too! Report It
Reply:I haven't learned C++ yet( opertune word there is yet) but in Visual Basic when you want to utilize a Str varialble and a Int variable you must declare them seperately and then use the
Val() option to translate. this will allow you to set arguments for converting string varialbes to integer or vise versa.
example intcalc = Val(strCalc)
dont know if that works or if it is even support in C++ but it never hurts to take a look.
Reply:try making myCalc unsigned...
unsigned int myCalc...might work!
Additional details(added later on)
man o man
1) there is nothing that is Length it is length (C++ is case sensitive)
2) it is function so correct call would be myString.length()
then it runs on gcc compiler
best of luck!
Reply:C++ I managed to avoid thus far, but I think AM post's is correct. Make myCalc an unsigned int.
I don't know how many bytes an int type in C++ is, but for the sake of illustration, lets say it is 1 byte.
An unsigned int can take on the values 0..255
Int can be -128..0..127.
Obviously you cannot stuff an insigned int into an int. The type cast can't help you because you run into the same problem. It can't map the silly things.
-Dio
Reply:You want to cast using "(unsigned int)", like this:
myCalc = 100-(unsigned int) (myString.Length);
Now, if you change the declaration of myCalc to
unsigned int myCalc;
you could avoid the whole issue. However, if the string length is greater than 100, you'll get a very large number as a result, which could be bad.
Example:
string myString = "Hello";
int myCalc;
myCalc = 100-(myString.Length); // is this correct? I want to minus string's length from 100.
But it gives compile error: "error C2440: 'type cast' : cannot convert from 'unsigned int (__thiscall std::basic_string%26lt;char,struct std::char_traits%26lt;char%26gt;,class std::allocator%26lt;char%26gt; %26gt;::*' to 'int'
Conversion is a valid standard conversion, which can be performed implicitly or by use of static_cast, C-style cast or function-style cast"
I tried casting with "int(myString.Length)" but it doesnt work. :(
Please help me out.
C++: Converting unsigned int to int?
length in standard C++ is a function. You need to call it using myString.length()
There shouldn't be a problem putting an unsigned int into an int variable. The compiler is probably complaining that you're trying to convert a function (length) into an int, since you left out the parentheses.
Reply:thanx others too! Report It
Reply:I haven't learned C++ yet( opertune word there is yet) but in Visual Basic when you want to utilize a Str varialble and a Int variable you must declare them seperately and then use the
Val() option to translate. this will allow you to set arguments for converting string varialbes to integer or vise versa.
example intcalc = Val(strCalc)
dont know if that works or if it is even support in C++ but it never hurts to take a look.
Reply:try making myCalc unsigned...
unsigned int myCalc...might work!
Additional details(added later on)
man o man
1) there is nothing that is Length it is length (C++ is case sensitive)
2) it is function so correct call would be myString.length()
then it runs on gcc compiler
best of luck!
Reply:C++ I managed to avoid thus far, but I think AM post's is correct. Make myCalc an unsigned int.
I don't know how many bytes an int type in C++ is, but for the sake of illustration, lets say it is 1 byte.
An unsigned int can take on the values 0..255
Int can be -128..0..127.
Obviously you cannot stuff an insigned int into an int. The type cast can't help you because you run into the same problem. It can't map the silly things.
-Dio
Reply:You want to cast using "(unsigned int)", like this:
myCalc = 100-(unsigned int) (myString.Length);
Now, if you change the declaration of myCalc to
unsigned int myCalc;
you could avoid the whole issue. However, if the string length is greater than 100, you'll get a very large number as a result, which could be bad.
B.C. Rich Warlock strings help...?
Alright, so as you saw I have a B.C. Rich Bronze Series Warlock guitar (red). I got it a little while ago, and just today one of my strings broke. It was the e (thinnest) string. I need help with getting it replaced. I'm a beginner and this is my first electric guitar. I read online that some some strings break real easily on it, so I need a kind of string that won't =]. Can I do it by myself or should I give it to the store? If I do it myself (which I'll probably get the previous owner of the guitar to do it for me) what kind of strings should I buy? Name, place available, and price please. Thanks a lot guys!!!
P.S. I know this is silly but I have no idea what I'm doing, except for playing, with a guitar. =D
B.C. Rich Warlock strings help...?
ok heres the deal...
you can buy new strings and do it yourself its really easy
you want to get a first string "e" with the gauge size being "9" %26lt;---IMPORTANT! also look into a stringing tool. they're really cheap and worth the money. they make stringing a guitar really easy instead of turning the peg by hand (hand cramps suck).
BUT! Heres The Problem. You Need To Find The Same Brand That Comes On Your Guitar. Obviously B.C. Rich Strings.
They're Strings Tend To Sound Different Than Others. You're Best Option To Get The Best Sound Out Of Your Guitar Is To Either Buy A New B.C. Rich "E" String Or Buy A Whole New Package Of Strings Which Really Isnt A Bad Idea.
It Is Good To Change Your Strings Every Once In A While
Good Brands To Look For Would Be "Ernie Ball", "Elixir", And Of Course B.C. Rich. Remember To Get 9 gauge Strings
Reply:ernie ball bad they will break blue steel are great but there steel they will wear your frets faster if you realy want some good strings elixer they are 13 dollars tho but great tone Report It
P.S. I know this is silly but I have no idea what I'm doing, except for playing, with a guitar. =D
B.C. Rich Warlock strings help...?
ok heres the deal...
you can buy new strings and do it yourself its really easy
you want to get a first string "e" with the gauge size being "9" %26lt;---IMPORTANT! also look into a stringing tool. they're really cheap and worth the money. they make stringing a guitar really easy instead of turning the peg by hand (hand cramps suck).
BUT! Heres The Problem. You Need To Find The Same Brand That Comes On Your Guitar. Obviously B.C. Rich Strings.
They're Strings Tend To Sound Different Than Others. You're Best Option To Get The Best Sound Out Of Your Guitar Is To Either Buy A New B.C. Rich "E" String Or Buy A Whole New Package Of Strings Which Really Isnt A Bad Idea.
It Is Good To Change Your Strings Every Once In A While
Good Brands To Look For Would Be "Ernie Ball", "Elixir", And Of Course B.C. Rich. Remember To Get 9 gauge Strings
Reply:ernie ball bad they will break blue steel are great but there steel they will wear your frets faster if you realy want some good strings elixer they are 13 dollars tho but great tone Report It
C/C++ pointers as strings?
I am a relative novice at C++, and I am trying to write a stack class that holds lines of text. I need to hold everything in pointers because I don't know the sizes of things, and I have one question. Do you have to use new or malloc to allocate adequate memory to a pointer, or will it act intelligently? For example will:
char *string = new char;
std::cin %26gt;%26gt; string;
instead of:
char *string = new char[100];
std::cin %26gt;%26gt; string;
possibly cause it to segfault or corrupt other variables (I know it will at least hold the data)? I haven't had that happen before, but just to make sure...
C/C++ pointers as strings?
the above answer is excellent advice (or below, the guy who said use strings). Do NOT use arrays of chars. You risk buffer overflow and if that happens your program will be entirely compromised. A buffer overflow in your case would be when you had a char array of say 100, but the user entered a line of text with over 100 chars. After 100, you're program will be writing char values to memory that is outside of your array and could be other variables, return values, etc.
Reply:Hi,
If u r using char pointers then dont worry about the size of string, to be saved in that char pointer.
Poiter reuires 2 bytes only. Char pointer holds only the address of the string not the string itself.
so
char *p;
p="a";
or
p="abcdefghijklmnopqrstuvwxyz";
so either u save a character or a string of any length, doesnt matter.
Reply:These examples do not make sense to me. 'char' is a primitive type and not a class.
This is how I would declare string. It is a simple array of char big enough for what you will use it for.
char string[100];
std::cin %26gt;%26gt; string;
If you use the C++ string class then it will resize and do clever things automatically. Hurrah!
#include %26lt;string%26gt;
string mystring;
std::cin %26gt;%26gt; mystring;
std::cout %26lt;%26lt; mystring;
wildflower
char *string = new char;
std::cin %26gt;%26gt; string;
instead of:
char *string = new char[100];
std::cin %26gt;%26gt; string;
possibly cause it to segfault or corrupt other variables (I know it will at least hold the data)? I haven't had that happen before, but just to make sure...
C/C++ pointers as strings?
the above answer is excellent advice (or below, the guy who said use strings). Do NOT use arrays of chars. You risk buffer overflow and if that happens your program will be entirely compromised. A buffer overflow in your case would be when you had a char array of say 100, but the user entered a line of text with over 100 chars. After 100, you're program will be writing char values to memory that is outside of your array and could be other variables, return values, etc.
Reply:Hi,
If u r using char pointers then dont worry about the size of string, to be saved in that char pointer.
Poiter reuires 2 bytes only. Char pointer holds only the address of the string not the string itself.
so
char *p;
p="a";
or
p="abcdefghijklmnopqrstuvwxyz";
so either u save a character or a string of any length, doesnt matter.
Reply:These examples do not make sense to me. 'char' is a primitive type and not a class.
This is how I would declare string. It is a simple array of char big enough for what you will use it for.
char string[100];
std::cin %26gt;%26gt; string;
If you use the C++ string class then it will resize and do clever things automatically. Hurrah!
#include %26lt;string%26gt;
string mystring;
std::cin %26gt;%26gt; mystring;
std::cout %26lt;%26lt; mystring;
wildflower
Subscribe to:
Posts (Atom)