problem on implementing pointer on string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • selam
    New Member
    • Nov 2006
    • 16

    problem on implementing pointer on string

    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
    Code:
    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;
    }
  • Manjiri
    New Member
    • Nov 2006
    • 40

    #2
    Originally posted by selam
    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,st r2);
    }
    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...

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

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

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Also you are trying to return a value from a void function.

        Comment

        Working...