0

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:

  1. Took input of parent string from user by cin.

  2. Then I used variable hlength to store the value of half of the length of original string.

  3. 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.

  4. 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.

  5. Till now everything seemed alright as the result of individual elements when being printed were correct.

  6. But!! here comes a situation which I never faced before: When I tried to print complete string using cout<< The output was blank.

  7. 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?

  • 2
    `std::string`s are compared with `==`. – tkausl Apr 17 '20 at 11:23
  • There is as well `string::compare` and `strcmp` expects char pointers - as your error message tells you. See: https://stackoverflow.com/questions/5492485/strcmp-or-stringcompare and https://en.cppreference.com/w/cpp/string/byte/strcmp – Jan Hackenberg Apr 17 '20 at 11:29
  • @tkausl and Jan Hackenberg Thank you and Yes you are right but why is my string not getting printed. Is the string not correctly assigned values? – kshitij tripathi Apr 17 '20 at 11:36
  • @kshitijtripathi Gave a more complex answer referencing to this question as well. – Jan Hackenberg Apr 17 '20 at 11:38

1 Answers1

0

strcmp expects char pointers - this is what your error message tells you. There is string::compare or operator == which you can use without this error. See: Stackexchange Topic and cppreference.

For completeness you could also convert your std::strings to char pointers with c_str() method of your strings, but this is kind of ugly.

You see the real values, but you experience undefined behaviour. s1 and s2 are intialized with zero length. Operator [pos] definition:

  • If pos is less than the string length, the function never throws exceptions (no-throw guarantee).

  • If pos is equal to the string length, the const-version never throws exceptions (no-throw guarantee).

  • Otherwise, it causes undefined behavior.

    The last point is what you experience - Maybe you write to a part of memory which does not belong to the string, but can access it in the same scope still. But when you try to cout the string, there is nothing written in the strings memory block. But who knows, its undefined behavior, anything can happen.

Section Exception safety in:

http://www.cplusplus.com/reference/string/string/operator[]/

Community
  • 1
  • 1
Jan Hackenberg
  • 481
  • 3
  • 14