Please can anyone tell me what is wrong with my program? for all cases it is displaying not identical | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Please can anyone tell me what is wrong with my program? for all cases it is displaying not identical

#include <iostream> // program to check for identical strings using namespace std; int checkstring(char a[],char b[]) { for(int i = 0;a[i]!='/0'&& b[i]!='/0';i++) { if(a[i]!= b[i]) { return -1; //if the strings are not identical then return -1 is executed and } program terminates returning -1 which 'x' of main would capture } return 0; // the flow of control comes to return 0 only if the strings are identical } int main(void) { char a[100]; char b[100]; cout<<"enter the first string"<<endl;cin.getline(a,100); cout<<"Enter the second string"<<endl;cin.getline(b,100); int x = checkstring(a,b); if(x==-1) { cout<<"not identical"; } if (x==0) { cout<<"identical"; } return 0; }

2nd Dec 2017, 7:21 AM
RR2001
RR2001 - avatar
2 Answers
+ 5
Thats a '\0' that you need to use for comparision, not a '/0'... Change your loop's test condition to : a[i]!='\0' && b[i]!='\0'
2nd Dec 2017, 7:26 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 1
Once again i forgot to replace / by \. Thanks for pointing out..
2nd Dec 2017, 7:28 AM
RR2001
RR2001 - avatar