c++ c-string topper function sometimes outputs random numbers in the end - c++

I need to use a character array pointing to a function. In the function, I need it to make the input into all capital letters. I figured I'd use the toupper function, which worked great for certain input, but when I input certain words I end up getting weird symbols/numbers in the end of my output(see output). Any help?
Code
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
void allUppercase(char*);
const int SIZE = 50;
int main()
{
char words[SIZE];
cout << "Please enter your text : ";
cin.get(words, (SIZE-1));
cout << "The keyboard input was \"" << words << "\".\n";
cout << "\nThe uppercase output is \"";
allUppercase(words);
cout << "\".\n";
return 0;
}
// outputs the string entered in uppercase letters
// (Use a character array to store the string)
void allUppercase(char *ch)
{
char temp[SIZE];
for (int i=0; i < SIZE; i++)
{
if (ch[i] != '\0')
temp[i] = toupper(ch[i]);
else
break;
}
cout << temp;
}
Example Output

You forgot to null terminate temp.
void allUppercase(char *ch) {
char temp[SIZE];
for (int i=0; i < SIZE; i++) {
if (ch[i] != '\0') {
temp[i] = toupper(ch[i]);
} else {
temp[i] = '\0';
break;
}
}
cout << temp;
}

You just need to add a zero byte at the end of the result string.
For example,
void allUppercase( char const* s )
{
char temp[SIZE];
int i = 0;
while( temp[i] = toupper( s[i] ) ) { ++i; }
cout << temp;
}
Disclaimer: code not touched by compiler.
In other news, in order to work with national characters such as Norwegian æ, ø and å, with a single byte per character encoding that supports them, you need to
cast the argument to toupper to unsigned char, and
call setlocale( LC_ALL, "" ) at the start of main.
Without the cast toupper can receive a negative argument value other than EOF, because char is usually a signed type, and in that case the behavior is undefined (e.g. a crash).
Also, instead of raw arrays and pointers, just use std::string. You have already included the <string> header.

the best way is to write a while loop like this
while(ch[i]!='\0')
{
temp[i] = toupper(ch[i]);
cout<<temp[i]
i++;
}
and this program will be terminated when '\0' is found

Related

Change each word in a line by removing all subsequent occurrences of the first letter of that word from it

A sentence is given in which words are separated by spaces (one or more). Change each word in the line, removing from it all subsequent occurrences of the first letter of this word (do not change the number of spaces between words).
#include <iostream>
#include <cstring>
using namespace std;
void check(char *input){
char* Letter = strtok(input, " ");
while (Letter){
char _Letter = Letter[0];
for (int i = 1; i < strlen(Letter); i++)
if (Letter[ i ] == _Letter)
Letter[ i ] = ' ';
cout << Letter << " ";
Letter = strtok(NULL, " ");
}
cout << endl;
}
int main(){
setlocale(LC_ALL , "Ukrainian");
char *input = new char[100];
gets(input);
check(input);
delete[] input;
return 0;
}
My code removes the occurrence of the first letter from the word, but does not take into account the spaces between words and leaves one space.Using std :: string is not allowed, you must use a character array with functions from the library <string.h> Help fix the code, please
traverse character by character, printing the characters you want to keep.
This makes it much easier to maintain whitespace and you don't need to mess around with moving characters around.
Lightly tested sketch:
int main()
{
bool in_word = false;
char ignore = 0;
char line[100];
gets(line);
char* c = line;
while (*c)
{
if (!std::isspace(*c))
{
if (!in_word)
{
in_word = true;
ignore = *c;
std::cout << *c;
}
else if (*c != ignore)
{
std::cout << *c;
}
}
else
{
in_word = false;
std::cout << *c;
}
c++;
}
}

Whitespace at the every first character of the string while printing it

