0
Is string function is possible to use for string datatype in c++?
According to me it is not possible...What about your opinion?
10 ответов
+ 3
Nitesh Srivastava
Right, we can't use C-string functions to work with C++ string, because C++ string is not a plain char array like C-string.
Easy way to compare two string in C++ is to use comparison operator e.g.
s1 == s2 // are they equal?
s1 != s2 // are they different?
Or use the `compare` method (works like strcmp) 👇
https://en.cppreference.com/w/cpp/string/basic_string/compare
There are various ways to copy a string in C++, some of common ways I happen to have seen:
string s1 = s2
string s1(s2)
Hth, cmiiw
+ 1
Your question is not clear. Explain.
+ 1
(strcmp(s1,s2) == 0) is the right way.
+ 1
That is correct for char array ,,,,but not for string datatype
+ 1
Ipang
Yes that r methods to do operations in string data type...
Like using ==,,,,!=,,,s1=s2 for copy
0
My question is that if we have two string s1,s2 both are string data type
Now if we do like strcpy(s1,s2);
strcmp(s1,s2);
In c++ ?
0
No it shows error because
0
#include<iostream>
#include<cstring>
using namespace std;
main()
{
string s1,s2;
s2="hello";
cout<<s1<<endl;
cout<<s2<<endl;
cout<<"-------------------------------------------------------"<<endl;
strcpy(s1,s2);
cout<<s1<<endl;
cout<<s2<<endl;
}
0
check the code and tell what is the output?
0
My bad, You're right.