TRANSLATING FROM C TO C++

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;
}
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.
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.
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.
closed account (oy721hU5)
Is there a way by preserving the same code, but by using the 'cout'
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;
}
closed account (oy721hU5)
Hey, what does putchar do?
@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.
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/

@Smac89: Oh, yes, I forgot that. Oops.
@John1: putchar prints a single character to the standard output (normally the console).
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.