This program takes a line, separates each word and adds them into a vector. But while printing the strings from vector after the first word all the remaining words contain a whitespace character at the beginning. I want those whitespaces to be removed.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void addmember(string s) {
vector<string> vlist;
string buf;
for(int i = 0; i < s.length(); i++) {
if(s[i] == ' ') {
buf.push_back('\0');
vlist.push_back(buf);
buf.erase();
}
buf.push_back(s[i]);
}
for(auto it : vlist) {
cout << it << "\t" << it.size() << endl;
}
}
int main() {
string s = "A friend is someone who knows all about you";
addmember(s);
return 0;
}
Output:
A
friend
is
someone
who
knows
all
about
You push the character onto the buf string even if it is a whitespace.
Put buf.push_back(s[i]) in a else branch.
for (int i = 0; i < s.length(); i++) {
if (s[i] == ' ') {
vlist.push_back(buf);
buf.erase();
}
else {
buf.push_back(s[i]);
}
}
Edit: As you use strings in the vector as well as cout << and not char*/char[] one can assume you only want C++ style strings.
If so, you should also remove the insertion of \0 terminators at the end of each substring. C++ strings are not zero terminated.
You push_back() every s[i], no matter what it is. You want to do that only if s[i] is not a space:
for(int i=0; i<s.length(); i++){
if(s[i] == ' '){
vlist.push_back(buf);
buf.erase();
} else {
buf.push_back(s[i]);
}
}

How to Replace character in c++ without using library

I have prototype - int replace_char(string &, char);
I can't use library from string and ctype.h, I should write my own function.
So the task is to find in the text caharacter, which should I should replace with "*" .
example: In This is my text .
replace all t to * . Result will be - *his is my *ex*.
#include <iostream>
using namespace std;
int replace_char(string &, char);
int main ()
{
cout << ""Please insert text:"
cin >> str;
}
int replace_char(string str, char c1)
{
for (int i = 0 ; i < str.length(); i++)
{
if(str[i]==c1)
str[i]='*';
}
return str;
}
There were several errors in the code:
The function signature mismatches, the prototype is defined as std::string& but in the function definition, std::string only was used.
The program never converted the capital letter T or anything which is capital in order to convert them before comparing each letter with a single char.
The function is never used in the code.
cin >> str won't take longer texts followed by next whitespace character.
The function wants to return an integer, but actually returned type was a std::string, which is totally a misunderstanding.
The code redefined:
#include <iostream>
// taking a reference of std::string and a char
int replaceText(std::string&, char);
int main(void) {
std::string s;
int rep;
std::cout << "Enter a string: ";
std::getline(std::cin, s); // getline() to accept whitespaces
// since we're using a reference, the original variable is manipulated
int rep = replaceText(s, 't');
std::cout << "Output: " << s << std::endl;
std::cout << "Replaced number of chars: " << rep << std::endl;
return 0;
}
int replaceText(std::string& str, char c) {
size_t len = str.length();
static int count;
// changing each letter into lowercase without using any built-in functions
for (size_t i = 0; i < len; i++)
if (str[i] >= 'A' && str[i] <= 'Z')
str[i] = str[i] + 32;
// replacing the character, the main work
for (size_t i = 0; i < len; i++)
if (str[i] == c) {
str[i] = '*';
count++; // count a static variable
}
return count; // returning the overall counts
}
The program firstly takes an input from the user of type std::string and uses reference to the variable str. Now the program enters to the function code.
In the beginning, the function converts each letter to lowercase without using any library or a built-in function. Afterwards, it tries to compare each letter of the string carefully and as soon the given character matches a value containing in the string passed to the function, it replaces and counts a static variable which keeps the value save for the entire program life.
Thereafter, it simply displays the manipulated string.
It outputs something like:
Enter a string: This is a text
Output: *his is a *ex*
Replaced chars: 3
You seem to have a good start.
You need to declare str before reading input into it. Try string str;
Then you need to use your function in main. Either store its output into another string like string replaced = replace_char(str, 't');
Or put it into the output directly like cout << replace_char(str, 't') << endl;
Probably this is what you need
#include <iostream>
using namespace std;
int replace_char(string &, char);
int main ()
{
string str;
cout << "Please insert text:"
std::getline(cin, str);
int rlen = replace_text(str, 't')
cout << str << endl;
cout << "Number of replaced : " << rlen << endl;
return 0;
}
int replace_char(string str, char c1)
{
int rlen = 0;
for (int i = 0 ; i < str.length(); i++)
{
if(str[i]==c1) {
str[i]='*';
rlen++;
}
}
return rlen;
}
Given the prototype of the function, I'm guessing you need to return the number of chars replaced. This implementation should work:
#include <string>
#include <iostream>
using namespace std;
int replace_char(string &, char);
int main ()
{
cout << "Please insert text:";
string str;
getline(cin, str);
int nCharsReplaced = replace_char(str, 't');
}
int replace_char(string& str, char c1)
{
int count = 0;
for (int i = 0 ; i < str.length(); i++)
{
if(str[i]==c1)
{
str[i]='*';
count++;
}
}
return count;
}
Keep in mind there's no need to return the string, as you're passing it by reference, so the argument itself is modified.
Also, if you want the example you provided to work the replace_char functions cannot be case sensitive, since you replaced the capital 'T' with '*' too. In order to achieve that, you could implement a function that turns every char to lowercase (ideally, you would use tolower from ctype):
char to_lower_case(char c)
{
return c - ('Z' - 'z');
}
And replace the if condition with:
if (to_lower_case(str[i]) == c1)
If you don't understand how this work, take a look at how ASCII works.

