Question

In: Computer Science

write a simple c program to reverse pairs of characters in a string. For exaple, "notified"...

write a simple c program to reverse pairs of characters in a string. For exaple, "notified" becomes "edfitino". The reversal should be in this order in a simple easy to understand c program.

Solutions

Expert Solution

Hello the string reversal is done in 2 categories

1)when the string length is odd

-when it is odd,reversal is done from last index to 1st index and the 0th index is simply added to the last

2)when the string length is even

-when it is even the reversal is done in normal fashion itself

The input_string is the original string and ouput_string is the reversal string

//stdio is for standard input and output
#include <stdio.h>
//string is used for calculating length of the string
#include <string.h>
int main(int argc, char *argv[])
{
   char input_string[50]={'\0'},output_string[50]={'\0'};
   int i,length_of_string=0,j=0,count=0;

   //read the string
   printf("Enter the String which you want to reverse:");
   scanf("%s",input_string);

   //calculate the length of the string by using strlen() function
   length_of_string=strlen(input_string);
   for(i=length_of_string-1;i>=0;i--)
   {

       //when string length is even
       if(length_of_string%2==0)
       {

           //add last 2 charcters of the input to the first 2 characters and so on
           if((i+2)%2==0)
           {
               output_string[j++]=input_string[i];
               output_string[j++]=input_string[i+1];
           }
           else
           {
               continue;
           }
       }

       //when string length is odd
       else
       {

           //add first character at the last index
           if(i==0)
           {
               output_string[j]=input_string[i];
           }
           else if((i+2)%2==0)
           {
               continue;
           }

           //add last 2 charcters of the input to the first 2 characters and so on
           else
           {
               output_string[j++]=input_string[i];
               output_string[j++]=input_string[i+1];
           }
       }
   }
   printf("%s",output_string);
   return 0;
}

Thank you.....


Related Solutions

Write a C program with a struct that contains a string of 12 characters and two...
Write a C program with a struct that contains a string of 12 characters and two integers: number1 and number2. Read all 3 values in from the keyboard using scanf. Print the string and the sum of the two integers using the fully qualified struct names.
In c++, using stack structure, write a program that will take a sequence of characters (string)...
In c++, using stack structure, write a program that will take a sequence of characters (string) and determine whether it is a palindrome. Use the linked version of the stack.
SOLVE IN C: 6.31 LAB: Print string in reverse Write a program that takes in a...
SOLVE IN C: 6.31 LAB: Print string in reverse Write a program that takes in a line of text as input, and outputs that line of text in reverse. You may assume that each line of text will not exceed 50 characters.The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text. Ex: If the input is: Hello there Hey quit then the output is: ereht olleH yeH Hint: Use the fgets function to...
Write a C++ program which reads a string, less than 10 characters long. This string represents...
Write a C++ program which reads a string, less than 10 characters long. This string represents an integer expressed in roman numbers. Let a function convert the number from roman to arabic form (i.e., our standard digits). Let then the main program writes out both forms. The roman numbers are written according to: M = 1000, D = 500, C =100, L=50, X=10, V=5, I=1. Examples: LXXXVII = 87 CCXIX = 219 MCCCLIV = 1354 MMDCLXXIII = 2673
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a...
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a list that only contains the duplicate characters. In other words, your new list should contain the characters which appear more than once. Suggested Approach Used two nested for loops. The first for loop iterates from 0 to range(len(input_str)). The second for loop iterates from first_loop_index + 1 to range(len(input_str)). The reason you want to start at first_loop_index + 1 in the nested inner loop...
Write a program that prints out the characters of a string each on a line and...
Write a program that prints out the characters of a string each on a line and counts these characters.  (Do not use the length function len() ).
write this program in C++ Write a program that prompts a user for three characters. The...
write this program in C++ Write a program that prompts a user for three characters. The program must make sure that the input is a number 10 - 100 inclusive. The program must re prompt the user until a correct input is entered. Finally output the largest and the lowest value. Example 1: Input : 10 Input : 20 Input : 30 The largest is 30. The lowest is 10. Example 2: Input : 100 Input : 50 Input :...
Write a program that uses a for loop output a string in reverse printing it character...
Write a program that uses a for loop output a string in reverse printing it character by character. C++ Program Example: "Enter some text:" this is a test "your text in reverse is:" tset a si siht Please note that you can find the length of the string by using the length function: string str = "hello" cout << str.length(); Also you can et a character in a string by providing an index to the function string str = "hello"...
Write a java program with 3 recursive methods that reverse the order of a string. The...
Write a java program with 3 recursive methods that reverse the order of a string. The first recursive method should reverse the order starting from the left selecting the leftmost character as the head and the remaining part of the string as its tail., the second starting from the right selecting the rightmost character as the head and the remaining part of the string on its left as the tail, and the last a recursive method starting from the middle...
Before C++ and classes, strings were stored in simple arrays of characters. The term C-string refers...
Before C++ and classes, strings were stored in simple arrays of characters. The term C-string refers to the classic implementation of strings in the C programming language. A C-string is a sequence of characters terminated by the null character, '\0'. Since C is part of C++, C-string implementations are valid in C++ as well as C. Recall that an array of characters is the underlying data structure for storing C-strings. For example, this definition creates such an array. char myString[100];...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT