How to terminate a std::string in C++? - c++

I'm trying to remove all punctuation characters from a std::string in C++. My current code:
string str_in;
string::size_type i, j;
cout << "please input string with punctuation character..." << endl;
cin >> str_in;
for (i = 0, j = 0; i != str_in.size(); ++i)
if (!ispunct(str_in[i]))
str_in[j++] = str_in[i];
str_in[j] = '\0';
cout << str_in << endl;
Is str_in[j] = '\0'; wrong?

If you want to truncate str_in to the first j characters, you can say str_in.resize(j).
If you want to use the standard library you could apply the erase-remove idiom like this:
#include <algorithm>
#include <iostream>
#include <string>
int main()
{
std::string str_in;
std::getline(std::cin, str_in);
// Here is where the magic happens...
str_in.erase(std::remove_if(str_in.begin(), str_in.end(), ::ispunct), str_in.end());
std::cout << str_in << '\n';
return 0;
}

the C++ string type is NOT implemented to be null terminated (although a c_str() call will give you a null terminated string.)
So yes, str_in[j] = '\0' is wrong for at least two reasons:
The str_in.length() will not reflect the size of the string you expect with the punctuation removed.
The null charatcter is an extra charter which will be sent to any output stream,such as cout << str_in;
Using the std::string class you should probably not oveeride the same buffer, but probably use a str_out buffer instead which will have the right length after you copy all the wanted (i.e. excluding the punctuation character), OR you should instead adjust the length of the str_in instead of adding the null.

I think str_in[j] = '\0' is wrong when the string has no any punctuation.

Instead of modifying the same string, create a new string (e.g. str_out) and append to that:
str_out += str_in[i];

Related

C-String implementation in C++ outputs incorrectly

