Thread: String?

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    25

    Exclamation String?

    How to replace string in C.. i need to replace
    'a' --> 'z'
    'b' --> 'y' ... first letter with last, second...

  2. #2
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Are you saying you need to reverse the string? Your explanation was very terse.

    You might be able to reverse it using the general swap technique:

    Code:
        char String[] = "Ride The Snake!";
    
        int i = 0;
    
        int range = strlen(String) - 1;
    
        for(int j = range; i < (range/2), j > (range/2); i++, j--)
        {
            if(String[i] == NULL || String[j] == NULL)
            {
                break;
            }
            else
            {
                char temp = String[j];
    
                String[j] = String[i];
    
                String[i] = temp;
            }
        }
    
        printf("%s\n", String);
    Edit:

    The swap technique basically goes like this. You take an intermediate variable of the correct type (temp), then assign it with one of the variables needing swapping ( String [j] ). Once this is done, you are free to assign values straight to the target variables.

    If you didn't use an intermediate to save it, you would be forced to overwrite data that you need:

    Code:
    String [j] = String [I];
    
    String [I] = String [j];
    See, in the second step you are just assigning whatever I is to itself, because you've overwritten whatever was there.
    Last edited by Alpo; 07-21-2014 at 10:10 PM.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    @alpo - compiler warnings?

  4. #4
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by gemera View Post
    @alpo - compiler warnings?
    Whoops, meant to write '\0'. Guess I don't haz the brain today lol.

    I didn't actually write that on the spot, I was just remembering it from a few months ago. I'll fix it.

    Edit: can't edit the original for some reason

    Code:
    #include <stdio.h>
    #include <string.h>
    int main(void)
    {
        char String[] = "Ride The Snake!";
        int i = 0;
        int range = strlen(String) - 1;
        int j = range;
        for(; i < (range/2) || j > (range/2); i++, j--)
        {
            if(String[i] == '\0' || String[j] == '\0')
            {
                break;
            }
            else
            {
                char temp = String[j];
                String[j] = String[i];
                String[i] = temp;
            }
        }
        printf("%s\n", String);
        return 0;
    }
    Last edited by Alpo; 07-22-2014 at 12:22 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Dunno, this could well be an exercise in implementing a simple substitution cipher.

    Alpo: great that you want to give implementing a string reversal program a try, but please do not spoonfeed. Besides, your loop looks too complicated: why check for a null character when you know that there won't be any? The range / 2 checks seem unnecessary too: you have two loop counters, so it is enough to terminate the loop when their values cross. If you were only using one loop counter then checking for half the range would make sense.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jul 2014
    Posts
    25
    Quote Originally Posted by Alpo View Post
    Are you saying you need to reverse the string? Your explanation was very terse.

    You might be able to reverse it using the general swap technique:

    Code:
    
    
        char String[] = "Ride The Snake!";
    
        int i = 0;
    
        int range = strlen(String) - 1;
    
        for(int j = range; i < (range/2), j > (range/2); i++, j--)
        {
            if(String[i] == NULL || String[j] == NULL)
            {
                break;
            }
            else
            {
                char temp = String[j];
    
                String[j] = String[i];
    
                String[i] = temp;
            }
        }
    
        printf("%s\n", String);
    Edit:

    The swap technique basically goes like this. You take an intermediate variable of the correct type (temp), then assign it with one of the variables needing swapping ( String [j] ). Once this is done, you are free to assign values straight to the target variables.

    If you didn't use an intermediate to save it, you would be forced to overwrite data that you need:

    Code:
    String [j] = String [I];
    
    String [I] = String [j];
    See, in the second step you are just assigning whatever I is to itself, because you've overwritten whatever was there.
    Example:
    char string[]=”ispit pia”; func (string);
    after transformation: string[]=”rhkrg hrz”

    I need to replace char--- 'a' with 'z'
    'b' with 'y' ... first letter with last, second -- befor last...

    i with r.. clear ?
    Last edited by Ilir Selmani; 07-22-2014 at 07:13 AM.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Your requirements are clear. What have you tried to solve this problem? Do you have any code written? Or at least some ideas on how to try solving it?

  8. #8
    Registered User
    Join Date
    Jul 2014
    Posts
    25
    Quote Originally Posted by Matticus View Post
    Your requirements are clear. What have you tried to solve this problem? Do you have any code written? Or at least some ideas on how to try solving it?
    I need some ideas ?

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Ilir Selmani View Post
    I need some ideas ?
    There's a lot more to programming than just writing code. Problem solving is an important skill to develop. That includes coming up with ideas on how to solve your problem. Sure we could give you some ideas, but that would rob you of a valuable learning experience.

    So why don't you think about it for a bit and see what you come up with? Maybe sitting down with a pen and paper would help you visualize the problem and see potential solutions.

  10. #10
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Your instructor should have given you enough information especially in the last class to provide the solution. Did the instructor talk at all about using an array as a lookup table?

  11. #11
    Registered User
    Join Date
    Jul 2014
    Posts
    25
    I wrote this, idk that part of replacing.. with for or ?

    insert
    Code:
    #include <stdio.h>
    #define MAX 50
    void changeString(char *str);
    int main ()
    {
    char s[MAX];
     printf("Write string:\n");
     gets(s);
     changeString(s);
     printf("New string:\n");
     puts(s);
    return 0;
    }
    void changeString(char *str)
    {
    int i = 0;
     while (str[i] != '\0')
    {
        if(str[i]=='a')
        {
            str[i]='z';
        }
        if(str[i]=='b')
        {
            str[i]='y';
        }
        if(str[i]=='c')
        {
            str[i]='x';
        }
    
    
        i++;
    }
    str[i]='\0';
    }

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    While your idea seems technically valid at a glance, it is not very elegant. If that's sufficient to solve the problem, you could go with it, but rstanley gave a great hint that would make lead to much more concise code.

    The "while()" loop would work as is, but a "for()" loop is the more natural selection when you are updating a variable each iteration of the loop.

    Also, see here: FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com

  13. #13
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by laserlight View Post
    Dunno, this could well be an exercise in implementing a simple substitution cipher.

    Alpo: great that you want to give implementing a string reversal program a try, but please do not spoonfeed. Besides, your loop looks too complicated: why check for a null character when you know that there won't be any? The range / 2 checks seem unnecessary too: you have two loop counters, so it is enough to terminate the loop when their values cross. If you were only using one loop counter then checking for half the range would make sense.
    Sorry about the spoonfeeding. Thanks for the ideas as well, I didn't think about checking if they had crossed. I'll be thinking about how to do it with one loop counter .

  14. #14
    Registered User zub's Avatar
    Join Date
    May 2014
    Location
    Russia
    Posts
    104
    Code:
    #include <stdio.h>
    
    
    char* cipher_string(char* const str)
    {
        char* s = str;
        int c;
        while( c = (unsigned char) *s ) {
            if( c > 64 && c < 91 ) {
                *s = 155 - c;
            } else if( c > 96 && c < 123 ) {
                *s = 219 - c;
            }
            ++s;
        }
        return str;
    }
    
    
    int main(void)
    {
        puts(cipher_string("I say: \"Abracadabra\"!!!"));
        return 0;
    }

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by zub View Post
    Code:
    #include <stdio.h>
    
    
    char* cipher_string(char* const str)
    {
        char* s = str;
        int c;
        while( c = (unsigned char) *s ) {
            if( c > 64 && c < 91 ) {
                *s = 155 - c;
            } else if( c > 96 && c < 123 ) {
                *s = 219 - c;
            }
            ++s;
        }
        return str;
    }
    
    
    int main(void)
    {
        puts(cipher_string("I say: \"Abracadabra\"!!!"));
        return 0;
    }
    No need for the nasty use of magic numbers there:
    Code:
            if( c >= 'A' && c <= 'Z' ) {
                *s = 'A' + 'Z' - c;
            } else if( c >= 'a' && c <= 'z') {
                *s = 'a' + 'z' - c;
            }
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 04-27-2013, 06:36 AM
  2. Replies: 1
    Last Post: 04-27-2013, 04:36 AM
  3. Replies: 22
    Last Post: 07-28-2011, 01:26 PM
  4. Replies: 7
    Last Post: 06-16-2011, 06:21 PM
  5. Replies: 1
    Last Post: 10-31-2005, 11:36 AM