TRANSLATING FROM C TO C++

Mar 1, 2014 at 4:42am
closed account (oy721hU5)
How do you convert this code to be C++ code, I know you use the #include locale, string, iostream and so on.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 /* toupper example */
#include <stdio.h>
#include <ctype.h>
int main ()
{
  int i=0;
  char str[]="Test String.\n";
  char c;
  while (str[i])
  {
    c=str[i];
    putchar (toupper(c));
    i++;
  }
  return 0;
}
Mar 1, 2014 at 5:08am
I would use std::string and std::transform (from <algorithm>), like this:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>

int main() {
    std::string str = "Test String.\n";
    std::transform(str.begin(), str.end(), str.begin(), toupper);
    std::cout << str;
    return 0;
}


You only need <locale> if either you are going to be using internationalization or require multiple locales within the same program.
Mar 1, 2014 at 5:26am
closed account (oy721hU5)
Is there an easier way to resolve this? Basically, can the original structure of the code in C be as similarly and closely translated into c++. I have not learned the std::transform.
Mar 1, 2014 at 5:34am
The original code is valid C++. If anything, to write it exactly as it is in C++ you could do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <cstdio>
#include <cctype>

int main ()
{
  int i=0;
  char str[]="Test String.\n";
  char c;
  while (str[i])
  {
    c=str[i];
    putchar (toupper(c));
    i++;
  }
  return 0;
}


However, C++ programmers should prefer the solution I posted above, due to being smaller and more intuitive as to its function. You should probably get used to learning the STL and the huge number of useful function that it provides. Only real way to fully learn it, though, is to spend time looking through the references that can be found, for example on this website.
Mar 1, 2014 at 5:56am
closed account (oy721hU5)
Is there a way by preserving the same code, but by using the 'cout'
Mar 1, 2014 at 5:59am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
//...

int main () 
{
  //...
  while (str[i])
  {
    c=str[i];
    std::cout << toupper(c);
    i++;
  }
  return 0;
}
Mar 1, 2014 at 6:29am
closed account (oy721hU5)
Hey, what does putchar do?
Mar 1, 2014 at 6:30am
@NT3, remember that touppper returns a number not a character. So you will need to explicitly cast the value returned, to a char in order to see the character.
Mar 1, 2014 at 6:48am
john you had putchar in your code :P If you are trying to go from c-c++ you should at least know what the c code does.

anyways it writes a single character to the output.

http://www.cplusplus.com/reference/cstdio/putchar/

Mar 1, 2014 at 6:50am
@Smac89: Oh, yes, I forgot that. Oops.
@John1: putchar prints a single character to the standard output (normally the console).
Mar 1, 2014 at 10:45am
john1 wrote:
I have not learned the std::transform.
So start learning now: http://en.cppreference.com/w/cpp/algorithm/transform
C++ standard library provides many useful functions you often had to write manually in C. It is good to know and use whatever your selected language can provide you with.

Another variant of NT3 code:
1
2
3
4
5
6
7
8
9
10
11
12
#include <algorithm>
#include <cctype>
#include <iostream>
#include <iterator>
#include <string>

int main() 
{
    std::string str("Test String.\n");
    std::transform(str.begin(), str.end(), 
                   std::ostream_iterator<char>(std::cout), toupper);
}
Topic archived. No new replies allowed.