#include <iostream>
#include <string>
usingnamespace std;
int main ()
{
string str;
str="Oh Captain,\n""My Great Captain, Our Journey is Over\n";
cout<<str<<endl;
cout<<"__________________________________________\n";
cout<<"Size of string in string data type: "<<sizeof(str)<<endl;
char str1[]="Oh Captain,\n""My Great Captain, Our Journey is Over\n";
cout<<"Size of same pharase in Array of characters: "<<sizeof(str1)<<endl;
return 0;
}
.
That is because sizeof() measures the size of the object, NOT the size of the underlying array. If you want to find the size of the string, do this instead, to get the number of characters:
cout << "Size of string in string data type: " << str.size() << endl;
Also, try to avoid using sizeof anyway, due to the fact that the array may very easily degenerate into a pointer and you will always get a wrong answer.
This is not working with me,
I've also included <string> and <cstring>
error C2065: 'str' : undeclared identifier
error C2228: left of '.size' must have class/struct/union type
Yes this One is working fine, thanks alot
Now I've another question..
I'm trying to make a user defined function similar to strcpy();
For me, this code working fine If i use it without function, but when I use function It's showing me error.. help me out :(
#include <iostream>
usingnamespace std;
void stringcopy (char[],char[]);
int main ()
{
char str1[]="Cplusplus.com is indeed a great place for learning.I've learded alot from this site.";
char str2[sizeof(str1)];
stringcopy(str1,str2);
cout<<str2<<endl;
return 0;
}
void stringcopy (char str1, char str2)
{
for (int k=0; k<sizeof(str1); k++)
{
str2[k]=str1[k];
}
}
You have it prototyped correctly on line 3 but on line 13 you are defining the function wrong. They need to be arrays/pointers not single characters.
Also instead of calling sizeof you can simply put str1[k]:
for( int k = 0; str1[k]; ++k )
Basically it will loop while str1[k] is true. True is anything but 0. The ascii code for a null character is a zero. C-strings are null-terminated so that means it will stop when it hits the null character. Therefore looping 0->size-1.
[edit] This is probably for your homework but another thing to mention is I think the strcopy returns an int. -1 if it fails to copy and some other values for other conditions. strcpy returns a pointer to the destination
Return Value
destination is returned.
Also you probably want to make sure that the first string is not larger than the size of the second array or you will be assigning to out of bounds memory.
#include <iostream>
usingnamespace std;
void stringcopy (char[],char[]);
int main ()
{
char str1[]="Cplusplus.com is indeed a great place for learning.I've learded alot from this site.";
char str2[sizeof(str1)];
stringcopy(str1,str2);
cout<<str2<<endl;
return 0;
}
void stringcopy (char *str1, char *str2)
{
for (int k=0; k<sizeof(str1); k++)
{
str2[k]=str1[k];
}
str2[k]=0;
}
Coder is correct I forgot to tell you to add the null character to the end. The reason yours does not work butch is because k is undefined on line 19. You must move int k outside of the for loop scope and into the stringcopy scope. Like coder's example it is outside of the loop.
1 2 3 4 5 6
int i;
for( i = 0; str2[i]; ++i )
str2[i] = str1[i];
str2[i] = '\0'; //or 0 if you want
Basically it will loop while str1[k] is true. True is anything but 0. The ascii code for a null character is a zero. C-strings are null-terminated so that means it will stop when it hits the null character. Therefore looping 0->size-1.
My this program crashes on compile time, is there any specific reason,
Actually in this program i want the user to type any string, and let the compiler automatically decide size,
1 2 3 4 5 6 7 8 9 10
#include <iostream>
usingnamespace std;
int main ()
{
char* str1;
cout<<"Enter any string: ";
cin>>*str1;
cout<<"You Entered:\n"<<str1<<endl;
return 0;
}
Actually in this program i want the user to type any string, and let the compiler automatically decide size,
The compiler would need psychic powers in order to predict what the user will type at run time. Also the user input could vary each time the program is run, so clearly the size needs to be determined at runtime, not compile time. The std::string is ideal here as it does exactly what you need, while keeping the code very simple.
Thanks @Chervil, Now my question is, How can we take input in string (Not in array of characters, but in object of type "string"), I've tried with cin.get but it gives me error
Thanks @giblit, that worked like magic :). I've another question, I've printed random numbers using rand(); But my problem is that if a number is printed, it should not be printed again.. How can I do that ?