Thread: String?

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

    Question String?

    String1=zamv
    String2=lpwf

    The programm need to replace z with l, a with p...

    string1[i]=string2[i] but in String3 which u put from keyboard

    Example:
    String3: ovaa za
    after transformation
    String3: ofpp lp

    I wrote this code but id doesnt work when you put sentences seperated with space ..
    example:
    String3: aaa zz
    after transformation
    String3: ppp -only this

    Can anyone help me to simplify this code... ? Thanks

    insert
    Code:
    #include <stdio.h>
    #define max 100
    
    
    int main()
    
    
    {
        char niza1[]="zamv";
        char niza2[]="lpwf";
        char niza3[max];
        int i=0;
        int j=0;
    
    
        printf("Vnesi Niza 3: \n");
        scanf("%s", niza3);
    
    
        while( niza3[i] != '\0' )
        {
           if (niza1[j]!='\0')
           {
               if(niza3[i]==niza1[j])
               {
                   niza3[i]=niza2[j];
               }
                else if(niza3[i]==niza1[j+1])
               {
                   niza3[i]=niza2[j+1];
               }
               else if(niza3[i]==niza1[j+2])
               {
                   niza3[i]=niza2[j+2];
               }
               else if(niza3[i]==niza1[j+3])
               {
                   niza3[i]=niza2[j+3];
               }
               else i++;
    
    
           }
        }
    
    
        printf("%s", niza3);
    
    
    
    
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    use a nested loop, and possibly use for loops instead of while loops.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Jul 2014
    Posts
    25
    Quote Originally Posted by Elkvis View Post
    use a nested loop, and possibly use for loops instead of while loops.
    I cant solve it!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,863
    Quote Originally Posted by Ilir Selmani
    I wrote this code but id doesnt work when you put sentences seperated with space
    That is because you are using scanf with %s to read the input: this only reads a "word". One option is to use fgets instead to read the entire line (but note that fgets stores the newline in the string if there is space).

    Nonetheless, you should consider the use of nested loops: what if instead of "zamv" and "lpwf" you had "zamvwsxedc" and "lpwftyuion"?
    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

  5. #5
    Registered User
    Join Date
    Jul 2014
    Posts
    25
    Quote Originally Posted by laserlight View Post
    That is because you are using scanf with %s to read the input: this only reads a "word". One option is to use fgets instead to read the entire line (but note that fgets stores the newline in the string if there is space).

    Nonetheless, you should consider the use of nested loops: what if instead of "zamv" and "lpwf" you had "zamvwsxedc" and "lpwftyuion"?
    Thank you ^_^

  6. #6
    Registered User zub's Avatar
    Join Date
    May 2014
    Location
    Russia
    Posts
    104
    Our goals are clear, tasks are defined! Let's work, comrades! -- Nikita Khrushchev

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