strcpy & strcmp, what is wrong?

This is a discussion on strcpy & strcmp, what is wrong? within the C Programming forums, part of the General Programming Boards category; hi, well here is a newbie's question. What is wrong with this program. I am writing a function to perform ...

  1. #1
    Registered User Silverdream's Avatar
    Join Date
    Feb 2002
    Posts
    53

    Question strcpy & strcmp, what is wrong?

    hi,
    well here is a newbie's question. What is wrong with this program. I am writing a function to perform strcpy:

    #include <stdio.h>
    #include <string.h>
    #include <conio.h>

    void xstrcpy(char *s);

    int main()
    {
    char s[80];
    printf("Enter the string\n");
    scanf("%s",s);
    xstrcpy(s);
    getch();
    return 0;
    }

    void xstrcpy(char *s)
    {
    char s1[80];
    while(*s!='\0');
    {
    *s1=*s;
    s1++;
    s++;
    }
    *s1='\0';
    printf("S1 = %s",s1);
    }

    and this one which gives me an error in the exit function. I am using borland C++ 5.5 compiler.

    #include <stdio.h>
    #include <string.h>

    void xstrcmp(char *s1, char *s2);

    int main()
    {
    char s1[80],s2[80];
    printf("\n Enter the first string");
    scanf("%s",s1);
    printf("\n Enter the second string");
    scanf("%s",s2);
    xstrcmp(s1,s2);
    return 0;
    }

    void xstrcmp(char *s1, char *s2)
    {
    while(*s1!='\0')
    {
    if(*s1==*s2)
    {
    s1++;
    s2++;
    continue;
    }

    else
    {
    printf("\nDifferent");
    exit();
    }
    }
    printf("\nSame");
    }

    Please help



  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    void xstrcpy(char *s)
    {
    char s1[80];
    while(*s!='\0');
    {
    *s1=*s;
    s1++;
    s++;
    }
    *s1='\0';
    printf("S1 = %s",s1);
    }

    and this one which gives me an error in the exit function. I am sing borland C++ 5.5 compiler.
    Where exactly is this supposed to be copying to? To the buffer? The buffer 'vanishes' afte the function ends. Your function is also flawed, in that, if you pass it a string longer than 79 characters, it will overflow your buffer. Second, you can't just "increment the pointer" for your buffer. Use a counter instead.

    s[x] = s1[x];

    Code:
    void xstrcmp(char *s1, char *s2) 
    { 
        while(*s1!='\0') 
        { 
            if(*s1==*s2) 
            { 
                s1++; 
                s2++; 
                continue; // you don't need this
            } 
            else 
            { 
                printf("\nDifferent"); 
                exit(); 
            } 
        } 
        printf("\nSame"); 
    }
    Use [ code ] and [ /code ] (without the spaces) to wrap your code fragments.

    You don't need the 'continue' because you're using 'if/else'. Also, your code isn't a true comparison. If s2 is longer than s1, then your function will still think that they're identical.

    You should modify it to read:
    Code:
    if( *s2 == '\0' )
        printf("Same.\n");
    else
        printf("Different.");
    Instead of just your "same" message.

    In the future, don't just say "compiler gives an error", actually telling us what type of error it gives will greatly increase your chance of getting a helpful answer. Not everyone who reads here actually compiles all code. I almost never do, and I rarely feel like reading tons of lines of unformatted code to find an error.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User Silverdream's Avatar
    Join Date
    Feb 2002
    Posts
    53
    Well here it is,

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    
    void xstrcpy(char *s);
    
    int main()
    {
      char s[80];
      printf("Enter the string\n");
      scanf("%s",s);
      xstrcpy(s);
      getch();
      return 0;
    }
    
    void xstrcpy(char *s)
    {
         char s1[80];
         while(*s!='\0');
         {
    	 *s1=*s;
    	 s1++;
    	 s++;
         }
     *s1='\0';
     printf("S1 = %s",s1);
    
    }
    Well thanx for telling me about the [code] [ /code]. I have done that now. Secondly u told me that i cant increment the pointer . why not? I am incrementing the pointer to point to the next character. Isnt that right? Also u told me that if a string above 80 characters length is passed then buffer will overflow. wont it only accept only 80 characters and truncate the others? If there is a way to handle that error, how can i do it? Please correct me if i am wrong. I am a newbie to C.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    have a look at your while loop in the function....

    you have while(*s!='\0'); ...you terminated the loop.

    you could use while(*s) this is same as while(*s!='\0') - which means while *s has a value i.e. not NULL.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    
    void xstrcpy(char *s);
    
    int main()
    {
      char s[80];
      int count = 0;
    
      printf("Enter the string\n");
      /* this will ensure string wont go out of bounds*/
      while(count < 80)
      {
               s[count]= getche(); /*getche echoes input to display */
               if(s[count] == '\r') break;
               count++;
      }
      s[count] ='\0'; /* add null */
      xstrcpy(s);
      getch();
      return 0;
    }
    
    void xstrcpy(char *s)
    {
         char s1[80];
         int counter = 0;
    
         while(*s)
         {
    	/* *s1=*s; s1 is not a pointer so create int counter*/
                   s1[counter++] = *s;
                   s++;
         }
        s1[counter] ='\0';
        printf("S1 = %s",s1);
    }
    Last edited by bigtamscot; 02-13-2002 at 05:26 AM.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  5. #5
    Registered User Silverdream's Avatar
    Join Date
    Feb 2002
    Posts
    53
    Thanx bigtamscot. But since i am new to this. i am unable to follow ur code.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    
    void xstrcpy(char *s);
    
    int main()
    {
      char s[80];
      printf("Enter the string\n");
      scanf("%s",s);
      xstrcpy(s);
      getch();
      return 0;
    }
    
    void xstrcpy(char *s)
    {
         char s1[80], *s2;
         s2=s1;
         while(*s!='\0')
         {
    		 *s2=*s;
    		 s2++;
    		 s++;
    	 }
    	 *s2='\0';
    	 printf("S1 = %s",s1);
    
    }
    I have done this. is this correct. Can u explain to me the part where u checking if the string is less than 80 characters?
    Thanx
    Last edited by Silverdream; 02-13-2002 at 06:08 AM.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    yes your last code should do the trick, assigning a pointer instead of an int counter in your function okay.

    Try not to use scanf(); to input a string, it can lead to countless bugs - the most common is no bounds checking (you allocate memory for 80 characters but the user enters 85-say).

    while(*s) is the equivalent of saying while the value at address is not equal zero(TRUE= any non zero value, could be -1), once zero is encountered loop terminates as value at the address is zero (FALSE=0).

    consider creating a flag to test whether a condition is true or false.
    Code:
       int flag = 1; 
    
       if(flag != 0) printf("Flag is not False\n");
       else printf("Flag is False\n");
    
       if(flag) printf("Flag is not False\n");
       else printf("Flag is False\n");
    
       flag = flag - 1;  /* flag-- now = 0*/
    
       if(flag == 0) printf("Flag is False\n");
       else printf("Flag is not False\n");
    
       if( ! flag) printf("Flag is False\n");
       else printf("Flag is not False\n");
    you get the idea.
    result of this would be

    Flag is not False
    Flag is not False
    Flag is False
    Flag is False

    NOTE the value of flag need not be one for if(flag) to be true, as long as it is not zero it will be true..could be minus 5.

    very useful when using one of string.h functions strcmp();
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  7. #7
    Registered User Silverdream's Avatar
    Join Date
    Feb 2002
    Posts
    53

    Talking

    Thanx. Got it now

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The problem is this:

    char array[256], *ar;

    ar = array;

    ar++; //valid

    array++; //not valid

    While you can use the name of an array as a pointer, you can't use standard pointer incrementation/decrementation on it. You can't do 'array++', because 'array' always points to array[0].

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcpy
    By Luigi in forum C++ Programming
    Replies: 17
    Last Post: 02-16-2003, 03:11 PM
  2. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  3. Hmm... Anything wrong with this?
    By jon_nc17 in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2002, 07:33 PM
  4. strcmp, any borders?
    By demonus in forum C Programming
    Replies: 3
    Last Post: 11-15-2002, 01:48 AM
  5. strcmp
    By coug2197 in forum C++ Programming
    Replies: 4
    Last Post: 02-01-2002, 11:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21