Capitalizing first letter in each word [duplicate] - c++

This question already has answers here:
toupper returns integer rather than char
(4 answers)
Closed 7 years ago.
Output is same as the input where am i making the mistake?
Pls check the test version, it prints the ASCII code of 'A' but not A why is it so?
The 1st if condition in the loop is to make sure that the string starts with a valid character only and not space.
void caps(char* p)
{
char* begin=NULL;
char* temp=p;
while(*temp)
{
if((begin == NULL) && (*temp!= ' '))
begin=temp;
if(begin && ((*(temp+1) == ' ' ) || (*(temp+1)=='\0')))
{
toupper(*temp);
begin=NULL;
}
temp++;
}
cout<<p;
}
int main()
{
char str[]={"i like programming"};
cout<< str <<endl;
caps(str);
return 0;
}
Test
If I use printf("%c",toupper(a)) it prints 'A' correctly.
#include <iostream>
#include <ctype.h>
using namespace std;
int main()
{
char a= 'a';
cout<<toupper(a); //prints ASCII code of A(65) but why not 'A' ?
return 0;
}

Well, in the first code cout << str << endl; str is a pointer(it's type is char*). To output the first character of str, use cout << str[0]; or cout << *str;. And toupper() returns int, not char, so you need to cast the result:
cout << (char)toupper(a);

Related

C++ "toupper" not converting character to uppercase

I'm trying to convert every letter in a string to uppercase. I'm looping through each character and using toupper on it. However, when I print the new string out, it's not working. Sorry if this is a newbie question. Any help would be greatly appreciated :)
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string str1, str2;
cin >> str1 >> str2;
int len = str1.size();
for (int i = 0; i < len; i++) {
toupper(str1[i]);
toupper(str2[i]);
cout << str1[i] << " " << str2[i] << endl;
}
}
std::toupper returns a value rather than modifying its argument. So you need to do:
str1[i] = std::toupper(str1[i]);
str2[i] = std::toupper(str2[i]);
in order to actually modify the strings.
If you turn on warnings, e.g with -Wall the compiler will tell you that your version of the code has no effect.
This may be better, depending on your coding standards:
std::transform(str1.begin(), str1.end(), str1.begin(), std::toupper);
std::transform(str2.begin(), str2.end(), str2.begin(), std::toupper);
The above uses the STL function transform to convert the string to all uppercase.
You need to save the modified strings back into the str arrays. Something like this:
str[i] = toupper(str[i]);
In your loop, you don't change the elements of the strings because toupper() returns a new character, it doesn't change the passed character. You need to make your elements be the same as the returned characters, as follows:
for (int i = 0; i < len; i++) {
str1[i] = toupper(str1[i]);
str2[i] = toupper(str2[i]);
cout << str1[i] << " " << str2[i] << endl;
}
For starters this loop
for (int i = 0; i < len; i++) {
toupper(str1[i]);
toupper(str2[i]);
cout << str1[i] << " " << str2[i] << endl;
}
can invoke undefined behavior because the strings str1 and str2 in general can have different lengths.
These calls
toupper(str1[i]);
toupper(str2[i]);
has no effect because they change neither str1[i] no str2[i].
Also you need to convert the argument of a call of toupper to the type unsigned char.
You could separately output each string the following way
for ( unsigned char c : str1 )
{
std::cout << ::toupper( c );
}
std::cout << ' ';
for ( unsigned char c : str2 )
{
std::cout << ::toupper( c );
}
std::cout << '\n';

The code below outputs 1 and 0 how ever i do not know why [duplicate]

This question already has answers here:
How do I properly compare strings in C?
(10 answers)
Closed 2 years ago.
I understand that the code below is outputing the result of a boolean question but i do not understand why it outputs 1 and 0 instead of 1 and 1.
#include <iostream>
using namespace std;
int main()
{
string d = "abc", e = "abc";
char a[] = "abc", b[] = "abc";
cout << (d == e);
cout << (a == b);
return 0;
}
//outputs 10
i also tried outputing the value stored in variables a and b and got the same value for both
#include <iostream>
using namespace std;
int main()
{
string d = "abc", e = "abc";
char a[] = "abc", b[] = "abc";
cout << (d == e);
cout << (a == b);
cout << "\n" << a << "\n";
cout << b;
return 0;
}
// outputs 10
//abc
//abc
In C, std::string has a special operator== that actually compares the strings. Contrary to what you might read, std::strings are not equivalent to char arrays. Char arrays are essentially treated as pointers, so == compares the memory addresses, which are different, so the program will output 0.
For comparing 2 arrays of char you should use strcmp, the == operators compares the pointer addresses instead of the value itself. You can also iterate the arrays and compare element by element.

