top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Reverse the non numeric words in a string?

0 votes
308 views

Suppose you are given "Luv you 123 luv me" the expected output is "vuL uoy 123 vul em"

posted Jul 10, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
#include <stdio.h>
#include <string.h>

#define MAX_LEN 200

/* 
   This function will reverse the input string,
   i.e content of word.
*/

void reverse_word(char *word, int len)
{
    char *first, *last, temp;

    first = word;
    last = word + len - 1;

    while (last > first) {
        temp = *first;
        *first = *last;
        *last = temp;
        last--;
        first++;
    }
}

int main()
{
    char input_str[MAX_LEN]; //max of 200 char.
    char buff[MAX_LEN];
    char *ptr, *token, *delim = " ";
    int len;

    printf("Please enter the string which you want to convert :\n");

    fgets(input_str, 200, stdin);
    input_str[strlen(input_str) - 1] = '\0'; // for removing the new line (\n) character.

    memset(buff, 0 , 200);
    ptr = buff;

    // it will extract token from the input_str, for more info see "man 3 strtok"
    token = strtok(input_str, delim);    

    while (token != NULL) {

        len = strlen(token);
        if (!isdigit(*token)) {
            reverse_word(token, len); 
        }

        strncpy(ptr, token, len);
        ptr[len] = ' ';
        ptr = ptr + len + 1;

        token = strtok(NULL, delim);
    }

    printf("output : %s\n", buff);

    return 0;
}
answer Aug 21, 2014 by Arshad Khan
Similar Questions
+6 votes

Say you are given "Hello World"
Output should be "olleH dlroW"

+6 votes

Given an array of characters which form a sentence of words, give an efficient algorithm to reverse the order of the words (not characters) in it

+4 votes

Given a dictionary of strings and another string find out if the string is an exact match to some words in the dictionary or varies at most in only one place of some word of the dictionary?

+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?

...