Thread: paralel ascii change of a string..

  1. #1
    Banned
    Join Date
    Aug 2009
    Posts
    43

    paralel ascii change of a string..

    i need to write a function which inputs a string
    and removes all the spaces from the start and removes all the spaces from the end of the string.
    the string consists of words separated by spaces.
    if between two words we have more then one space ,it cuts down the excess spaces
    til its left with one space only between two words.
    and each char is flipped to its parallel ascii value "c" to "x" , "i" to "h" etc..
    for example:
    " C is wonderful "
    turn to
    "X hr ofuivwmld"

    i tried to solve it like this

    Code:
    char* AtBash(char* text)
    {
         int i=0,start,j=0,k;
         char* word,*str,c;
         str=text;
         while (i<strlen(text))
         {
             for(i=0;str[i]!=' ';i++);
             
                 if (i<strlen(text))
                 {
                      start=str;
                      for(j=0;((i<strlen(word)-1)&&str!=NULL;i++)
                      word[i-start]=text[i];
                      strcopy(word,word+1);
                      for (k=0;k<strlen(word);k++)
                      {
                           c=word[k];
                           if(c>'Z')
                            str[j]=c-26+'A';
                           else
                            str[j]=c-26+'a';
                            j++;
                      }
                       str[j]=' ';
                 }
              srt[j-1]='\0';
             return str;
    }
    Last edited by kakaroto; 08-20-2009 at 10:49 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    This is what vim thought your function looked like after auto-indenting:
    Code:
    char* AtBash(char* text)
    {
        int i=0,start,j=0,k;
        char* word,*str,c;
        str=text;
        while (i<strlen(text))
        {
            for(i=0;i<strlen(str);i++)            
            {                                                  
                if (i<strlen(text))
                    {
                    start=str;
                    for(j=0;((i<strlen(word)-1)&&str!=NULL;i++)
                            word[i-start]=text[i];
                            strcopy(word,word+1);
                            for (k=0;k<strlen(word);k++)
                            {                            
                            c=word[k];
                            if(c>'Z')
                            str[j]=c-26+'A';                                       
                            else
                            str[j]=c-26+'a';
                            j++;
                            }
                            str[j]=' ';
                            }
                            srt[j-1]='\0';
                            return str;
                            }
    I think it's still waiting for you to maybe finish off that (clause) for the first for-loop.

  3. #3
    Banned
    Join Date
    Aug 2009
    Posts
    43
    now i think its perfectly indented

    Code:
    char* AtBash(char* text)
    {
         int i=0,start,j=0,k;
         char* word,*str,c;
         str=text;
         while (i<strlen(text))
         {
             for(i=0;str[i]!=' ';i++);
             
                 if (i<strlen(text))
                 {
                      start=str;
                      for(j=0;((i<strlen(word)-1)&&str!=NULL;i++)
                      word[i-start]=text[i];
                      strcopy(word,word+1);
                      for (k=0;k<strlen(word);k++)
                      {
                           c=word[k];
                           if(c>'Z')
                            str[j]=c-26+'A';
                           else
                            str[j]=c-26+'a';
                            j++;
                      }
                       str[j]=' ';
            }
              srt[j-1]='\0';
             return str;
    }

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Slow down some. . . you don't have to do _EVERYTHING_ in the same function. Create a function that removes the spaces, test, then create a second function that does the string swap.

  5. #5
    Banned
    Join Date
    Aug 2009
    Posts
    43
    i am required to do it in one function
    where is the logical mistake?

  6. #6
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Then at least start by making the function ONLY remove spaces. Once that is hardened, do the next thing.

    Blowing 50 lines of code (when you are a beginner) is a real pain in the rear to debug. Take it slow. Make something work, then add to it.

  7. #7
    Banned
    Join Date
    Aug 2009
    Posts
    43
    i was told to solve it in this manner
    can you say where is the problem there
    ??

  8. #8
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Yes, your problem starts near the top of your function and lasts through about the last closing brace. That is where you need to start looking.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by kakaroto View Post
    i was told to solve it in this manner
    can you say where is the problem there
    ??
    There's a line without an error?
    1. word doesn't point to anywhere, so trying to read from it/write to it/use it in any way whatsover is horribly wrong.
    2. You're having trouble deciding whether start is a char * or an int.
    3. Although your first for-loop correctly does nothing with the leading spaces,
      1. that fact is not commented and
      2. your indentation does not match, as all the rest of the code appears to be "inside" that for-loop.
    4. Did you write strcopy? Even if you did, there's no reason for there to be anything in that spot that I can see, let alone copying strings from one place to another.
    5. Your "encoding" appears to have lower- and upper-case backwards, I think.
    6. Your "encoding" also turns letters into non-letters.
    7. Your "encoding" also does not do "mirroring" as specified in the problem.
    8. You don't appear to be dealing with multiple spaces between words (or, for that matter, dealing with single spaces between words, as they get "encoded" too).

  10. #10
    Banned
    Join Date
    Aug 2009
    Posts
    43
    i dont know how to do mirroring

    i know that if ch is 'a'
    and i want ch to be 'd'

    then ch=ch+3

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, for a minute I thought that you were supposed to do a<->z, b<->y, c<->x, etc. But that only worked once in your sample string. So, no, I don't know how you're supposed to do mirroring either.

  12. #12
    Banned
    Join Date
    Aug 2009
    Posts
    43
    how to do
    to do a<->z, b<->y, c<->x, etc. .

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by kakaroto View Post
    how to do
    to do a<->z, b<->y, c<->x, etc. .
    You need to figure out how many letters you are past a, and then back up that many letters from z. (And you should only do that if you are a letter, which is why there's islower() and isupper().)

  14. #14
    Banned
    Join Date
    Aug 2009
    Posts
    43
    can you see how to solve it with out the helping functions
    like i did it
    ?

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by kakaroto View Post
    can you see how to solve it with out the helping functions
    like i did it
    ?
    Yes. (Assuming by "helping" functions you mean islower and isupper. The reason I brought them in is that your testing for lower- or upper-case is also broken and will have to be thrown away and started over if you need to do it yourself.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. How properly inherit from template?
    By 6tr6tr in forum C++ Programming
    Replies: 118
    Last Post: 04-25-2008, 04:30 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM