Like Tree9Likes

[Problem] String repeat(String s, int n) that creates and returns a new string

This is a discussion on [Problem] String repeat(String s, int n) that creates and returns a new string within the C Programming forums, part of the General Programming Boards category; finally got it Code: #include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> char* Repeat(char *s,int n) { if(n==0){return " ";} char *a=(char*)malloc(2*sizeof(s)); strcat(a,s); char* ...

  1. #16
    Registered User
    Join Date
    Jul 2011
    Posts
    19
    finally got it

    Code:
     #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    #include<stdlib.h>
    
    
    char* Repeat(char *s,int n)
    {
    	if(n==0){return " ";}
    	
    	char *a=(char*)malloc(2*sizeof(s));
    
    	strcat(a,s);
    
    	
    
    
     char* p=Repeat(a,n-1);
    	strcat(a,p);
    	return a ;
    
    }
    
    
    
    
    int main()
    {
    	 char a[]="Hello";
    	 char *b;
    	 int n;
    	 printf("Enter the no of times u want to repeat\n");
    	 scanf("%d",&n);
    	 b= Repeat(a,n);
    	 printf("%s",b);
       
        
        getchar();
        
    }

  2. #17
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    Nope, you don't got it:
    Code:
    #include<stdio.h>
    #include<conio.h> // I sincerely hope you're not still using Turbo C.  If so, time to upgrade.
    // Pelles C, Code::Blocks with MinGW or even MSVC++ Express are much better, free options
    #include<string.h>
    #include<stdlib.h>
    
    
    char* Repeat(char *s,int n)
    {
        if(n==0) {
            return " ";  // this is not the empty string, it's a string with one space.  "" is the empty string (two double " with nothing between them)
        }
        
        char *a=(char*)malloc(2*sizeof(s));  // This line mallocs a new string every time, so you throw away the old ones.  This is a memory leak.
        // You also mallco the wrong size.  s is a char *, which always has the same size (probably 4 or 8 bytes).
        // I can't reliably repeat a string like "ABCDEFGHIJKLMNOPQRSTUVWXYZ" more than a few times.  Try it yourself.
        // Your malloc here needs to be based on strlen(a) from main (the length of the original string to repeat)
    
    
        strcat(a,s);
    
    
        char* p=Repeat(a,n-1);
        strcat(a,p);
        return a;
    }
    
    
    int main(void)  // Can't hurt to be explicit here.
    {
        char a[]="Hello";
        char *b;
        int n;
        printf("Enter the no of times u want to repeat\n");
        scanf("%d",&n);
        b= Repeat(a,n);
        printf("%s",b);
    
    
        getchar();
    
    
        return 0;  // Main is supposed to return an int, so make it do so.
    }
    There are copious other errors. Your code is still quite broken. I don't think you fully followed my advice in post #7. Did you work this out by hand and do several examples on paper?
    Last edited by anduril462; 07-28-2011 at 12:57 PM.

  3. #18
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    That is just part of my signature/tag line. It is not directed towards you.
    LOL... maybe you should make the above part of your signature...

  4. #19
    Registered User
    Join Date
    Jul 2011
    Posts
    19
    Quote Originally Posted by anduril462 View Post
    Nope, you don't got it:
    Code:
    #include<stdio.h>
    #include<conio.h> // I sincerely hope you're not still using Turbo C.  If so, time to upgrade.
    // Pelles C, Code::Blocks with MinGW or even MSVC++ Express are much better, free options
    #include<string.h>
    #include<stdlib.h>
    
    
    char* Repeat(char *s,int n)
    {
        if(n==0) {
            return " ";  // this is not the empty string, it's a string with one space.  "" is the empty string (two double " with nothing between them)
        }
        
        char *a=(char*)malloc(2*sizeof(s));  // This line mallocs a new string every time, so you throw away the old ones.  This is a memory leak.
        // You also mallco the wrong size.  s is a char *, which always has the same size (probably 4 or 8 bytes).
        // I can't reliably repeat a string like "ABCDEFGHIJKLMNOPQRSTUVWXYZ" more than a few times.  Try it yourself.
        // Your malloc here needs to be based on strlen(a) from main (the length of the original string to repeat)
    
    
        strcat(a,s);
    
    
        char* p=Repeat(a,n-1);
        strcat(a,p);
        return a;
    }
    
    
    int main(void)  // Can't hurt to be explicit here.
    {
        char a[]="Hello";
        char *b;
        int n;
        printf("Enter the no of times u want to repeat\n");
        scanf("%d",&n);
        b= Repeat(a,n);
        printf("%s",b);
    
    
        getchar();
    
    
        return 0;  // Main is supposed to return an int, so make it do so.
    }
    There are copious other errors. Your code is still quite broken. I don't think you fully followed my advice in post #7. Did you work this out by hand and do several examples on paper?
    i didnt get about conio.h ? whats wrong to use this ??
    yes i am using Pelle now...
    Yes i tried 1 or 2 ...should i try more on paper ?

    thanxs a lot for such a reply...really good for me...someone pointing out mistakes ...

  5. #20
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Right off... this is a bad choice for recursion. It would work far better as a loop....

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    char * repeat(char *str, int times)
      { char *ret = calloc(times, (strlen(str) + 1));
         while (times-- > 0)
           strcat(ret,str);
         return ret; }
    
    
    
     int main(void) 
    {  char a[]="Hello";
        char *b;
        int n;
        printf("Enter the no of times u want to repeat\n");
        scanf("%d",&n);
        b = Repeat(a,n);
        printf("%s",b);
        free (b);
        getchar();
        return 0; }
    Last edited by CommonTater; 07-28-2011 at 01:29 PM.

  6. #21
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    Quote Originally Posted by CommonTater View Post
    Right off... this is a bad choice for recursion. It would work far better as a loop....
    Perhaps you forgot:

    Quote Originally Posted by tarunjain07 View Post
    I am a beginner in c and trying my hand at recursion .
    It is a bad case for general use, but it's fine for practice, and will highlight some problems and issues that go along with dynamic memory allocation and recursion, as well as seeing just how slow lots of successive strcat calls can be. Neither of which the classic Fibonacci sequence example and the like will do.

  7. #22
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anduril462 View Post
    It is a bad case for general use, but it's fine for practice, and will highlight some problems and issues that go along with dynamic memory allocation and recursion, as well as seeing just how slow lots of successive strcat calls can be. Neither of which the classic Fibonacci sequence example and the like will do.
    Point taken .... We need to know what does not work as well as what does.

  8. #23
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    Quote Originally Posted by tarunjain07 View Post
    i didnt get about conio.h ? whats wrong to use this ??
    Nothing terribly wrong with it, though I don't see any conio.h specific functions, so you don't need it. Also, much of the time I see it, it's a sign people are using the undead Turbo C, which is a piece of crap.
    yes i am using Pelle now...
    Good, nothing to worry about.
    Yes i tried 1 or 2 ...should i try more on paper ?
    3-5 is probably better, but since you did try a few, I think your problem is not a full understanding of memory allocation or recursion. Here's some pseudo-code for what I would do:

    Code:
    // s is the string to repeat, n is how many times
    char *repeat(char *repeated, char *original, int n)
    {
        if n == 0
            base case, make an empty string
        
        strcat original onto repeated
        recurse with repeated, original and n-1
    }
    
    int main(void)
    {
        char original[80];  // let the user enter a string to repeat, this makes testing easier too
        char *repeated;  // to hold the allocated memory with repeated string
        int n;
    
        print "Enter a string to repeat"
        read original
        print "Enter how many times to repeat"
        read n
    
        repeated = malloc enough for n copies of original + 1 for the null terminator, you work out the math
        repeat(repeated, original, n);
    
        print "The repeated string is", repeated
    
        free(repeated);  // free up memory when you're done
    
        return 0;
    }
    Read it carefully and study it. There is a way to do this with the malloc call in repeat(), but it is a bit more complicated, so don't bother with that yet until you understand this one 100%.
    AndrewHunter likes this.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fread creates a string with crap at the end
    By kulfon in forum C Programming
    Replies: 10
    Last Post: 12-15-2010, 09:58 AM
  2. Repeat for part of string
    By bertazoid in forum C Programming
    Replies: 6
    Last Post: 10-26-2008, 05:06 PM
  3. Replies: 1
    Last Post: 10-31-2005, 10:36 AM
  4. Replies: 0
    Last Post: 04-05-2003, 08:33 AM
  5. Problem comparing string from text file with string constant
    By XenoCodex Admin in forum C++ Programming
    Replies: 3
    Last Post: 07-25-2002, 10:17 AM

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