Trying to check entered text versus my own custom answers

here is what I attempted to throw together, unfortunately it's not doing what I want it to. What I want it to be doing is checking the entered text vs a few words that I consider correct. So, for example, if I want the only correct answers to be "thanks" or "please", how would I make the program check if the word the user entered is either "thanks" or "please"?
I have a feeling I can't just write B == 'funs etc.
help me out please:
#include <iostream>
using namespace std;
int main ()
{
string B;
for (;;)
{cout << "enter text here" << '\n' ;
cin >> B ;
if (B == 'fUNS'|| B == 'funs' || B == 'funzies')
{
cout << "correct!!!!!!" << endl;
break;
}
else
{
cout << "sorry, please try again" << endl;
continue;
}
}
return 0;
}
Unlike some languages using ' or " to enclose a sequence of characters produces very different results.
A single quote defines a single character literal e.g:
char a = 'A';
You can use multiple characters to define the value of an integer (although this is non-standard):
int a = 'ABCD';
A double quote defines a string literal which is a sequence of characters in an array:
const char str[5] = "ABCD";
Note the literal has a hidden null character at the end which is why it has 5 elements rather than 4. String literals are comparable and assignable with std::string:
std::string test( "ABCD" );
std::cout << test == "ABCD";
test = "EFGH";
std::cout << test == "ABCD";
I have a feeling I can't just write B == 'funs etc.
Yes, you can, since B is a std::string, which has an operator== defined. You just need to use " (which is used to define string literals) instead of ' (which is used to define character literals), eg:
if (B == "fUNS" || B == "funs" || B == "funzies")

How can I compare C-style strings? [duplicate]

This question already has answers here:
Compare equality of char[] in C
(5 answers)
Closed 6 years ago.
The for loop reverses original string 's' and stores it in 'temp.'Temp is printed correctly. After which, s and temp are compared, but the result always shows NO. :(
#include<cstring>
#include <iostream>
using namespace std;
int main()
{
char s[100], temp[100];
cin >> s;
cout << strlen(s);
for(int i=0;i<strlen(s);i++)
{
temp[i]=s[strlen(s)-i];
}
cout << "temp is" << temp;
if(temp==s)
{
cout << "YES";
}
else
{
cout << "NO";
}
return 0;
}
You should use strcmp instead of == because the latter is merely a pointer comparison. Also, '\0' has to be used to end your string. And your current code would actually create an empty string because your string at position strlen(s) contains '\0'.
Then again, you should not work with char arrays in C++ on your own, rather use std::string as already pointed out in comments.

How character convert to integer number? [duplicate]

This question already has answers here:
Convert char to int in C and C++
(14 answers)
Closed 9 years ago.
My code is this:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
int a;
cout << "Enter a number" << endl;
cin >> a;
string a_str;
ostringstream temp;
temp << a;
a_str = temp.str();
for(int i = 0; i < a_str.length(); i++) {
char c = a_str[i]; //How this character convert to integer number
}
system("pause");
return EXIT_SUCCESS;
}
How char c convert to int ?
I need this for
I need this because I need to get the highest digit
If you want to get char '8' to int 8 for example, this would be enough for ascii
int i = a_str[0] - '0';
const int i = atoi(&c);
should work too
char is already an integral type. I think you meant the following
char c = a_str[i] - '0';
atoi(c) for integers
atof(c) for floats
do the trick
You can also write it yourself:
This works for normal keys (not arrows keys or...)
#include <iostream>
using namespace std;
int main () {
char c;
cin >> c;
cout << (int)c << endl;
}
if you just have digits, you can do this:
int num = c - '0';