converting to upper and lower case in c++ - c++

I wrote this code in c++
#include <iostream>
using namespace std;
char convert1 (char x[])
{
for ( int i =0; i<5;i++)
{
if ( i/2 ==0 )
x[i] =tolower(x[i]);
else
x[i] = toupper(x[i]);
return x[i];
}
}
int main()
{
char z[] = "REDCA";
cout<<convert1(z);
return 0;
}
it will work but it always print only the small case of the first letter R. but I want to print the complete word like this
rEdCa
so how can i modify it please

You want if(i%2 == 0) rather than if(i/2 == 0) (probably a typo).
Also, you are only returning x[i], which is a single char. The better idea would be using std::string instead of char *, but you can make this work with char * as well. (I won't provide full code, as we have established in your previous question, this is your assignment and you should do it, not us.)

The return value of convert1 is char. So, cout << convert1(x) sees a char, and thus you just get the first r.
You probably want something like this:
template<size_t S>
char const* convert1(char (&x)[S])
{
for(int i = 0; i < S; ++i)
{
if(i%2 ==0) {
x[i] = tolower(x[i]);
} else {
x[i] = toupper(x[i]);
}
}
return x;
}
Want better, use a std::string.

Related

Transform uppercase letters to lowercase and vice-versa using single parameter function (C++)

I have the trans function which uses a single parameter, has to be void, and returns through c the opposite case of a letter from a word input in main.
Example:
input: dOgdoG
output: DoGDOg
The function does change the case, but i cant figure out a way to build the new word / replace the old one because i keep getting compiling errors regarding "const char" or "invalid conversions".
The following program gives error "invalid conversion from char to const char*
I only changed the type of the function for example purposes.
#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;
char trans(char c)
{
if(c >= 'a' && c <= 'z')
return c-32;
else
if(c >= 'A' && c <= 'Z')
return c+32;
}
int main()
{
char s[101], s2[101] = "";
cin >> s;
int i;
for(i=0; i<strlen(s); i++)
{
strncat(s2, trans(s[i]), 1);
}
cout<<s2;
return 0;
}
EDIT:
I changed from the char function to a void function and removed the body of the for.
void trans(char c)
{
if(c >= 'a' && c <= 'z')
c-=32;
else
if(c >= 'A' && c <= 'Z')
c+=32;
}
int main()
{
char s[101], s2[101] = "";
cin >> s;
int i;
for(i=0; i<strlen(s); i++)
{
/// i dont know what to put here
}
cout<<s2;
return 0;
}
Don't reinvent the wheel. The standard library has functions to identify uppercase and lowercase letter, and to change case. Use them.
char trans(char ch) {
unsigned char uch = ch; // unfortunately, character classification function require unsigned char
if (std::isupper(uch))
return std::tolower(uch);
else
return std::toupper(uch);
}
You might be inclined to change that else branch to else if (std::islower(uch) return std::toupper(uch); else return uch;, but that's not necessary; std::toupper only changes lowercase letters to uppercase, so it won't affect characters that aren't lowercase.
Then, when you call it, just copy the result:
int i = 0;
for ( ; i < strlen(s); ++i)
s2[i] = tran(s[i]);
s2[i] = '\0';
EDIT:
Since there seems to be a requirement to do things the hard way, let's change trans to match:
void trans(char& ch) {
unsigned char uch = ch; // unfortunately, character classification function require unsigned char
if (std::isupper(uch))
ch = std::tolower(uch);
else
ch = std::toupper(uch);
}
And now, you can just apply it in place:
for (int i = 0; i < strlen(s); ++i)
trans(s[i]);
I called this "the hard way" because with the original version of trans you can use it directly to modify the original string:
for (int i = 0; i < strlen(s); ++i)
s[i] = trans(s[i]);
and you can use it to copy the string:
for (int i = 0; i < strlen(s); ++i)
s2[i] = trans(s[i]);
// don't forget the terminating nul
With pass by reference, you can only modify in place; copying requires an additional step:
strcpy(s2, s1);
for (int i = 0; i < strlen(s); ++i)
trans(s2[i]);
strncat takes 2 strings and a number as parameters; your second argument is a char, not a string.
You can use std::transform with string utility functions like std::isupper, std::toupper and similarly for lowercase. Since, the question is tagged c++ std::string is preferred over const char*1 for strings.
#include <string>
#include <cctype>
#include <iostream>
#include <algorithm>
char trans(unsigned char c){
if (std::isupper(c))
return std::tolower(c);
else if (std::islower(c))
return std::toupper(c);
else
return c;
}
int main(){
std::string s = "dOgdoG12";
std::string out;
out.resize(s.length());
std::transform(s.begin(), s.end(), out.begin(), trans);
std::cout << out; // DoGDOg12
}
Demo
1. SO post on char* vs std::string

Count the vowels of every word

I have to count the vowels of evey word in a given text. My attempt :
#include <iostream>
#include <string.h>
using namespace std;
char s[255], *p, x[50][30];
int c;
int main()
{
cin.get(s, 255);
cin.get();
p = strtok(s, "?.,;");
int n = 0;
while (p)
{
n++;
strcpy(x[n], p);
p = strtok(NULL, "?.,;");
}
for (int i = 1; i <= n; i++)
{
c = 0;
for (int j = 0; j < strlen(x[i]); j++)
if (strchr("aeiouAEIOU", x[i][j]))
c++;
cout << c << " ";
}
return 0;
}
PS: I know that my code is a mix between C and C++, but this is what I am taught in school.
Case closed in the comments.
However, for the fun, I propose you another variant that avoids to use the terrible strtok(), doesn't require a risky strcpy(), and processes each input character only one.
As you are bound to your teacher's mixed style and apparently are not supposed to use c++ strings yet, I also respected this constraint:
const char separators[]=" \t?.,;:"; // I could put them in the code directly
const char vowels[]="aeiouyAEIOUY"; // but it's for easy maintenance
int vowel_count=0, word_count=0;
bool new_word=true;
char *p=s;
cout << "Vowels in each word: ";
do {
if (*p=='\0' || strchr(separators,*p)) {
if (!new_word) { // here, we've reached the end of a word
word_count++;
cout << vowel_count << " ";
vowel_count = 0;
new_word=true;
} // else it's still a new word since consecutive separators
}
else { // here we are processing real chars of a word
new_word=false; // we have at least on char in our word
if (strchr(vowels, *p))
vowel_count++;
}
} while (*p++); // It's a do-while so not to repeat the printing at exit of loop
cout << endl<<"Words: "<<word_count<<endl;
Demo
This is my solution:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char s[255];
int n,i,counter=0;
cin.get(s,255);
for(i=0; i<=strlen(s)-1; i++)
if(s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u') counter++;
cout<<counter;
return 0;
}
If you have a vowel( a, e, i, o or u) you are adding up to the counter.
You can also use strchr but this is a more simple, understandable method.

How to display duplicate characters in a string in C++?

I am working on some code for a class that requires me to output duplicates in a string. This string can have any ascii character but the output needs to show only the repeated character and the total number of times it repeats.
Here are some sample inputs and outputs
mom, m:2
taco, No duplicates
good job, o:3
tacocat, t:2 c:2 a:2
My code works for all but the last test case, the t:2 and a:2 appears twice, Now I have come to the conclusion that I need to store duplicated characters somewhere and run a check on that list to see if that duplicate has already been printed so I tried using a vector.
My method is to push the character into the vector as the duplicates are printed and if a character is already in the vector then it is skipped in the printing. But I have not been able to find a way to this. I tried to use the find() from #include<algorithm> but got a syntax error that I am unable to fix. Is there a function that I can apply for this? Or am I going about this in a bad way?
I found the implementation of find() here & I looked here but they don't match and it breaks my code completely when I try to apply it.
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
vector <char> alreadyprintedcharacters;
void findrepeats(string const&);
int main()
{
string input;
cout << "Enter the input : ";
getline(cin, input);
findrepeats(input);
return 0;
}
void findrepeats(string const &in)
{
int trackerOfDuplicates = 0;
int asciiArray[256];
char ch;
int charconv;
for (int i = 0; i < 256; i++) // creates my refference array for the comparison and sets all the values equal to zero
asciiArray[i] = 0;
for (unsigned int i = 0; i < in.length(); i++)
{
ch = in[i];
charconv = static_cast<int>(ch);
if (asciiArray[charconv] == 0)
{
asciiArray[charconv] = 1;
}
else if (asciiArray[charconv] > 0)
{
asciiArray[charconv] = asciiArray[charconv]++;
}
}
bool trip = false;
for (unsigned int i = 0; i < in.length(); i++)
{
char static alreadyprinted;
char ch = in[i];
if ((asciiArray[ch] > 1) && (ch != alreadyprinted) && (find(alreadyprintedcharacters.begin(), alreadyprintedcharacters.end(), ch)!= alreadyprintedcharacters.end()))// change reflected HERE
{
cout << in[i] << " : " << asciiArray[ch] << endl;//???? maybe a nested loop
trip = true;
alreadyprinted = ch;
alreadyprintedcharacters.push_back(alreadyprinted);
}
}
if (trip == false)
cout << "No repeated characters were found.\n";
}
Your code works fine for me (gives the correct output for tacocat) if you fix the error related to std::find:
std::find doesn't return a bool, it returns an iterator (in your case, a std::vector<char>::iterator). If you want to check if std::find found something, you should compare it to alreadyprintedcharacters.end(), because that's what std::find returns if it didn't find something.
You can create an integer array of 256 and initialize it to 0 at first. Then loop over characters in the string and increment each index that corresponds to that letter. In the end, you can print out letters that have values greater than 1. Just change your findrepeats function to the following:
void findrepeats(string const &in)
{
int asciiArray[256];
char ch;
int charconv;
bool foundAny = false;
for (int i = 0; i < 256; i++) asciiArray[i] = 0;
for (unsigned int i = 0; i < in.length(); i++)
{
ch = in[i];
charconv = static_cast<int>(ch);
asciiArray[charconv]++;
}
for (unsigned int i = 0; i < 256; i++)
{
char static alreadyprinted;
if (asciiArray[i] > 1)
{
foundAny = true;
cout << static_cast<char>(i) << " : " << asciiArray[i] << endl;
}
}
if (!foundAny)
cout << "No repeated characters were found.\n";
}
You have to make following changes in your code
change the loop body where you are updating the reference array for the comparison and sets all the values like this:
//your code
else if (asciiArray[charconv] > 0)
{
asciiArray[charconv] = asciiArray[charconv]++;
}
in the above code the value of asciiArray[charconv] doesn't change because it is a post increment asciiArray[charconv]++; , either change it to a pre increment ++asciiArray[charconv]; or write asciiArray[charconv] = asciiArray[charconv]+1;
Here is a link to this why it doesn't increment.
Also you can change the loop like this,more simplified:
for (unsigned int i = 0; i < in.length(); i++)
{
ch = in[i];
charconv = static_cast<int>(ch);
asciiArray[charconv]++;
}
change the type of found to std::vector<char>::iterator coz find returns an iterator to the first element in the range that compares equal to val & if no elements match, the function returns last.
std::vector<char>::iterator found = find(alreadyprintedcharacters.begin(), alreadyprintedcharacters.end(), ch);
Then your condition should be like
if((asciiArray[ch] > 1) && (ch!=alreadyprinted) && (found == alreadyprintedcharacters.end()))
I don't quite get why you need all of that code (given you stated you can't use std::map).
You declared an array of 256 and set each item to 0, which is OK:
for (int i = 0; i < 256; i++)
asciiArray[i] = 0;
Now the next step should be simple -- just go through the string, one character at a time, and increment the associated value in your array. You seem to start out this way, then go off on a tangent doing other things:
for (unsigned int i = 0; i < in.length(); i++)
{
ch = in[i]; // ok
asciiArray[ch]++;
We can set a boolean to true if we discover that the character count we just incremented is > 1:
bool dup = false;
for (unsigned int i = 0; i < in.length(); i++)
{
ch = in[i]; // ok
asciiArray[ch]++;
if ( asciiArray[ch] > 1 )
dup = true;
}
That is the entire loop to preprocess the string. Then you need a loop after this to print out the results.
As to printing, just go through your array only if there are duplicates, and you know this by just inspecting the dup value. If the array's value at character i is > 1, you print the information for that character, if not, skip to the next one.
I won't show the code for the last step, since this is homework.
Just met similar question last week, here is what I did, maybe not a best solution, but it did work well.
string str("aer08%&#&%$$gfdslh6FAKSFH");
vector<char> check;
vector<int> counter;
//subscript is the bridge between charcheck and count. counter[sbuscript] store the times that check[subscript] appeared
int subscript = 0;
bool charisincheck = false;
for (const auto cstr : str) //read every char in string
{
subscript = 0;
charisincheck = false;
for (const auto ccheck : check) // read every element in charcheck
{
if (cstr == ccheck)//check if the char get from the string had already existed in charcheck
{
charisincheck = true; //if exist, break the for loop
break;
}
subscript++;
}
if (charisincheck == true) //if the char in string, then the count +1
{
counter[subscript] += 1;
}
else //if not, add the new char to check, and also add a counter for this new char
{
check.push_back(cstr);
counter.push_back(1);
}
}
for (decltype(counter.size()) i = 0; i != counter.size(); i++)
{
cout << check[i] << ":" << counter[i] << endl;
}met
import java.util.*;
class dublicate{
public static void main(String arg[]){
Scanner sc =new Scanner(System.in);
String str=sc.nextLine();
int d[]=new int[256];
int count=0;
for(int i=0;i<256;i++){
d[i]=0;
}
for(int i=0;i<str.length();i++){
if(d[str.charAt(i)]==0)
for(int j=i+1;j<str.length();j++){
if(str.charAt(i)==str.charAt(j)){
d[str.charAt(i)]++;
}
}
}
for(char i=0;i<256;i++){
if(d[i]>0)
System.out.println(i+" :="+(d[i]+1));
}
}
}
//here simple code for duplicate characters in a string in C++
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main(){
clrscr();
char str[100];
cin>>str;
int d[256];
int count=0;
for(int k=0;k<256;k++){
d[k]=0;
}
for(int i=0;i<strlen(str);i++){
if(d[str[i]]==0)
for(int j=i+1;j<strlen(str);j++){
if(str[i]==str[j]){
d[str[i]]++;
}
}
}
for(int c=0;c<256;c++){
if(d[c]>0)
cout<<(char)c<<" :="<<(d[c]+1)<<"\n";
}
getch();
}

I'm having trouble with my conversion program

For my class, I am to write a program in C++ that converts each character in a sentence to the opposite case (upper to lower, lower to upper). We are supposed to use arrays and a user-defined method, and this is what I came up with:
#include <iostream>
using namespace std;
// declare variables
int count = 0; // array counter
int i = 0; // loop control
char ch[100]; // each character entered will be stored in this array
char newCh[100]; // this will hold each character after its case has been changed
main()
{
cout << "Enter a sentence." << endl; // prompts user
while ( ch[count] != '\n' ) // loop continues until "enter" is pressed
{
cin >> ch[count]; // store each character in an array
count += 1; // increment counter
}
int convert(); // call user-defined function
}
// even though it isn't necessary, we are using a user-defined function to perform the conversion
int convert()
{
for ( i = 0; i >= 0; i++ )
{
if ( (ch[i] > 64) and (ch[i] < 91)
)
{
newCh[i] = tolower(ch[i]);
}
else
{
newCh[i] = toupper(ch[i]);
}
cout << newCh[i];
}
}
I'm not sure why, but it doesn't work. I don't believe that my while loop is terminating and executing the rest of the program. Any advice would be greatly appreciated.
The loop condition in while ( ch[count] != '\n' ) is wrong, as all entries in ch will be initialized to zero by the compiler, and as you increase count inside the loop the condition will never be false and you have an infinite loop, causing you to write beyond the limits of the array.
And writing beyond the limits of an array leads to undefined behavior, and will cause your whole program to be illegal.
I suggest you learn about std::string and std::getline.
There's a problem with your for loop - you want for ( i = 0; i < count; i++ ). Also your function can be void and you need to pass the count value into it (and you just need to invoke it with convert() without int or void in front.
I have rewrite your code with some modification. The following code works perfectly in my machine -
#include <iostream>
#include<cstdio>
#include<string>
using namespace std;
void convert(char *, int);
string line;
char input[1024];
char output[1024];
main()
{
cout << "Enter a sentence." << endl;
while (getline(cin, line)) { // POINT 1
cout<< line<<endl;
//converting to char array since you need char array
//POINT 2
for(int i=0; i< line.length(); i++){
input[i]=line[i];
}
convert(input, line.length());
cout<<output<<endl;
input[1024] = {0}; //POINT 3
output[1024] = {0};
}
}
//Custom Convert Method
void convert(char input[], int size){
for(int i = 0; i < size; i++){
if(input[i] >= 'a' && input[i] <= 'z'){
output[i] = toupper(input[i]);
} else {
output[i] = tolower(input[i]);
}
}
}
Note some points (in my comment) here -
POINT 1: reading a n entire line using getline() method. Here line is a string
POINT 2: since you need char array here I am converting the string line to char array input[1024]
POINT 3: input and output array are being reset to work with the next value;
Output of the code:
"Ctrl+C" will terminate the program
Hope it will help you.
Thanks a lot.

Memory pointer cleared when returned with object

This has been destroying me for a while. I'm sure there's a reason for this:
chain operator+(chain c)
{
chain<Object> result;
for (int i = 0; i < length(); i++)
{
result.insert(*(Object*)(memory+(i*sizeof(Object))));
}
for (int i = 0; i < c.length(); i++)
{
result.insert(c[i]);
}
for (int i = 0; i < result.length(); i++) // This for loop successfully shows all objects in result
{
cout << result[i];
}
return result;
}
When the value is returned, ie:
chain<int> a;
cin >> a; // enter "5 6 7 8"
chain<int> b;
cin >> b; // enter "9 10 11 12"
chain <int> c = a+b;
cout << c; // Returns "0 0 7 8 9 10 11 12"
The first two numbers are always 0. I can't figure out why. This only happens when adding two chains together; if I cout a or b, I get all of the values.
I would really appreciate it if anyone has any info to share :)
EDIT**
Full Source
#ifndef CHAIN_H
#define CHAIN_H
#include <iostream>
#include <stdlib.h>
using namespace std;
template <class Object>
class chain
{
public:
chain(){
memorySize = 8;
memory = calloc(memorySize, sizeof(Object));
count = 0;
}
chain(Object item){
memorySize = 8;
memory = calloc(memorySize, sizeof(Object));
count = 0;
insert(item);
}
chain(chain & original){
memorySize = 8;
memory = calloc(memorySize, sizeof(Object));
count = 0;
for (int i = 0; i < original.length(); i++)
{
insert(original[i]);
}
}
~chain(){
free(memory);
}
chain operator+(chain c){
chain<Object> result;
for (int i = 0; i < length(); i++)
{
result.insert(this->operator[](i));
}
for (int i = 0; i < c.length(); i++)
{
result.insert(c[i]);
}
for (int i = 0; i < result.length(); i++)
{
cout << result[i];
}
return result;
}
Object & operator[](int pos){
return *(Object*)(memory+(pos*sizeof(Object)));
}
int length(){
return count;
}
void insert(Object item){
if (count == memorySize)
{
doubleMemory();
}
this->operator[](count) = item;
count++;
}
private:
int count;
int memorySize;
void * memory;
void doubleMemory(){
memorySize *= 2;
memory = realloc(memory, (memorySize*sizeof(Object)));
}
};
template <class Object>
ostream& operator<<(ostream& out, chain<Object>& c){
for (int i = 0; i < c.length(); i++)
{
out << c[i] << " ";
}
}
template <class Object>
istream& operator>>(istream& in, chain<Object>& c){
char ch;
int number = 0;
int sign;
while(ch != '\n')
{
ch = in.get();
if (ch == '-')
{
sign = 1;
}
else if (ch >= '0' && ch <= '9')
{
number *= 10;
number += (ch-48);
}
else if (ch == ' ' || ch == '\n')
{
number = sign == 1? 0 - number : number;
c.insert(number);
sign = 0;
number = 0;
}
}
}
#endif
Here's the code I'm testing against:
#include "chain.h"
using namespace std;
int main(){
chain<int> a, b, c;
chain<int> d(10);
chain<int> e(d);
cin >> a;
cout << endl;
cout << endl;
c = a+d;
cout << c;
}
~
The code you’ve shown isn’t the problem. The real problem is probably either in the copy constructor or the destructor of chain – maybe also in the insert method (or, on C++11, in the move constructor).
(It could also be in Object’s copy constructor but I think that’s unlikely.)
EDIT: Oh my. Don’t write such code in C++. It’s unsafe left, right and center. As long as Object is a POD you should be fine but if it isn’t this code yields undefined behaviour. In particular, it doesn’t call the proper constructors and destructors for the objects you store in your chain.
Furthermore, your copy constructor should take an argument of type chain const& since you’re not modifying the passed chain. This in turn requires that you make your class const correct by providing an appropriate const overload of operator [].
Finally and most glaringly, you violate the rule of three because you don’t implement operator = for your chain. Trying to assign one chain to another will consequently result in double frees.
Generally avoid calloc and free and use a standard container instead, or, if that’s not an option, use new[] plus a smart pointer like boost::shared_array to manage memory (but do not use delete[]).
Another thing, never use using namespace in a header file, it will pollute the namespace and lead to name conflicts in the weirdest places.
Well, I see the following problem regarding the copy-constructor. It seems to me that you want this to act as a copy-constructor:
chain(chain & original){
memorySize = 8;
memory = calloc(memorySize, sizeof(Object));
count = 0;
for (int i = 0; i < original.length(); i++)
{
insert(original[i]);
}
}
But it can't since the declaration of the method is wrong. You are missing the const modifier in the copy argument. If you don't include it, the compiler assumes that you are only declaring a normal constructor which works with a mutable reference, and this is not the copy-constructor you need to define following the
Rule of Three.
Just change this:
chain(chain & original){
to this:
chain(const chain & original){
This will probably solve a wrong memory handling, and hopefully you will get a different output in your program.