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

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    19

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

    Hi eveyone

    Question is :-

    String repeat(String s, int n) that creates and returns a new string that is made by concatenating
    n copies of the parameter string s. For example, calling this method with the parameters “Hello”
    and 3 would return the string “HelloHelloHello”. If n equals zero, the method should return the
    empty string.

    I am a beginner in c and trying my hand at recursion .This code doesn't work.
    Please guide me and also tell any clever/advance/optimise steps that i can use to code as a pro.


    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    
    
    char* Repeat(char *s,int n)
    {
    char *c;
    char *e;
    	
      if(n==0)
     {
    	return " ";
    
     }
     
    if(n==1)
     {
     	c=strcpy(c,s);
      	return c;
     }
    
      if(n>1)
     {
    
      e=Repeat(s,n-1);
    
      e=strcat(e,s);
      return e;
     }
    
    }
    
    
    
    
    void 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);
       
        
        getch();
        
    }
    Last edited by tarunjain07; 07-28-2011 at 09:55 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why do you have c, and e just appearing like magic? You need some variables, and probably more importantly, you need some space to put these strings. Do you know malloc? Now might be a good time to learn.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    1) Go into your compiler settings and turn the warning levels all the way up.
    2) When you compile your code, treat each and every warning as an error to be corrected.

    You'll be surprised how smart your compiler is about helping you write decent code.

    For example... your variable e ... you just make that up, never declare it, don't initialize it and simply start using it... Your compiler should be complaining about that...

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    19
    Quote Originally Posted by tabstop View Post
    Why do you have c, and e just appearing like magic? You need some variables, and probably more importantly, you need some space to put these strings. Do you know malloc? Now might be a good time to learn.
    yes i know malloc
    but not getting the use here...
    could please edit the function ...i will try to learn/understand by that code...

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    19
    Quote Originally Posted by CommonTater View Post
    1) Go into your compiler settings and turn the warning levels all the way up.
    2) When you compile your code, treat each and every warning as an error to be corrected.

    You'll be surprised how smart your compiler is about helping you write decent code.

    For example... your variable e ... you just make that up, never declare it, don't initialize it and simply start using it... Your compiler should be complaining about that...
    I am using dev c++ can u tell me for that where and how to ?
    and also please tell some compiler for win 7

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by tarunjain07 View Post
    I am using dev c++ can u tell me for that where and how to ?
    and also please tell some compiler for win 7
    You really need to get a new compiler. Take a look at One of the many compiler threads. <------Click there
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Nope, we can not edit the function. You have to do the learning and the coding. We just give you a nudge in the right direction when you're stuck. Here are a few pointers for you:
    1. Work the problem out with paper and pencil first, and run through several examples by hand.
    2. Count the characters in "Hello" (remember the null), then count the characters in "HelloHelloHello" (remember the null again). Try some other tests. What is the relationship between their lengths? That will tell you how to take your original string, and n, and malloc the right number of bytes for your new string.
    3. Translate that into code, little by little, testing as you go. Perhaps start with an iterative (loop) solution, then make it recursive.
    4. If you're still stuck, come back and post what you tried.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tarunjain07 View Post
    I am using dev c++ can u tell me for that where and how to ?
    and also please tell some compiler for win 7
    First... if you want to know something about a specific IDE... that's what help files are for.

    Dev C++ has been abandoned for quite some time. It's compiler is very out of date and you should look to udating to something current. For C-99 I generally recommend Pelles C <--- click the link!
    Last edited by CommonTater; 07-28-2011 at 10:14 AM.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tarunjain07 View Post
    yes i know malloc
    but not getting the use here...
    could please edit the function ...i will try to learn/understand by that code...
    Before you even start trying to use malloc, you need to review your C textbooks/tutorials for the correct way to declare and use variables. You cannot simply make them up as you go!

  10. #10
    Registered User
    Join Date
    Jul 2011
    Posts
    19
    Quote Originally Posted by CommonTater View Post
    Before you even start trying to use malloc, you need to review your C textbooks/tutorials for the correct way to declare and use variables. You cannot simply make them up as you go!
    ok i will try and willl post back

  11. #11
    Registered User
    Join Date
    Jul 2011
    Posts
    19
    Quote Originally Posted by CommonTater View Post
    Before you even start trying to use malloc, you need to review your C textbooks/tutorials for the correct way to declare and use variables. You cannot simply make them up as you go!
    i know malloc ..as i have read my textbooks and topics like link list and tree etc..now as i felt that my programming is very weak..
    so i am working on this
    block by block ..started with recursion ..

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tarunjain07 View Post
    ok i will try and willl post back
    Looking at your edited code I see you are declaring your strings as char* ... nope, not going to work. A pointer is useless without space to store the data in...

    AND you are attempting to return a string from your function... Be very careful with this, it doesn't do what you seem to think it does...

    FWIW... please don't back edit like that... post a fresh copy, leave the original post alone. Others reading the thread will see the edited code and the conversation following won't make any sense to them at all.

    i know malloc ..as i have read my textbooks and topics like link list and tree etc..now as i felt that my programming is very weak..
    so i am working on this
    block by block ..started with recursion ..
    Simply reading the textbook isn't the way to learn C... Work through it page by page, do all the exercises and quizzes, play with the code (it's just as important that you learn what does not work), repeat as necessary, then move on to the next section. When simply reading most people's retention of information (called "comprehension") runs about 60%... if you take the time and work with the code as you learn you can probably drive that up to about 90%... There is a very real difference between "knowing" and "understanding"....
    Last edited by CommonTater; 07-28-2011 at 10:24 AM.

  13. #13
    Registered User
    Join Date
    Jul 2011
    Posts
    19
    Quote Originally Posted by CommonTater View Post
    Looking at your edited code I see you are declaring your strings as char* ... nope, not going to work. A pointer is useless without space to store the data in...

    AND you are attempting to return a string from your function... Be very careful with this, it doesn't do what you seem to think it does...

    FWIW... please don't back edit like that... post a fresh copy, leave the original post alone. Others reading the thread will see the edited code and the conversation following won't make any sense to them at all.
    ok..i got what u mean to say...i have to allocate memory by malloc and then return that to char * for storage...
    i know thats really foolish of me but i am new

  14. #14
    Registered User
    Join Date
    Jul 2011
    Posts
    19
    Quote Originally Posted by AndrewHunter View Post
    Real men never define acronyms; they understand them genetically - How to write unmaintainable code
    it redirects to a site..
    boss how to use this site as this has quotes and other stuff

  15. #15
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by tarunjain07 View Post
    it redirects to a site..
    boss how to use this site as this has quotes and other stuff
    That is just part of my signature/tag line. It is not directed towards you.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

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, 10: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, 11:36 AM
  4. Replies: 0
    Last Post: 04-05-2003, 09: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