By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,131 Members | 1,867 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,131 IT Pros & Developers. It's quick & easy.

problem on implementing pointer on string

P: 16
Hi,
I have a problem in writing a code using pointer.My assignment is to create a function that compre 2 strings by using a pointer.It returns 1 if they are different and 0 if they are the same.I come up with the following code,but i cann't implement pointer ,please help me in modifying it.The existing code has some bugs but i couldn't identify where is it
Expand|Select|Wrap|Line Numbers
  1. void main() {
  2.      char str1;
  3.      char str2;
  4.      cout<<"Enter the first string";
  5.      cin>>str1;
  6.      cout<<"Enter the second string";
  7.      cin>>str2;
  8.      compstr(str1,str2);
  9. }
  10. void compstr(char s1,char s2){
  11.  if(strcmp(s1,s2)==0){
  12.  return 1;
  13. else 
  14.  return 0;
  15. }
Feb 20 '07 #1
Share this Question
Share on Google+
3 Replies


P: 40
Hi,
I have a problem in writing a code using pointer.My assignment is to create a function that compre 2 strings by using a pointer.It returns 1 if they are different and 0 if they are the same.I come up with the following code,but i cann't implement pointer ,please help me in modifying it.The existing code has some bugs but i couldn't identify where is it
void main(){
char str1;
char str2;
cout<<"Enter the first string";
cin>>str1;
cout<<"Enter the second string";
cin>>str2;
compstr(str1,str2);
}
void compstr(char s1,char s2){
if(strcmp(s1,s2)==0){
return 1;
else
return 0;
}


Hello Friend..

You want to do it by using pointers right...?
Then first use two character pointers...
Use two char variables to read the two strings..
Assign their addresses to character pointers..
Now pass their address to function by using poiter variables...

Function strcmp will return 0 if they are equal else any nonzero integer..
Here you are returning value 1 if they are equal where it is totally opposite of what you require( value 1 should be returned if they are different as you mentioned) So change this also...

Try it...
All the best...
Feb 20 '07 #2

Ganon11
Expert 2.5K+
P: 3,652
Also, I think using strcmp within your compstr function is kind of pointless - if you were allowed to use strcmp, you could just use it rather than hiding it within your own function. My guess is that you need to write your own version of strcmp without using it.
Feb 20 '07 #3

Banfa
Expert Mod 5K+
P: 8,916
Also you are trying to return a value from a void function.
Feb 20 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.