top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Swap 2 characters in a sentence which is adjacent to white space

+4 votes
1,730 views

It should be work for Dynamic input

For Example
Input: Hello Good to See You
Output: HellG ooot dS oeY eou

posted Aug 5, 2014 by Avinash Ks

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
Nice Question.... (y) (y)

2 Answers

+2 votes

Check the following Code

#include <stdio.h>
#include <ctype.h>

static inline void swap(char *a, char *b){
        char ch = *a;
        *a = *b;
        *b = ch;
}

char *wordTop(char *s){
        while(*s && isspace(*s))
                ++s;
        return *s ? s : NULL;
}

char *wordTail(char *s){
        char *p;
        do{
                p = s++;
        }while(*s && !isspace(*s));
        return p;
}

int main(){
        char *input;
        char *top, *tail;

        input = malloc(255);

        printf("Enter the Input\n");
        gets(input);
        top = input;
        for(;;){
                tail = wordTail(wordTop(top));
                if((top = wordTop(tail+1)))
                        swap(top, tail);
                else
                        break;
        }
        printf("\n%s\n", input);
}

Input:
My Name is Pardeep

Output:
MN yami eP sardeep

answer Aug 5, 2014 by Pardeep Kohli
I am giving solution in the Java.Please look....
public class swap {

    public static void main(String[] args) {
        String input = "Hello Good to See You";
        char chracters[] = new char[]{};
        if (input != null && !input.isEmpty()) {
            chracters = input.toCharArray();
            for (int i = 0; i < chracters.length; i++) {
                if (i > 0 && chracters[i] == ' ') {
                    char tempChar = chracters[i - 1];
                    chracters[i - 1] = chracters[i + 1];
                    chracters[i + 1] = tempChar;
                }
            }
        }
       
        for (int i = 0; i < chracters.length; i++) {
            System.out.print(chracters[i]);
        }   
    }

}

input = "Hello Good to See You"
output = HellG ooot dS oeY eou
is this possible to without using pointers.can give me solution without using pointers.
+2 votes
#include<stdio.h>
void swap(char *str)
{
    char temp;
    char *p, *q;
    p=str;
    while(*p)
    {
          if( *p != ' ' && *(p+1) == ' ')
          {
             q = p;
          }  
          if(*(p-1) == ' ' && *p != ' ')
          {
               temp = *q;
               *q = *p;
                *p = temp;
          } 
         p++;
    }
}

int main(void)
{
     char string[255];
     printf("Enter the String \n");
     gets(string);
     swap(string);
     printf("output string :\n");
     printf("%s\n", string);
     return 0;
}
answer Aug 5, 2014 by Prakash Singh
is this possible to without using pointers.can give me solution without using pointers.
Yes. It is possible, we have to do small modifications inside swap functions.

void swap(char *str)
{
   char temp; int i,j;
    i=0;
    while(str[i])
    {
          if(str[i] != ' ' && str[i+1] ==' ')
                   j =i;
          if(str[i-1] == ' ' && str[i] != ' ')
           {
                    temp = str[j];
                    str[j] = str[i];
                    str[i] = temp;
           }
            i++;
     }
}

please check this one.
Similar Questions
+2 votes

Write a C program which accept two strings and print characters in second string which are not present in first string?

Example:
String 1: apple
String 2: aeroplane

output:
ron

+1 vote

char *p="India";
char t[10]="USA";

after the swap t should contain India and p as USA.

+2 votes

Given a string and two words which are present in the string, find the minimum distance between the words

Example:
"the brown quick frog quick the", "the" "quick"
Min Distance: 1

"the quick the brown quick brown the frog", "the" "brown"
Min Distance: 2

C/Java code would be helpful?

...