D
dev_15
Hi, i have the following program to display the input received
separated into any word that has Uppercase letters and all lowercase
words.
For the following input "Upper lower" i get the following
output on the console
Upper
0002A634lower
0002A634
I was expecting just
Upper
lower
Why am i getting the print of some memory address presumably, code
below:
using std::string;
using std::vector;
using std::cin;
using std::cout;
using std:
stream;
using std::endl;
bool IsUpper(const string& s);
ostream& Write(ostream& out, vector<string>& v);
vector<string> GetUpper(vector<string>& v);
int _tmain()
{
vector<string> vec;
string s;
while (cin >> s)
vec.push_back(s);
vector<string> Upper = GetUpper(vec);
cout << Write(cout, Upper);
cout << Write(cout , vec);
return 0;
}
ostream& Write(ostream& out, vector<string>& v)
{
for(vector<string>::size_type i =0; i!=v.size(); ++i)
{
out << v << endl;
}
return out;
}
vector<string> GetUpper(vector<string>& v)
{
vector<string> Upper;
vector<string>::iterator iter = v.begin();
while (iter != v.end())
{
if (IsUpper(*iter))
{
Upper.push_back(*iter);
iter = v.erase(iter);
}
else
{
++iter;
}
}
return Upper;
}
bool IsUpper(const string& s)
{
bool ret = false;
typedef string::size_type string_size;
string_size i = 0;
while (i != s.size() && (ret==false))
{
if (isupper(s))
ret = true;
++i;
}
return ret;
}
separated into any word that has Uppercase letters and all lowercase
words.
For the following input "Upper lower" i get the following
output on the console
Upper
0002A634lower
0002A634
I was expecting just
Upper
lower
Why am i getting the print of some memory address presumably, code
below:
using std::string;
using std::vector;
using std::cin;
using std::cout;
using std:
using std::endl;
bool IsUpper(const string& s);
ostream& Write(ostream& out, vector<string>& v);
vector<string> GetUpper(vector<string>& v);
int _tmain()
{
vector<string> vec;
string s;
while (cin >> s)
vec.push_back(s);
vector<string> Upper = GetUpper(vec);
cout << Write(cout, Upper);
cout << Write(cout , vec);
return 0;
}
ostream& Write(ostream& out, vector<string>& v)
{
for(vector<string>::size_type i =0; i!=v.size(); ++i)
{
out << v << endl;
}
return out;
}
vector<string> GetUpper(vector<string>& v)
{
vector<string> Upper;
vector<string>::iterator iter = v.begin();
while (iter != v.end())
{
if (IsUpper(*iter))
{
Upper.push_back(*iter);
iter = v.erase(iter);
}
else
{
++iter;
}
}
return Upper;
}
bool IsUpper(const string& s)
{
bool ret = false;
typedef string::size_type string_size;
string_size i = 0;
while (i != s.size() && (ret==false))
{
if (isupper(s))
ret = true;
++i;
}
return ret;
}