We have to define the size of the string like "char String[80]", or by taking the size of the string as user input, but what i want to do is , that user just give the string and the size changes as the sentence gets bigger .
Asked
Active
Viewed 59 times
-4
-
3Use `std::string` instead of a `char` array. Can't get Python-like friendliness if you're going to do the job the hard way. – user4581301 Oct 05 '18 at 17:45
-
1You're thinking in terms of C. In C++ on the other hand, we have [std::string](http://www.cplusplus.com/reference/string/string/), which you should really get to know. Also, use it with `std::cin` instead of `scanf`/`fgets`. – alter_igel Oct 05 '18 at 17:45
-
tell me any way. – WildFire Oct 05 '18 at 17:45
-
2This should be covered in in any good [introductory C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), which you should probably read to get a thorough grounding in C++. An answer here would be too short to do it justice. My guess is you're coming from a C background or a bad C++ teacher. – alter_igel Oct 05 '18 at 17:46
-
2Tell you what? Use `std::string` instead of a `char` array? Already did that. – user4581301 Oct 05 '18 at 17:46
-
how to use std::string show me an example. – WildFire Oct 05 '18 at 17:49
-
Okay Thank you everyone i can now understand how to do it but please tell me something more about std::string ....i am a high school student. – WildFire Oct 05 '18 at 17:52
-
2Speaking as somebody who taught themselves C++ by trial and error in high school, I **really** recommend getting a [beginner's book on C++](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) if you want to commit to learning C++. Otherwise, choose a simpler language that gets the job done. – alter_igel Oct 05 '18 at 17:58
-
There is no programming language before 11th grade in my school (say country). – WildFire Oct 05 '18 at 18:04
-
But what if i wanna make a function which can do it ? – WildFire Oct 05 '18 at 18:07
-
1I''m still not entirely certain what you are after here. Are you tasked with writing your own resizable string class or are you trying to find out what available in the C++ programming language? If you have to write your own, this is a common early programming assignment and is covered in detail all over the Internet (usually poorly). If you are looking for information, definitely get a book. C++ is a batshit crazy language. Since you appear to be coming in from Python, you won't need one of the truly basic beginner books. – user4581301 Oct 05 '18 at 18:26
1 Answers
3
Since you're in C++, you could use a std::string
object instead of a char[]
std::string will adapt its size dynamically, as you call its various mutator methods, like append()
or insert()
.
See: http://www.cplusplus.com/reference/string/string/
Otherwise, you might get more specific in what you mean by "the user gives the string". Types it on a command-line? The rest of the answers will likely depend on that. Some more info, please.
-
1This is a case where I'm actually all in on the cplusplus.com link. Even where it's not quite right, it's close enough for a new programmer and it's a much easier read than the [cppeference documentation on `std::string`](https://en.cppreference.com/w/cpp/string/basic_string) – user4581301 Oct 05 '18 at 18:20
-
-
I'm guessing everyone here, including myself, will recommend against that: The standard libraries have well-honed, well-tested implementations that you or me are unlikely to improve upon. Special cases probably exist where it's actually worth re-inventing the wheel like that, but we can't tell whether this is one, unless you give more information about your use case. You're not giving us enough to go on, by far. – Oct 06 '18 at 08:36
-
user4581301, I think the cppreference link looks only slightly more scary from that perspective than the cplusplus.com link (with template, traits, allocator in the first 7 lines of the page). The cplusplus.com goes around that by discussing the `string` typedef first. It a more practical approach. When you say cplusplus.com is _not quite right_, do you mean in general (as in you recommend not using it at all) or specifically about `std::string` and/or `std::basic_string`? Important to me as an old-hand-but-hopelessly-outdated-and-out-of-practice-trying-to-upgrade-self ;_) – Oct 06 '18 at 08:52