Why is this so

Pages: 12
Why String size is less than the array of characters, Though both have same pharase

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
using namespace 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
Do you still have string str = "Oh Captain, ...."; in your code still?
Nopes.. I'm trying with
cout << "Size of string in string data type: " << str.size() << endl;
Last edited on
Well you need the string to be declared to call the method of the string.

1
2
3
4
5
6
7
8
9
10
11
#include <string>
#include <iostream>

int main()
{
    std::string str = "Hello, World";

    std::cout << "String size: " << str.size() << std::endl;

    return( 0 );
}
String size: 12
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 :(
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace 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.
Last edited on
Well THanks for your assistance, this program run correctly, but in output it also show some odd characters, how can I remove them ?? :(
but in output it also show some odd characters, how can I remove them ??
the problem with giblits approach is that the 0 will not be copied. You need to add it after the loop like so:
1
2
3
4
5
6
int k=0;
		for (; str1[k]; k++)
	{
		str2[k]=str1[k];
	}
str2[k]=0;
Last edited on
@coder777, sorry but your code also not worked me :(
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace 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;

}
Last edited on
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 
@giblit Thanks, in line 4 I did't understand how this works str2[i];
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.


It is equal to

str2[i] != '\0'; or str2[i] != 0
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>
using namespace std;
int main ()
{
	char* str1;
	cout<<"Enter any string: ";
	cin>>*str1;
	cout<<"You Entered:\n"<<str1<<endl;
	return 0;
}
@ButchCavendish i don't think you can do that but maybe you could do this :


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
#include <cstring>
int main ()
{
	char *temp,*str1;
	temp=new char[100];
	cout<<"Enter any string: ";
	cin>>temp;
	str1=new char[strlen(temp)+1];
	strcpy(str1,temp);
	delete[] temp;
	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
Last edited on
You can use std::cin >> if it is one word but if you want a full sentence or something you will want to use std::getline( std::cin , some_string );
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 ?
Pages: 12