0

Is string function is possible to use for string datatype in c++?

According to me it is not possible...What about your opinion?

27th Jul 2020, 2:02 PM
Nitesh Srivastava
Nitesh Srivastava - avatar
11 Réponses
+ 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
27th Jul 2020, 2:49 PM
Ipang
+ 1
Your question is not clear. Explain.
27th Jul 2020, 2:08 PM
Lakshay Mittal
Lakshay Mittal - avatar
+ 1
(strcmp(s1,s2) == 0) is the right way.
27th Jul 2020, 2:22 PM
Lakshay Mittal
Lakshay Mittal - avatar
+ 1
That is correct for char array ,,,,but not for string datatype
27th Jul 2020, 2:24 PM
Nitesh Srivastava
Nitesh Srivastava - avatar
+ 1
Ipang Yes that r methods to do operations in string data type... Like using ==,,,,!=,,,s1=s2 for copy
27th Jul 2020, 4:02 PM
Nitesh Srivastava
Nitesh Srivastava - avatar
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++ ?
27th Jul 2020, 2:20 PM
Nitesh Srivastava
Nitesh Srivastava - avatar
0
No it shows error because
27th Jul 2020, 2:23 PM
Nitesh Srivastava
Nitesh Srivastava - avatar
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; }
27th Jul 2020, 2:36 PM
Nitesh Srivastava
Nitesh Srivastava - avatar
0
check the code and tell what is the output?
27th Jul 2020, 2:36 PM
Nitesh Srivastava
Nitesh Srivastava - avatar
0
My bad, You're right.
27th Jul 2020, 2:49 PM
Lakshay Mittal
Lakshay Mittal - avatar