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;
}
Related
I am trying to replace phrases in a text, but I tried to run this c++ program, but this deletes all the string after the replaced one, so I can't continue replacing different texts using this function.
for example: input is hellookyou and the output is hellohello. the "you" part is missing. could you please explain why it is missing and what I should do. Thanks in advance
//edited this header part, and altered some of the code so that it is buildable
#include <iostream>
#include <conio.h>// for getch() function
std::string replaceOK(std::string a){
char ok[] = "ok";
while(a.find(ok, 0) < a.length()){
a.replace(a.find(ok, 0), a.length() - a.find(ok, 3),"hello");
}
return a;
}
int main(){
std::string a;
std::cin >> a;
a = replaceOK(a);
std::cout << a << std::endl;
return 0;
}
Fixed code.
This won't work if it has the same phrase that I want to change in the resulting changed phrase. It will loop on endlessly.
#include <iostream>
#include <conio.h>// for getch() function
std::string replaceOK(std::string a){
char ok[] = "ok";
while(a.find(ok, 0) < a.length()){
std::cout << "1"; // for visualization of the loop.
a.replace(a.find(ok, 0), a.length() - a.find(ok, 3),"ok1");
// changed "hello" to "ok1"
}
return a;
}
int main(){
std::string a;
std::cin >> a;
a = replaceOK(a);
std::cout << a << std::endl;
return 0;
}
The problem lies in the way you are using the a.replace() function.
The replace() function works in the way
a.replace(0,3,"red") This would change 3 characters from the 0th index and replace them with the string "red".
So in you case you are going to the index where you encounter the first occurence of "ok" string using a.find("ok",0), then by using a.length()-a.find("ok",3), you are getting 5 as the value and replacing them with "hello".
So you are basically doing a.replace(5,5,"hello"). Because of this the "okyou" part gets replaced by "hello" string.
The code is here:
#include <iostream>
#include <conio.h>// for getch() function
using namespace std;
string replaceOK(string a){
string ok = "ok";
int n=a.find("ok",0);
int p=1;//for counting the number of occurence of "ok"
while(n < a.length()){
if(p==1)
a.replace(n,ok.size(),"hello"); //replace 1st occurence of ok to hello
if(p==3)
a.replace(n,ok.size(),"ok1"); //replace 3rd occurence of ok to ok1
if(n+2>=a.length())
break;
n=a.find("ok",n+2);
p+=1;
}
return a;
}
int main(){
string a;
cin >> a;
a = replaceOK(a);
cout << a << std::endl;
return 0;
}
hi guys so my question is how to convert a char array to a string. here is my code:
#include<iostream>
using namespace std;
int main()
{
while (true) {
char lol[128];
cout << "you say >> ";
cin.getline(lol,256);
cout << lol << endl;;
}
return 0;
}
so I want to convert lol to a string variable like "stringedChar" (if thats even english lol)
so I can do stuff like:
string badwords[2] = {"frick","stupid"};
for (int counter = 0; counter < 2;counter++) {
if(strigedChar == badwords[counter]) {
bool isKicked = true;
cout << "Inappropriate message!\n";
}
}
Sorry im just a c++ begginer lol
Do something like this :
as char lol[128];
into string like: std::string str(lol);
Line : cin.getline(lol,256); <--> should be changed to cin.getline(lol,128)
Just invoke std::getline() on a std::string object instead of messing about with a char array, and use std::set<std::string> for badwords as testing set membership is trivial:
#include <iostream>
#include <set>
#include <string>
static std::set<std::string> badwords{
"frick",
"stupid"
};
int main() {
std::string line;
while (std::getline(std::cin, line)) {
if (badwords.count(line) != 0) {
std::cout << "Inappropriate message!\n";
}
}
return 0;
}
Note that this tests whether the entire line is equal to any element of the set, not that the line contains any element of the set, but your code appears to be attempting to do the former anyway.
First off, you have a mistake in your code. You are allocating an array of 128 chars, but you are telling cin.getline() that you allocated 256 chars. So you have a buffer overflow waiting to happen.
That said, std::string has constructors that accept char[] data as input, eg:
#include <iostream>
using namespace std;
int main()
{
while (true) {
char lol[128];
cout << "you say >> ";
cin.getline(lol, 128);
string s(lol, cin.gcount());
cout << s << endl;;
}
return 0;
}
However, you really should use std::getline() instead, which populates a std::string instead of a char[]:
#include <iostream>
#include <string>
using namespace std;
int main()
{
while (true) {
string lol;
cout << "you say >> ";
getline(cin, lol);
cout << lol << endl;;
}
return 0;
}
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;
}
}
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));
Basically, I am trying to print a string by eliminating all digits entered in a string s. But string c does not print.Also c.empty() gives a true value. Why does this happen and how to solve it ?
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
string s;
string c;
getline(cin,s);
int l=0;
for(decltype(s.size()) i=0;i<=s.size();i++)
{
if(!isdigit(s[i]))
{
c[l]=s[i];
l+=1;
}
}
cout<<c<<endl; //no visible output
cout<<c.empty(); //this prints 1
return 0;
}
Other commentators have explained what's gone wrong, but you also have a simpler way of removing digits in C++!
#include <iostream>
#include <algorithm>
int main()
{
std::string s = "abc 123 abc 123 abc 123";
std::cout << "Original: " << s << std::endl;
s.erase(std::remove_if(s.begin(), s.end(),
[](char ch) { return std::isdigit(ch); }),
s.end());
std::cout << "Without Digits: " << s << '\n';
}
string c; //An empty string
...
if(!isdigit(s[i]))
{
c[l]=s[i];
You have not allocated space in c to put characters.You could use push_back instead to push elements into c.
c.push_back(s[i]);
Or you could call resize() to have space allocated for c.
c.resize(s.length());
...
c[l]=s[i];
l+=1;
At the end of the loop, don't forget to put a \0, if you run your loop till s.size()-1.
c[l]='\0';
use operator+= to append the char to the string, like
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
string s;
string c;
getline(cin,s);
int l=0;
for(decltype(s.size()) i=0;i<=s.size();i++)
{
if(!isdigit(s[i]))
{
//Use this
c+=s[i];
}
}
cout<<c<<endl; //no visible output
cout<<c.empty(); //this prints 1
return 0;
}