String Object from iostream to stdio.h issue

--EDITED QUESTION--
Hi there
I'm new to this forum and c++. I'm working through a c++ book that uses purely iostream examples, and I've been told to only use stdio.h and I can't seem to translate this example from an iostream format to stdio.h. I know the basics of stdio.h but I can't workout how to manage objects. Thank you for any help you can give me.

1
2
3
4
5
6
7
8
9
10
11
12
13
// String Tester
// Demonstrates string objects
#include <iostream>
#include <string>
using namespace std;
int main()
{
   string word1 = "Game";
   string word2("Over");
   string word3(3, ’!’);

   string phrase = word1 + " " + word2 + word3;
   cout << "The phrase is: " << phrase << "\n\n";

I've managed to create string 'phrase' using a char[]
1
2
   char phrase[12]
   sprintf_s( phrase, "%s %s%s", word1, word2, word3 );


However I have no idea how to find the string's size through the .size() function, I don't want to use sizeof. I also need to printf the character at position 0, find the phrase 'eggplant' and erase parts of 'phrase'. Basically I need this translated to stdio.h format.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

   cout << "The phrase has " << phrase.size() << " characters in it.\n\n";
   cout << "The character at position 0 is: " << phrase[0] << "\n\n";
   cout << "Changing the character at position 0.\n";
   phrase[0] = ’L’;
   cout << "The phrase is now: " << phrase << "\n\n";

   for (unsigned int i = 0; i < phrase.size(); ++i)
     {
      cout << "Character at position " << i << " is: " << phrase[i] << endl;
     }
   cout << "\nThe sequence ’Over’ begins at location ";
   cout << phrase.find("Over") << endl;

   if (phrase.find("eggplant") == string::npos)
     {
      cout << "’eggplant’ is not in the phrase.\n\n";
     }
   phrase.erase(4, 5);
   cout << "The phrase is now: " << phrase << endl;
   phrase.erase(4);
   cout << "The phrase is now: " << phrase << endl;
   phrase.erase();
   cout << "The phrase is now: " << phrase << endl;
   if (phrase.empty())
     {
      cout << "\nThe phrase is no more.\n";
     }


return 0;
}
Last edited on
Topic archived. No new replies allowed.