print 2nd word in a string with its size in C++

I am trying to make a program in which a user enters a string and i will print out the second word in the string with its size.
The delimiter's are space( ), comma(,) and tab( ).
I have used a character array and fgets to read from user and a character pointer that points to the first element of the array.
source code:
#include"iostream"
#include<stdio.h>
#include<string>
using namespace std;
// extract the 2nd word from a string and print it with its size(the number of characters in 2nd word)
int main()
{
char arr[30], arr1[30];
char *str = &arr1[0];
cout<<"Enter a string: ";
fgets(str, 30, stdin);
int i = 0, j, count = 1, p = 0; // count is used to find the second word
// j points to the next index where the first delimiter is found.
// p is used to store the second word found in character array 'arr'
while(*(str+i) != '\n')
{
if(*(str+i) == ' ' || *(str+i) == ',' || *(str+i) == ' ')
{
count++;
if(count == 2)
{
// stroing 2nd word in arr character array
j = i+1;
while(*(str+j) != ' ' || *(str+j) != ',' || *(str+j) != ' ')
{
arr[p] = *(str+j);
cout<<arr[p];
p++;
i++;
j++;
}
break;
}
}
i++;
}
arr[p+1] = '\0'; // insert NULL at end
i = 0;
while(arr[i] != '\0')
{
cout<<arr[i];
i++;
}
cout<<"("<<i<<")"<<endl;
return 0;
}
Help me out with this.
To start, don't use std::cin for testing. Just set a value in your code for consistency and ease of development. Use this page for a reference.
#include <iostream>
#include <string>
int main() {
std::string str("this and_that are the tests");
auto start = str.find_first_of(" ,\n", 0);
auto end = str.find_first_of(" ,\n", start + 1);
std::cout << str.substr(start, end - start);
return 0;
}
And this is still somewhat of a hack, it just depends where you are going. For instance the Boost library is rich with extended string manipulation. If you are going to parse out more than just one word it can still be done with string manipulations, but ad-hoc parsers can get out of hand. There are other tools like Boost Spirit to keep code under control.
The delimiters used when extracting from a stream depends on the locale currently in effect. One (cumbersome) way to change the extraction behaviour is to create a new locale with a special facet in which you specify your own delimiters. In the below example the new locale is used to imbue a std::stringstream instead of std::cin directly. The facet creation part is mostly copy/paste from other answers here on SO, so you'll find plenty of other examples.
#include <iostream>
#include <locale> // std::locale, std::ctype<char>
// https://en.cppreference.com/w/cpp/locale/ctype_char
#include <sstream> // std::stringstream
#include <algorithm> // std::copy_n
#include <vector> // a container to store stuff in
// facet to create our own delimiters
class my_facet : public std::ctype<char> {
mask my_table[table_size];
public:
my_facet(size_t refs = 0)
: std::ctype<char>(&my_table[0], false, refs)
{
// copy the "C" locales table to my_table
std::copy_n(classic_table(), table_size, my_table);
// and create our delimiter specification
my_table[' '] = (mask)space;
my_table['\t'] = (mask)space;
my_table[','] = (mask)space;
}
};
int main() {
std::stringstream ss;
// create a locale with our special facet
std::locale loc(std::locale(), new my_facet);
// imbue the new locale on the stringstream
ss.imbue(loc);
while(true) {
std::string line;
std::cout << "Enter sentence: ";
if(std::getline(std::cin, line)) {
ss.clear(); // clear the string stream from prior errors etc.
ss.str(line); // assign the line to the string stream
std::vector<std::string> words; // std::string container to store all words in
std::string word; // for extracting one word
while(ss>>word) { // extract one word at a time using the special facet
std::cout << " \"" << word << "\" is " << word.size() << " chars\n";
// put the word in our container
words.emplace_back(std::move(word));
}
if(words.size()>=2) {
std::cout << "The second word, \"" << words[1] << "\", is " << words[1].size() << " chars\n";
} else {
std::cout << "did not get 2 words or more...\n";
}
} else break;
}
}
#include"iostream"
#include<stdio.h>
#include<string>
#include <ctype.h>
using namespace std;
int main()
{
char c;
string str;
char emp = ' ';
cout<<"Enter a string: ";
getline (cin,str);
int j = 0, count = 1, counter = 0;
for (int i = 0; i < str.length() && count != 2; i++)
{
cout<< str[i] <<endl;
if( isspace(str[i]) || str[i] == ',' || str[i] == '\t' )
{
count++;
if(count == 2)
{
j = i+1;
while(j < str.length())
{
if (isspace(str[j]) || str[j] == ',' || str[j] == '\t')
{
break;
}
cout<<str[j];
counter++;
j++;
}
cout<<endl;
}
}
}
cout<<"size of the word: "<<counter<<endl;
return 0;
}
This is a simple answer to what you want, hope to help you.
// Paul Adrian P. Delos Santos - BS Electronics Engineering
// Exercise on Strings
#include <iostream>
#include <sstream>
using namespace std;
int main(){
// Opening Message
cout << "This program will display the second word and its length.\n\n";
// Ask for a string to the user.
string input;
cout << "Now, please enter a phrase or sentence: ";
getline(cin, input);
// Count the number of words to be used in making a string array.
int count = 0;
int i;
for (i=0; input[i] != '\0'; i++){
if (input[i] == ' ')
count++;
}
int finalCount = count + 1;
// Store each word in a string array.
string arr[finalCount];
int j = 0;
stringstream ssin(input);
while (ssin.good() && j < finalCount){
ssin >> arr[j];
j++;
}
// Display the second word and its length.
string secondWord = arr[1];
cout << "\nResult: " << arr[1] << " (" << secondWord.size() << ")";
return 0;
}

