Thread: Questions on String functions!

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

    Question Questions on String functions!

    what is wrong with this function which acts like strncpy

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    
    void xstrncpy(char *s, int n);
    
    int main()
    {
      int n;
      char s[80];
      printf("\nEnter the string\n");
      scanf("%s",s);
      printf("\nEnter the number of characters to be copied");
      scanf("%d",n);
      xstrcpy(s,n);
      getch();
      return 0;
    }
    
    void xstrncpy(char *s, int n)
    {
         int i;
         char s1[80], *s2;
         s2=s1;
         for(i=1;i<=n;i++)
         {
    	 *s2=*s;
    	 s2++;
    	 s++;
         }
     *s2='\0';
     printf("S1 = %s",s1);
    
    }
    and this which is for strset

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void xstrset(char *s, char c);
    
    int main()
    {
    	char s[]="apocalypse";
    	char c='a';
    
    	xstrset(s,c);
    
    	return 0;
    }
    
    void xstrset(char *s, char c)
    {
    	while(*s)
    	{
    		*s=c;
    		s++;
    
    	}
    	printf("%s",s);
    }
    and this which reverses and string

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void xstrrev(char *s, char *s1);
    
    int main()
    {
    	char s[]="sasdh";
    	char s1[10];
    
    	xstrrev(s,s1);
    
    	return 0;
    }
    
    void xstrrev(char *s, char *s1)
    {
    	int len,i;
    	len=strlen(s);
    	while(*s!='\0')
    	{
    		s++;
    	}
    
    	while(len!=0)
    	{
    		s--;
    		*s1=*s;
    		len--;
    	}
    	*s1='\0';
    	printf("%s",s1);
    }
    Also i want to write a custom function for strstr. Can anyone pls help me for this?

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Wrong with xstrncpy is that you are moving the pointer. At the end, the pointer is pointing to the string and therefor you see nothing when printing. It can be done a little easier, assumed is that dest has enough memory.

    Code:
    void xstrncpy (char *dest, char *src, int n)
    {
         int i = 0;
         
         while (src [i] != NULL && i < n)
         {
            dest [i] = src [i];
            i++;
         }
    }

  3. #3
    Registered User Silverdream's Avatar
    Join Date
    Feb 2002
    Posts
    53
    I tried in this , but it doesnt work. Why so?

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    
    void xstrncpy(char *s, int n);
    
    int main()
    {
      int n;
      char s[80];
      printf("\nEnter the string\n");
      scanf("%s",s);
      printf("\nEnter the number of characters to be copied");
      scanf("%d",n);
      xstrncpy(s,n);
      getch();
      return 0;
    }
    
    void xstrncpy(char *s, int n)
    {
         int i=1;
         char s1[80], *s2;
         while(s[i]!='\0' && i<=n)
         {
    	 s1[i]=s[i];
    	 i++;
         }
         *s2='\0';
         printf("S1 = %s",s1);
    
    }
    Please help.

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    First rule of scanf with variables that are not pointers....DONT FORGET THE AMPERSAND!!!!



    Code:
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    
    void xstrncpy(char *s, int n);
    
    int main()
    {
      int n = 0;
      char s[80];
      printf("\nEnter the string\n");
      scanf("%s",s);
      printf("\nEnter the number of characters to be copied\n");
      scanf("%d",&n);
      xstrncpy(s,n);
      getch();
      return 0;
    }
    
    void xstrncpy(char *s, int n)
    {
         int i;
         char s1[n+1];
         for(i = 0;i<n;i++)
         {
         s1[i]=s[i];
         }
         s1[i]='\0';
         printf("S1 = %s",s1);
    
    }
    Last edited by Fordy; 02-14-2002 at 08:16 AM.

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

    but shouldnt this statement

    s1[i]='\0';


    be

    s1[i+1]='\0'

    Correct me if i am wrong.
    Last edited by Silverdream; 02-14-2002 at 09:22 AM.

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Look at it like this....


    Code:
    for(i = 0;i<n;i++)
         {
         s1[i]=s[i];
         }
         s1[i]='\0';
    At the start, i = 0 and if I ask for 3 letters, the loop will iterate 3 times. So with 3 loops,

    i = 1 (at the end of the loop)
    i = 2 (at the end of the loop)
    i = 3 (at the end of the loop)

    so after the for block;

    s1[i]='\0';

    is actually

    s1[3]='\0';

    which is the 4th charector...and the one which you want NULL terminated......

    If you do i+1, then you are terminatin the 5th char...and hoping the 4th doesnt have anything nasty in it...
    Last edited by Fordy; 02-14-2002 at 09:43 AM.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Creating String Functions
    By stickman in forum C++ Programming
    Replies: 8
    Last Post: 04-30-2006, 05:26 PM
  4. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM