0

I'm trying to create a program in C++ where it will turn whatever sentence you input into

-all caps

-all lowercase

-capitalized letters will be switched with lowercase & vice versa

I've written everything up correctly so far, the only part I can't figure out is how to get it to give me back the three individual responses. If I only have one function within the while loop it works fine, but not with all three & I don't know how to get around that. Thanks!

Here's my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

void allCaps(char ch);
void allLows(char ch);
void mixed(char ch);
char ch;

int main()
{

  printf("Enter Sentence:\n");

  while((ch=getchar()) != '\n')
  {

    allCaps(ch);
    printf("%c",ch,"\n");
    allLows(ch);
    printf("%c",ch,"\n");
    mixed(ch);
    printf("%c",ch,"\n");


  }

  return ch;
}
void allCaps(char ch)
{
  if(ch >= 'a' && ch <='z')
     ::ch=ch-32;

}
void allLows(char ch)
{
  if(ch>='A'&&ch<='Z')
     ::ch=ch+32;
}

void mixed(char ch)
{
  if(ch >= 'a' && ch <='z')
     ::ch=ch-32;
  else if(ch>='A'&&ch<='Z')
     ::ch=ch+32;
}
  • 3
    Look up `toupper` and `tolower` – Ed Heal Mar 23 '17 at 09:40
  • As well as [`isupper`](http://en.cppreference.com/w/cpp/string/byte/isupper) and [`islower`](http://en.cppreference.com/w/cpp/string/byte/islower). – Some programmer dude Mar 23 '17 at 09:40
  • 1
    Char is single character you should use string or array of chars – cymruu Mar 23 '17 at 09:40
  • 1
    Sidenode in addition to all other comments: having this _global_ variable `ch` is a terrible thing. – Jabberwocky Mar 23 '17 at 09:41
  • Also note that [`getchar`](http://en.cppreference.com/w/c/io/getchar) returns an `int`. And that the input might not end with a newline. – Some programmer dude Mar 23 '17 at 09:43
  • 3
    And if you're really programming in C++ (the only C++-specific thing you use is the scope operator to modify the global variable `ch`) then why not use actual C++ functionality like input or output streams and references? Perhaps you should take a few steps back and [find a good beginners book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to read? – Some programmer dude Mar 23 '17 at 09:44

2 Answers2

0

What you are doing is outputting each letter 3 times in a row as you read it.

Instead of doing this, you could define for example three strings like this: std::string s1="",s2="",s3=""; and then you can do

s1+=allCaps(ch);s2+=allLows(ch);s3+= mixed(ch);

inside your while loop and at the end you simply output s1,s2 and s3.

Sunil Garg
  • 14,608
  • 25
  • 132
  • 189
Sorin Tirc
  • 113
  • 6
0

Besides the fact that you could use io-streams, tolower and toupper, references and other specific functionalities your main problem is the global variable ch.

Consider what happens if you input the char 'A'. allCaps() will get the upper case 'A' as an input, convert it into a (still) upper case 'A' and store the result in the global variable ch. This ch (now (or still) containing 'A') will now be passed to allLows() which again will now convert the upper case 'A' into an lower case 'a' and store the result again in your global ch. Here comes the interesting part. Remember you initially entered an upper case 'A' so mixed() should produce a lower case letter right? But since allLows() already converted your input to an lower case letter, mixed() will now produce a upper letter again.

You can fix this by not declaring ch globally and make your functions return the result instead. e.g. see the new allCaps()

char allCaps(char ch) 
{
  if(ch >= 'a' && ch <='z')
    return ch - 32;
  return ch;
}

And use it like so

int main()
{
  printf("Enter Sentence:\n");

  char ch; // <-- local declaration
  while((ch=getchar()) != '\n')
  {
    printf("%c\n", allCaps(ch)); <-- all get the same (not modified) input
    printf("%c\n", allLows(ch));
    printf("%c\n", mixed(ch));
  }
}

However this will still not do exactly what you want. Instead of reading a single character read a whole std::string for example using std::getline and then loop over each character and convert it using tolower and toupper

muXXmit2X
  • 2,745
  • 3
  • 17
  • 34