Isupper / tolower doesn't work - c++

here's the problem, my program does not change uppercase letter to lowercase letter. I can not figure out why doesn't it.
#include <iostream>
#include <ctype.h>
using namespace std;
int main(){
string str="hEhEhehe";
for(int i=0;i<str.size();i++){
if(isupper(str.at(i)))
tolower(str.at(i));
}
cout << str;
return 0;
}

You need to assign the value back to the index of the string.
if(isupper(str.at(i)))
str[i] = tolower(str.at(i));

Related

How do I flip the first number in the string with the last number and then second and second last and so on?

Here's what I tried and it's not logical. First_num++ is just copying 2 numbers instead of the second number and so is the last_num++
How do I continue copying like that and cout them
I know I can just cout the thing in reverse because the output is the same.I can do that by reversing the count but the question wants me to exchange the characters one by one. Please help, thanks.
#include <iostream>
#include <cctype>
using namespace std;
void reverse_word(char character[]);
int main()
{
char characters[50];
reverse_word(characters);
return 0;
}
void reverse_word(char character[])
{
char temp1[2] = "\0";
char temp2[2] = "\0";
char first_num = 1;
char last_num = 1;
cout << "Enter a word to reverse first word last word second first second last and so on: ";
cin >> character;
for (int i=0;i<strlen(character)/2;i++)
{
strncpy(temp1, character, first_num);
first_num++;
strcpy(temp2 , &character[strlen(character)-last_num]);
last_num++;
}
for (int i = 0; i < strlen(character) / 2; i++)
{
cout << temp2;
cout << temp1;
}
}
You can use the build-in function of c++ reverse(begin, end) where the begin and end iterator are as parameter like below:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "String example";
reverse(str.begin(), str.end()); // outputs --> elpmaxe gnirtS
cout<<"\n"<<str;
return 0;
}
This is a short and fast solution , but if you want to get a more depth view you can apply some nice alogrithm .
You should cout the temp variables in the same for loop, not in a different one.

How to convert string of characters to their respective int values?

I'm trying to convert a string of characters into their ASCII int values. However I cannot get this to work for one even one character in the string. I would expect a result of 72 when entering 'H', but it returns a 0 (the same for every character I've tried).
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
string a;
cin >> a;
const char * b = a.c_str();
int c = atoi(b);
cout << int(c) << endl;
}
Thanks in advance.
atoi parses the C-string interpreting its content as an integral number, i.e.
int i = atoi("123"); // i = 123
You don't want this: you want to know the ASCII value of every single character of the input string. To figure this out, you can use this code snippet:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string a;
cin >> a;
for(int c: a)
cout << c << '\n';
}
I'm not quite used to the string library but simply type:
cout<<(int)a[pozition];
You can place that in a for like this.
for(int i=0;i<a.length();i++)
cout<<(int)a[i]<<endl;
You can just cast each character to an int
#include <iostream>
#include <string>
using namespace std;
int main()
{
string a;
cin >> a;
for (int i = 0; i < a.length(); i++) {
cout << (int) a[i] << endl;
}
}

How to assign char to string object in c++?

i am trying to convert the string into capital letter string by assigning single char's to string like this:-
#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
using namespace std;
int main()
{
string a;
getline(cin,a);
string b;
b.reserve(a.size()+1);
for(int i=(a.size()),i1=0;1;i1++)
{
if(b[i1]!='\0')
b[i1]=(char)toupper(a[i1]);
else
{
a[i1]='\0';
break;
}
}
cout << b <<endl;
}
every when run a.out by ./a.out ,Only endl gets prints
here is sample run:-
$ ./a.out
play clash royale
$
What is wrong in my program?? How can I assign single char to string??
There are some issues with your program. The main one is probably the diference between string reserve and string resize. What you want in your program is already had a string of a.size() length, so, use b.resize(a.size()).
A working version is bellow (there are better ways to write this, just being most consistent with OP proposal):
#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
using namespace std;
int main()
{
string a;
getline(cin,a);
string b;
b.resize(a.size());
for(int i1=0; i1 < a.size();i1++)
{
if(a[i1]!='\0')
b[i1]=(char)toupper(a[i1]);
else
{
b[i1]='\0';
break;
}
}
cout << b <<endl;
}

converting uppercase letter to lowercase and vice versa for a string

I am trying to convert a string characters from uppercase to lowercase. There is no compilation error but I am still getting same output as input:
#include <iostream>
#include<string.h>
#include<ctype.h>
using namespace std;
int main() {
char a[100];
cin>>a;
for(int i =0;a[i];i++){
if(islower(a[i])){
toupper(a[i]);
}
else if(isupper(a[i])){
tolower(a[i]);
}
}
cout<<a;
return 0;
}
std::toupper , std::tolower functions do not work in-place. They return the result, so you have to assign it to a[i] again:
char a[100];
std::cin>>a;
for(std::size_t i =0;a[i];i++){
if(std::islower(a[i])){
a[i]=std::toupper(a[i]);// Here!
}
else if(std::isupper(a[i])){
a[i]=std::tolower(a[i]);// Here!
}
}
std::cout<<a;
You could use the transform function from the Standard Library with a lambda function that returns the uppercase or lowercase character of a given character.
#include <algorithm>
#include <iostream>
using namespace std;
int main
{
string hello = "Hello World!";
transform(hello.begin(), hello.end(), hello.begin(), [](char c){
return toupper(c);})
cout << hello << endl;
}
This would output HELLO WORLD!. You can imagine doing the same thing for lowercase
Here's a solution I found by calling another char variable "charConvert" and setting it equal to the converted character.
#include <iostream>
#include<string.h>
#include<ctype.h>
using namespace std;
int main() {
char a[100];
cin >> a;
char charConvert;
for (int i = 0; a[i] ; i++) {
if (isupper(a[i])) {
charConvert = tolower(a[i]);
}
else if (islower(a[i])) {
charConvert = toupper(a[i]);
}
}
cout << charConvert << endl;
return 0;
}

Ask user to enter his/her name, and then reverse the given name and show it on screen. using character array

My code is this But i am getting garbage values. how do i make it general so that anyone can use it?
#include <iostream>
#include <string>
using namespace std;
void main()
{
char ch[31];
cin.get(ch,31);
for (int i = 30; i >= 0; i--)
{
cout << ch[i];
}
system("pause");
}
You are taking garbage char from you empty cells.
You must first know the number of char inserted by the user
use reverse function from algorithm header file and use string instead of char.
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
string s;
getline(cin,s);
reverse(s.begin(),s.end());
cout<<s<<endl;
return 0;
}