Okay so, I don’t really think there is any error, but I will propose you a modified version of your code, which will be more readable, more efficient, and less error prone:
// #include<bits/stdc++.h> I don't know what that include is, use more specific header based on your needs like
#include <string> // for std::string
#include <iostream> // for std::cout
// using namespace std;
// Avoid using "using namespace", it's a really bad practice in C++
int main()
{
std::string name;
std::string sname;
int t;
std::cin >> t;
while(t--)
{
std::getline(std::cin, name);
// *(name.begin()) -= 32;
// Use operator[], it was made for that purpose
name[0] -= 32;
size_t pos = name.find(" ");
if(pos == std::string::npos)
{
std::cout << name;
}
name[pos] = 49; // pretty dangerous since pos could be std::string::npos, which is the max value for a size_t
pos = name.find(" ");
size_t first_one = name.find("1");
// here you should check if first_one != std::string::npos
if(pos == std::string::npos)
{
name[first_one + 1] -= 32;
sname = name.substr(name.find("1") + 1); // dangerous
std::cout << name[0] << ". " << sname;
}
else
{
name[first_one + 1] -= 32;
name[pos + 1] -= 32;
sname = name.substr(name.find(" ")+1); // dangerous too
std::cout << name[0] << ". " << name[name.find("1")+1] << ". " << sname;
}
// printf("\n"); Avoid using printf, std::cout is here for c++ (printf comes from C)
std::cout << '\n';
}
return 0;
}
So to conclude, avoid “using namespace”, that’s bad, for explanation, google, first link, etc..
Use websites like cppreference.com or cplusplus.com to see methods and usages of objects like std::string or std::cout.
Avoid doing std::cout << "a" << "b";
, instead do std::cout << "ab";
, or if you need to output single chars, std::cout << 'a';
.
Verify everything, a big and very important rule for devs is “Never trust the user”, always imagine they are gonna try to break your program, you might think “yeah but i will be the only user”, that’s a bad reason, take good habits early. So always check, here if t is negative, your program will be almost infinite, if there is no space or ‘1’, it will explode, etc.. All of these small mistakes might be security flaws in bigger software.
Good luck 🙂
2
solved Can not account for an extra line in output