-
String Length function
Hi everyone, I have been learning about strings and successfully wrote my first string program which counts the characters in a given sentence. The problem that I ran into was that I wanted the program to count a string of characters that was entered by the user (no matter what it was)
e.g.
My name is Glenn
output: 16 (including spaces)
This is the program I was able to successfully run
Code:
#include<iostream.h>
#include<cstring>
int main()
{
char *string_a= "This is a test";
cout<<"The length of \""<<string_a<<"\"is "<<strlen (string_a)<<endl;
return 0;
}
This is the program that I am trying to write so that it returns the number of characters input by the user
Code:
#include<iostream.h>
#include<cstring>
int main()
{
char *string;
cout<<"What is your string";
cin>>string;
cout<<" The length of "<<string<<" is " << strlen (string)<<endl;
return 0;
}
I thought that the program was written correctly because it returns no (compiling) error but when it compiles Visual C returns an error and closes my program out.
:(
Can someone please poing me in the right direction....Should I start over??
Thank you very much
-
char *string;
cout<<"What is your string";
cin>>string;
You need to allocate space for your pointer
Easy suggestion is to start with
char string[100];
-
If you're including the string library, why not just use string variables instead of char arrays?
-
#include<cstring> - For c style string manipulation like strlen, strcat, strcmp - the new version of <string.h>.
#include<string.h> - Deprecated version of <cstring>.
#include<string> - For the standard string class (std::string).
Even though you aren't actually including the string library, I would definitely agree with joshdick to use the standard string class. Also, why do you use the old <iostream.h> but the new <cstring>? Just stick to the new headers (e.g. <iostream>).
And finally, if for some reason you do need to use c-style strings, I would add to Salem's advice by protecting the input so that it won't overflow using setw.
[Edit: See later post for solution that better solves the OP's problem (this one ignores the inclusion of space characters in the string).]
So here is my preferred solution:
Code:
#include<iostream>
#include<string>
int main()
{
std::string string_a;
std::cout<<"What is your string: ";
std::cin>>string_a;
std::cout<<" The length of "<<string_a<<" is " << string_a.length()<<std::endl;
return 0;
}
And here is a solution with c style strings:
Code:
#include<iostream>
#include<cstring>
#include<iomanip>
int main()
{
char string_a[100];
std::cout<<"What is your string: ";
std::cin>>std::setw(100)>>string_a;
std::cout<<" The length of "<<string_a<<" is " << strlen (string_a)<<std::endl;
return 0;
}
-
Thanks for clearing that up for me, jlou.
-
Sorry, I didn't notice that you really want the input with the spaces. Here are the two pieces of code above corrected to accept the entire line.
Preferred using standard string:
Code:
#include<iostream>
#include<string>
int main()
{
std::string string_a;
std::cout<<"What is your string: ";
std::getline(std::cin, string_a);
std::cout<<" The length of \""<<string_a<<"\" is " << string_a.length()<<std::endl;
return 0;
}
And using c-style strings:
Code:
#include<iostream>
#include<cstring>
int main()
{
char string_a[100];
std::cout<<"What is your string: ";
std::cin.getline(string_a, 100);
std::cout<<" The length of \""<<string_a<<"\" is " << strlen (string_a)<<std::endl;
return 0;
}
-
Thanks
Hi everyone, I just wanted to say thanks for the help. I was able to clean up my code and run it successfully.
Thanks
:)