Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C programming guidance

Options
  • 04-01-2016 5:39pm
    #1
    Registered Users Posts: 14


    Hi all, was recommended by a friend to come on here to see if anyone could help or even point me in the right direction with a c programming problem I'm having. I had an assignment due in college today for 2 pm but was only able to get 1 of 2 programs wrote up. I can't seem to get my head round the 2nd one. It sounds very simple but trust me I've spent the bones of 30 hrs trying to get this over the xmas.
    What I have to do is take a string of letters and swap them with other letters to print out a statement.
    For example a = f
    B =v
    I'm not asking for someone to write the program for me but maybe shed a bit of light on it and maybe tell me which statement or back bone of the program to be.
    Thanks so much of any one could help
    Patrick


Comments

  • Registered Users Posts: 5,370 ✭✭✭DublinDilbert


    Your post seems to be in the wrong forum, so if a mod could move to the development forum.


    In C strings are stored as arrays of characters. You need to be able to index the Array to swap letters around.

    I've pasted some example code below. The first line creates an array with 20 elements, and assigns the string values into it. I just choose 20 as i was being lazy. The second line then takes one element and assigns its value into another element over-writing the value. The 3rd line just prints out the string.

    char test_str[20]="hello world\0";

    test_str[2]=test_str[1];

    printf("%s \n", test_str);


    The above program is simple, but it means that the value in temp_str[2] the 3rd element in the array is lost. So the easiest thing to do is use a temp variable:-

    char test_str[20]="123456789\0";
    char temp;

    temp = test_str[2];
    test_str[2]=test_str[1];
    test_str[1]=temp;

    printf("%s \n", test_str);


  • Registered Users Posts: 14 Pac8


    Thanks very much for the quick reply. If I have the letters I need to swap ie I have a group of letters already and I no what they have to change to. Could I just make a statement like
    Char secretText[] = " my text"
    Char a
    Char b
    Char c
    Etc...
    Then say... For a = "what ever letter in my text"
    For b = etc...
    Printf( %s, new secrtText)


  • Boards.ie Employee Posts: 12,597 ✭✭✭✭✭Boards.ie: Niamh
    Boards.ie Community Manager




  • Registered Users Posts: 1,275 ✭✭✭bpmurray


    Pac8 wrote: »
    Thanks very much for the quick reply. If I have the letters I need to swap ie I have a group of letters already and I no what they have to change to. Could I just make a statement like
    Char secretText[] = " my text"
    Char a
    Char b
    Char c
    Etc...
    Then say... For a = "what ever letter in my text"
    For b = etc...
    Printf( %s, new secrtText)

    Hmmm - are you talking about C or C++ or Python or what?

    For C, if you want to replace specific letters in a string with certain letters in a kind of simplistic password protection or similar, you can do it quite simply. You'll need 2 arrays for your replacement letters, one array for lowercase and one for uppercase. Then you replace each letter in your input string with the character in the appropriate array:
    // Replace the A-Z values below with what you wan as replacements
    char *LowerReplacements = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char *UpperReplacements = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
    void replaceString(char *inpStr)
    {
       char *pStr = inpStr;
       int   iX;
    
       while (*pStr) // Until we hit the NULL at the end of the string
       {
          if (*pStr >= 'A' && *pStr <= 'Z')
          {
              iX = *pStr - 'A';
              *pStr = UpperReplacements[iX];
          }
          else if (*pStr >= 'a' && *pStr <= 'z')
          {
              iX = *pStr - 'a';
              *pStr = LowerReplacements[iX];
          }
          pStr++;
       }
    }
    
    
    /* Test */
    char *myString = "This should work";
    replaceString(myString);
    printf("Changed string = '%s'\n", myString);
    


  • Registered Users Posts: 5,370 ✭✭✭DublinDilbert


    Pac8 wrote: »
    Thanks very much for the quick reply. If I have the letters I need to swap ie I have a group of letters already and I no what they have to change to. Could I just make a statement like
    Char secretText[] = " my text"
    Char a
    Char b
    Char c
    Etc...
    Then say... For a = "what ever letter in my text"
    For b = etc...
    Printf( %s, new secrtText)

    Sorry I'm not sure what you need to do. To access the letters/characters within the string you need to index the array using the [ ] brackets, with the number of the element you want between them.

    Have you looked up arrays and how to use them??

    So the first letter in the string can be accessed as mystrring[0]


  • Advertisement
Advertisement