I was recently working on a problem teaching new users of C++, which I myself am, how to use cstrings and the different implementations of them compared to the imported string object in C++. As I was working on the problem, I came across an error where, despite initializing the size of the cstring to an appropriate length for the operations that were being done, the cstring was being outputted strangely.
When I would go to print out the cstring using cout, it would print some of the cstring correctly, but oftentimes the first several characters were random characters that had nothing to do with the operations being done to the cstring. However, I found a way to definitively prevent those characters from being printed; however, I am curious as to why this works as well as what the issue is here.
I found that adding cout << ""; on its own line prior to printing the cstring resolved the issue of the random characters being outputted when printing the cstring. However, this seems like only a temporary fix and I am looking to find a more educated approach to solving this issue.
Below I have included the code that was causing the errors.
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
using namespace std;
int main() {
vector<string> words = {"Hello,", "and", "welcome", "to", "the", "world", "of", "C++!"};
// Calculate the total number of characters in the words vector
// (including an additional character for space)
int length = 0;
for(int i = 0; i < words.size(); i++) {
length += words.at(i).length() + 1;
}
cout << ""; // Removing this line of code will cause the output to do strange things
// Initialize the cstring to be of size length
char cstring[length];
// Build the cstring using cstring library functions
for(int i = 0; i < words.size(); i++) {
strcat(cstring, (words.at(i) + " ").c_str());
}
// Null-terminate the cstring
cstring[length-1] = '\0';
// Output the cstring
cout << cstring << " " << strlen(cstring) << endl;
return 0;
}
If the line of code containing cout << ""; is removed, the output looks something like this, with a random amount and random set of characters at the beginning of the output each time:
`k+��Hello, and welcome to the world o 39
However, by including the line, I am able to achieve the desired output:
Hello, and welcome to the world of C++! 39
For starters variable length arrays is not a standard C++ feature
// Initialize the cstring to be of size length
char cstring[length];
Secondly you defined an uninitialized array. So using strcat invokes undefined behavior
strcat(cstring, (words.at(i) + " ").c_str());
This statement
// Null-terminate the cstring
cstring[length-1] = '\0';
is redundant because the function strcat appends also the terminating zero provided that the character array you declared has a space to accommodate the zero character (and you forgot to reserve a space for the terminating zero in the array).
If the compiler supports variable length arrays then the program can look the following way
#include <iostream>
#include <string>
#include <vector>
#include <cstring>
int main()
{
std::vector<std::string> words =
{
"Hello,", "and", "welcome", "to", "the", "world", "of", "C++!"
};
// Calculate the total number of characters in the words vector
// (including an additional character for space)
size_t length = words.size();
for ( const auto &s : words ) length += s.length();
// Initialize the cstring to be of size length
char cstring[length + 1];
cstring[0] = '\0';
// Build the cstring using cstring library functions
for ( const auto &s : words )
{
std::strcat( cstring, ( s + ' ' ).c_str() );
}
// Output the cstring
std:: cout << cstring << ' ' << length << std::endl;
return 0;
}
The program output is
Hello, and welcome to the world of C++! 40

Capitalizing letters in C++

I have an assignment where the user enters a student name in the format ( last name, first name). Can you help me figure out how to capitalize the first letter for both the first name and the last name?
I was using this to turn the user input into an array, so I could have the first letter capitalized, but when I did this, I had trouble getting it to work outside of the for loop.
for (int x = 0; x < fName.length(); x++)
{
fName[x] = tolower(fName[x]);
}
fName[0] = toupper(fName[0]);
I used your code and just added some parsing around it. You really are very close.
I can't help myself. For user input, I always use getline() followed by a stringstream to parse the words from the line. I find it avoids a lot of edge cases that get me into quicksand.
When getline() gets an input, it returns true unless it has problems. If the user inputs Ctrl-d, it will return false. Ctrl-D is basically an EOF (end of file) code, which works well in this case (as long as you are not trying to input the Ctrl-d from inside your debugger. Mine does not like that.
Note that I am using std::string in place of an array. std::string can be treated like an array for subscripting, but it prints nicely and has other functions that make it better for processing character strings.
#include <iostream>
#include <string> // Allow you to use strings
#include <sstream>
int main(){
std::string input_line;
std::string fName;
std::string lName;
std::cout << "Please enter students as <lastname>, <firstname>\n"
"Press ctrl-D to exit\n";
while(std::getline(std::cin, input_line)){
std::istringstream ss(input_line);
ss >> lName;
// remove trailing comma. We could leave it in and all would work, but
// it just feels better to remove the comma and then add it back in
// on the output.
if(lName[lName.size() - 1] == ',')
lName = lName.substr(0, lName.size() - 1); // Substring without the comma
ss >> fName;
for (int x = 0; x < fName.length(); x++) // could start at x=1, but this works.
{
fName[x] = tolower(fName[x]); // make all chars lower case
}
fName[0] = toupper(fName[0]);
for (int x = 0; x < lName.length(); x++)
{
lName[x] = tolower(lName[x]);
}
lName[0] = toupper(lName[0]);
std::cout << "Student: " << lName << ", " << fName << std::endl;
}
}

Count letters in String without using strlen()

I created a program that the user enters a string. But i need to count how many letters are in the string. The problem is im not allowed to use the strlen()function. So i found some methods but they use pointers but im not allowed to use that yet as we havent learned it. Whats the best way to do this in a simple method? I also tried chars but i dont have luck with that either.
#include <iostream>
using namespace std;
string long;
string short;
int main()
{
cout << "Enter long string";
cin >> long;
cout << "Enter short string";
cin >> short;
return 0;
}
I need to get the length of long and short and print it.
I am trying to use it without doing strlen as asked in some previous questions.
Do you mean something like this?
//random string given
int length = 1;
while(some_string[length - 1] != '\0')
length++;
Or if you don't want to count the \0 character:
int length = 0;
while(some_string[length] != '\0')
length++;
You can count from the beginning and see until you reach the end character \0.
std::string foo = "hello";
int length = 0;
while (foo[++length] != '\0');
std::cout << length;
If you use standard cpp string object I think the best way to get length of your string is to use method:
long.size();
It gives you number of characters in string without end string character '\0'. To use it you must include string library :
#include <string>
If you decide to use char table you can try method from cin:
char long_[20];
cout << "Enter long string\n";
int l = cin.getline(long_,20).gcount();
cout << l << endl;
gcount()

How to remove first word from a string?

Let's say I have
string sentence{"Hello how are you."}
And I want string sentence to have "how are you" without the "Hello". How would I go about doing that.
I tried doing something like:
stringstream ss(sentence);
ss>> string junkWord;//to get rid of first word
But when I did:
cout<<sentence;//still prints out "Hello how are you"
It's pretty obvious that the stringstream doesn't change the actual string. I also tried using strtok but it doesn't work well with string.
Try the following
#include <iostream>
#include <string>
int main()
{
std::string sentence{"Hello how are you."};
std::string::size_type n = 0;
n = sentence.find_first_not_of( " \t", n );
n = sentence.find_first_of( " \t", n );
sentence.erase( 0, sentence.find_first_not_of( " \t", n ) );
std::cout << '\"' << sentence << "\"\n";
return 0;
}
The output is
"how are you."
str=str.substr(str.find_first_of(" \t")+1);
Tested:
string sentence="Hello how are you.";
cout<<"Before:"<<sentence<<endl;
sentence=sentence.substr(sentence.find_first_of(" \t")+1);
cout<<"After:"<<sentence<<endl;
Execution:
> ./a.out
Before:Hello how are you.
After:how are you.
Assumption is the line does not start with an empty space. In such a case this does not work.
find_first_of("<list of characters>").
the list of characters in our case is space and a tab. This will search for first occurance of any of the list of characters and return an iterator. After that adding +1 movers the position by one character.Then the position points to the second word of the line.
Substr(pos) will fetch the substring starting from position till the last character of the string.
You can for example take the remaining substring
string sentence{"Hello how are you."};
stringstream ss{sentence};
string junkWord;
ss >> junkWord;
cout<<sentence.substr(junkWord.length()+1); //string::substr
However, it also depends what you want to do further
There are countless ways to do this. I think I would go with this:
#include <iostream>
#include <string>
int main() {
std::string sentence{"Hello how are you."};
// First, find the index for the first space:
auto first_space = sentence.find(' ');
// The part of the string we want to keep
// starts at the index after the space:
auto second_word = first_space + 1;
// If you want to write it out directly, write the part of the string
// that starts at the second word and lasts until the end of the string:
std::cout.write(
sentence.data() + second_word, sentence.length() - second_word);
std::cout << std::endl;
// Or, if you want a string object, make a copy from the start of the
// second word. substr copies until the end of the string when you give
// it only one argument, like here:
std::string rest{sentence.substr(second_word)};
std::cout << rest << std::endl;
}
Of course, unless you have a really good reason not to, you should check that first_space != std::string::npos, which would mean the space was not found. The check is omitted in my sample code for clarity :)
You could use string::find() to locate the first space. Once you have its index, then get the sub string with string::substr() from the index after the index of the space up to the end of the string.
One liner:
std::string subStr = sentence.substr(sentence.find_first_not_of(" \t\r\n", sentence.find_first_of(" \t\r\n", sentence.find_first_not_of(" \t\r\n"))));
working example:
#include <iostream>
#include <string>
void main()
{
std::string sentence{ "Hello how are you." };
char whiteSpaces[] = " \t\r\n";
std::string subStr = sentence.substr(sentence.find_first_not_of(whiteSpaces, sentence.find_first_of(whiteSpaces, sentence.find_first_not_of(whiteSpaces))));
std::cout << subStr;
std::cin.ignore();
}
Here's how to use a stringstream to extract the junkword while ignoring any space before or after (using std::ws), then get the rest of the sentence, with robust error handling....
std::string sentence{"Hello how are you."};
std::stringstream ss{sentence};
std::string junkWord;
if (ss >> junkWord >> std::ws && std::getline(ss, sentence, '\0'))
std::cout << sentence << '\n';
else
std::cerr << "the sentence didn't contain ANY words at all\n";
See it running on ideone here....
#include <iostream> // cout
#include <string> // string
#include <sstream> // string stream
using namespace std;
int main()
{
string testString = "Hello how are you.";
istringstream iss(testString); // note istringstream NOT sstringstream
char c; // this will read the delima (space in this case)
string firstWord;
iss>>firstWord>>c; // read the first word and end after the first ' '
cout << "The first word in \"" << testString << "\" is \"" << firstWord << "\""<<endl;
cout << "The rest of the words is \"" <<testString.substr(firstWord.length()+1) << "\""<<endl;
return 0;
}
output
The first word in "Hello how are you." is "Hello"
The rest of the words is "how are you."
live testing at ideon

Taking string as input but dealing with characters individually in C++

I wanted to know if there is any efficient method of getting a string as input and then performing some operation on its characters individually?
Also, after performing operations (check that length of string may also increase or decrease), can we output the new string (string got after performing operations) instead of outputting the characters individually using a for loop?
Note that time is a crucial factor, please provide fastest methods.
Is there is any efficient method of getting a string as input and then performing some operation on its characters individually?
Yes, there is: read std::string as usual (say, with std::getline or the >> operator of an input stream), and then access the individual characters in a loop.
std::string str;
std::getline(std::cin, str);
for (int i = 0 ; i != str.size() ; i++) {
std::cout << "Code of character " << i << " is " << (int)str[i] << std::endl;
}
First demo on ideone.
Also, after performing operations, can we output the new string (string got after performing operations) instead of outputting the characters individually using a for loop?
Yes, you can: std::string is mutable, meaning that you can change it in place.
std::string str;
std::getline(std::cin, str);
for (int i = 0 ; i != str.size() ; i++) {
if (!std::isalpha(str[i])) {
str[i] = '#';
}
}
std::cout << str << std::endl;
Second demo on ideone.
You can do it like this:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string in;
cout << "Input please\n";
cin >> in;
if(in.size() >= 5)
in[5] = 'A';
cout << in << "\n";
return 0;
}
Or you can use std::getline(), instead of std::cin.
Output:
Input please
samaras
samarAs
However, are you sure this is the bottleneck of your program? You can check this with some profiling tools, like the one I use.
[EDIT]
Since OP is asking about efficiency, I did some testing. However, you can to take into account the time that user takes to type the input, but since I am the same person, we can assume this is constant.
So, I did modified a bit a code from another answer, like this:
std::string str;
cout << "Input please\n";
std::getline(std::cin, str);
if (str.size() >= 5) {
str[5] = '#';
}
std::cout << str << "\n";
Output:
Input please
Samaras
Samar#s
It took me 1.04237 seconds.
And with my code, I got
Input please
Samaras
SamarAs
It took me 0.911217 seconds.
Which actually show that they are pretty close and I would say the difference is due to my typing speed.
I did the timings with std::chrono, like the code I have in my pseudo-site.
Basic operation... Some search on the internet could have helped you but here you go...
std::string processStr(const std::string &str)
{
for (std::string::iterator it = str.begin(); it != str.end(); ++it)
// process your string (getting a char is done by dereferencing the iterator
// like this: *it
return (str);
}