recursive function with strings

This is a discussion on recursive function with strings within the C Programming forums, part of the General Programming Boards category; I have to write this recursive function: write a recursive function called shift which takes a string and rotates each ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    63

    recursive function with strings

    I have to write this recursive function:
    write a recursive function called shift which takes a string and rotates each character cycle one position at the time.The problem is that i don't understand what the problem says. What should i do?
    i think that i should add +1 in each character so the character goes to the next ascii(example if we have abc now becomes bcd) but i am not sure what the problem ask?


    OR
    what about cycle arithmetic with characters and modulo(&#37 ,like in cryptography

    my code for the first thought
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define FIRST_ASC ' '
    #define LAST_ASC '-'
    
    void shift(char str[],int i);
    
    int main(void){
    
    int i=0;
    
    char str[]="hello world there";
    
    shift(str, i);
    
    printf("%s\n", str);
    
    char c;
    c='a';
    c=c+1;
    printf("%c\n", c);
    
    return 0;
    }
    
    
    void shift(char str[], int i){
    
    int p=i;
     if(str[p]=='\0') return;
    
    /*if((str[p]>= FIRST_ASC) && (str[p]<=LAST_ASC))  i didn't need this*/
      str[p]=(str[p] + 1);
    
    shift(str, ++p);
    }
    Last edited by alzar; 01-19-2008 at 10:20 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It looks like the function does what you want it to do; if you want to know what your instructor wants it to do, you should ask him/her. (In other words: I'm not sure either; maybe your instructor wants 'z' to cycle back to 'a'.)
    Last edited by tabstop; 01-19-2008 at 08:37 AM. Reason: good English

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    63
    I can't ask the instructor.So for this reason i am asking here if anyone understand something other? I was thought also something about cycle arithmetic with characters and the USE of modulo(&#37 like in cryptography.Do you understand me?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    I can't ask the instructor.
    You cannot clarify questions with your instructor?

    So for this reason i am asking here if anyone understand something other?
    As tabstop pointed out, there are at least two interpretations of the question:
    a) Rotate the characters with respect to their positions in the string.
    b) Rotate the value of the characters with respect to their positions in the alphabet.

    Additionally, the direction of the rotation is left unspecified (perhaps it does not matter).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    63
    Quote Originally Posted by laserlight View Post
    You cannot clarify questions with your instructor?


    As tabstop pointed out, there are at least two interpretations of the question:
    a) Rotate the characters with respect to their positions in the string.
    b) Rotate the value of the characters with respect to their positions in the alphabet.

    Additionally, the direction of the rotation is left unspecified (perhaps it does not matter).
    thanks.So we can't say that is something like this I said (modulo....).
    These a) and b) that you tell me i can make them.

    and i can't ask my "instructor" because i don't know him.This exercise i found it on the web from a university

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    So we can't say that is something like this I said (modulo....).
    You can use modulo to cycle through the alphabet and go from 'z' -> 'a', but it is probably easier just to check for 'z' and then assign 'a'.

    and i can't ask my "instructor" because i don't know him.This exercise i found it on the web from a university
    I suspect that your interpretation of the question is correct since "character cycle" seems to imply cycling through the alphabet. However, since this is not an actual assignment, you might as well attempt both interpretations.

    Likewise, you should feel free to make your own input/output requirements and experiment. There is no need to ask us to set the question for you, you can set it yourself and check with us to verify that your solution is a good one (or otherwise).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

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