I was trying to make Substrings of exactly half the length of original string and then sorting them in order of their ASCII codes and finally checking their equality by using strcmp() library function. (That is i was trying to check if the parent string can be divided in two equal halves.) So here is what I did:
Took input of parent string from user by cin.
Then I used variable hlength to store the value of half of the length of original string.
Now I declared two strings of equal lengths (equal to hlength ) so that first half of string is copied to substring named "s1" and the rest half is copied to substring "s2". i used for loops to copy the elements of the string.
To check whether the strings are correctly copied or not I printed each element of string just after copying them by using cout<< as you can see in the attached code.
Till now everything seemed alright as the result of individual elements when being printed were correct.
But!! here comes a situation which I never faced before: When I tried to print complete string using cout<< The output was blank.
Later instead of printing the strings I just tried comparing them with the strcmp() function but it resulted in error: cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string}' to 'const char' for argument '1' to 'int strcmp(const char*, const char*)'*
#include <bits/stdc++.h>
#define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define llt long long int
using namespace std;
int main()
{
int length,hlength,i,result;
string s;
cin>>s;
length = s.length();
hlength = length/2;
string s1,s2;
for(i = 0; i< hlength;i++)
{
s1[i] = s[i];
cout<<s1[i]<<endl; //each element gets printed successfully
}
for(i = 0; i< hlength ; i++)
{
s2[i] = s[length -1 - i];
cout<<s2[i]<<endl; //the elements are printed successfully, but obviously in the reverse order( order not matters)
}
cout<<s1<<endl<<s2; // no strings printed
sort(s1.begin(),s1.end());
sort(s2.begin(),s2.end());
result = strcmp(s1 , s2); //ERROR
cout<<result<<endl;
return 0;
}
PS: The original string is even in length.
Please help me know why the string is not printed but individual elements are printed correctly. And what does that error means?