How do you flush the contents of `std::cin` before an additional read from it?

I am trying to read a single character multiple times. The catch is that I need to prevent user errors. So for example:
char arr[10];
for(int i = 0; i < 10; i++)
{
cin.get(arr[i]);
}
Where the inputs should be something like a, b, c, d, .... But if someone were to enter ab for the first entry I want to capture the a and then ignore the b. I know about cin.ignore however I don't know how I would go about ignoring an arbitrary number of alphanumeric characters or symbols considering that I want to ignore a potentially unlimited number of characters and then stop ignoring and read again.
How can I either ignore an arbitrary number of characters and then stop ignoring or how can I actually flush the buffer for cin.
Most input is line feed so if you want to ignore all characters in the input stream until you hit a newline then you could use:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n')
Since we ignore up to the streamsize there should not be an extra content in the input buffer.
If you want user to hit enter after each symbol, then code could be as simple as this:
char arr[10];
for(int i = 0; i < 10; )
{
std::string line;
std::getline( std::cin, line );
// check that line is not empty
if( line.empty() ) {
std::cout << "missing input" << std::endl;
continue;
}
arr[i++] = line[0]; // get only first symbol and ignore the rest
}
if you have something else in mind, I am afraid that will not work with std::cin - you do not see any input until user presses enter. In that case you would have to use OS specific functions to get unbuffered input.
The following is the code that you want, if your inputing like this a 'enter' b 'enter' c 'enter' etc...
#include <iostream>
#include <string>
using namespace std;
int main() {
char arr[10];
string line;
for (int i = 0; i < 10; i++)
{
getline(cin, line);
arr[i] = line[0];
cout << endl << "Here is the Char: " << arr[i] << endl;
}
return 0;
}
BUT if you enter input like this in one line: a,b,c,d,e,f,g,h,i,j 'enter' then you want the following code:
#include <iostream>
#include <string>
using namespace std;
int main() {
char arr[10];
string line;
int i = 0;
size_t end;
getline(cin, line);
end = 0;
int counter = 0;
if (line != "") {
while (end != string::npos && counter < 10) {
if (counter == 0) {
arr[counter] = line[0];
}
else {
end = line.find(",", end + 1);
arr[counter] = line[end + 1];
}
counter++;
}
}
for (int i = 0; i < 10; i++) {
cout << endl << "Here is the Char: " << arr[i] << endl;
}
return 0;
}