Question

write a function in C that swaps every other byte: for example, Input: "badcfe” Output: abcdef...

write a function in C that swaps every other byte: for example,

Input: "badcfe”

Output: abcdef

I was thinking something like , where len would be two since I just want to swap values next to each other, but this doesnt work.

void

swap(char *p, int len) {

int i;

char tmp;

for(i = 0; i < len/2; i++) {

tmp = p[len-i-1];

p[len-i-1] = p[i];

p[i] = tmp;

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
void swap(char *p, int len) {
    int i;
    char tmp;
    for(i = 0; i < len && i+1 < len; i+=2) {
        tmp = p[i];
        p[i] = p[i+1];
        p[i+1] = tmp;
    }
}
Add a comment
Know the answer?
Add Answer to:
write a function in C that swaps every other byte: for example, Input: "badcfe” Output: abcdef...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a python function called "get_sma", that takes two input arguments, similar to the example provided....

    Write a python function called "get_sma", that takes two input arguments, similar to the example provided. First argument is the original prices of type list, and the 2nd input argument being the number of days we want to calculate SMA, like a 8 day sma or 35 day sma. This function should return a new list of sma price, based on the input day range, and should be able to handle any SMA calculation, like: sma200 for 200 days moving...

  • Using C: Implement the function ranges that takes no input and prints the ranges (i.e., the...

    Using C: Implement the function ranges that takes no input and prints the ranges (i.e., the minimum and maximum legal values) of the types char, short, int, long, and long long, both signed and unsigned. You should do this by printing appropriate constants defined in the header file /usr/include/limits.h (this is why hw1.c starts with #include <limits.h>). This should print something like: signed char minimum value: -128 maximum value: 127 unsigned char minimum value: 0 maximum value: 255 signed short...

  • "Controlling complexity is the essence of computer programming." Write a program that uses the C++ string...

    "Controlling complexity is the essence of computer programming." Write a program that uses the C++ string functionality to count the number of vowels in this phrase. Q7. Write a function bool xor(bool A, bool B) which returns the Exclusive OR of boolean input variables A and B. Test your function by printing out the full truth table of the function. Q8. Write a method swap(char_all, int i, int i) which swaps element i and element j of a given character...

  • This assignment tests your ability to write C programs that handle keyboard input, formatted console output,...

    This assignment tests your ability to write C programs that handle keyboard input, formatted console output, and input/output with text files. The program also requires that you use ctype functions to deal with the logic. In this program, you will input from the user two characters, which should both be letters where the second letter > the first letter. You will then input a file character-by-character and output those letters that fall between the two characters (inclusively) to an output...

  • I NEED HELP WITH DEBUGGING A C PROGRAM! PLEASE HEAR ME OUT AND READ THIS. I...

    I NEED HELP WITH DEBUGGING A C PROGRAM! PLEASE HEAR ME OUT AND READ THIS. I just have to explain a lot so you understand how the program should work. In C programming, write a simple program to take a text file as input and encrypt/decrypt it by reading the text bit by bit, and swap the bits if it is specified by the first line of the text file to do so (will explain below, and please let me...

  • write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....

    write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below. int pow_xy(int *xptr, int y); Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow. For the remaining problems,...

  • Language C++ (Please include a short description & Screenshot of output) Implement a Priority...

    Language C++ (Please include a short description & Screenshot of output) Implement a Priority queue using a SORTED list. Use Quick sort after adding a new node. Example of quick sort below. Adopt to your program the code below. #include <iostream> void quickSort(int a[ ], int first, int last); int pivot(int a[], int first, int last); void swap(int& a, int& b); void swapNoTemp(int& a, int& b); void print(int array[], const int& N); using namespace std; int main() { int test[]...

  • HELP NEEDED in C++ (preferred) or any other language Write a simple program where you create...

    HELP NEEDED in C++ (preferred) or any other language Write a simple program where you create an array of single byte characters. Make the array 100 bytes long. In C this would be an array of char. Use pointers and casting to put INTEGER (4 byte) and CHARACTER (1 byte) data into the array and pull it out. Make sure you can access both character and integer data at any location in the array. Read data from a text file....

  • This is my assignment prompt This is an example of the input and output This is...

    This is my assignment prompt This is an example of the input and output This is what I have so far What is the 3rd ToDo in the main.cpp for open the files and read the encrypted message? Does the rest of the code look accurate? Programming Assignment #6 Help Me Find The Secret Message Description: This assignment will require that you read in an encrypted message from a file, decode the message, and then output the message to a...

  • I need little help with C language. I need to pass what I get from HexToBin(char*...

    I need little help with C language. I need to pass what I get from HexToBin(char* hexdec) into char* input so that what I got there it should pass it as string array parametr. Example: Enter IEEE-Hex: 40200000 Equivalent Binary value is : 01000000001000000000000000000000 Decimal Number: 2.5 #include <stdio.h> void HexToBin(char* hexdec) {    long int i = 0;    while (hexdec[i]) {        switch (hexdec[i]) {        case '0':            printf("0000");            break;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT