Armenian Knowledge Base  

Go Back   Armenian Knowledge Base > Technical sections > Languages, Compilers, Interpreters > Algorithms
Register

Reply
 
LinkBack Thread Tools
Old 01.02.2014, 12:55   #1
Splendid
 
AvDav's Avatar
 
Join Date: 07 2004
Location: Universe
Age: 36
Posts: 3,431
Downloads: 22
Uploads: 0
Reputation: 222 | 3
Default Reversing words in a sentence (interview puzzle)

Задача о печати слов в предложении в обратном порядке. Вся идея заключается в том, чтобы свернуть строку а затем свернуть отдельные лексемы. Реализация на С++ представлена ниже.

Code:
#include <iostream>
    
void reverseString(char *b, char *e) {
    char tmp;
    while (b < e) {
        tmp = *b;
        *b = *e;
        *e = tmp;
        ++b;
        --e;
    }
}
     
void reverseWords(char* str) {
    char *front = str;
    char *back = str;
    while (*++back);
    --back;
     
    reverseString(front, back);
     
    while(*front) {
        for(; *front && *front == ' '; ++front);
        for(back = front; *back && *back != ' '; ++back);
        --back;
        reverseString(front, back);
        front = ++back;
    }
}
     
int main() {
    char s[] = {"The quick brown fox jumps over the lazy dog"};
    reverseWords(&s[0]);
    std::cout << s;
    return EXIT_SUCCESS;
}
Reply With Quote
Reply

Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
Yet another interview puzzle (anagrams) AvDav Algorithms 0 27.01.2014 20:17
Words arabaliozian Comments 0 23.06.2010 15:06
[JAVA GAME MOTOROLA VXX] Puzzle Bobble acid Mobile Phones and Gadgets 0 25.07.2005 21:16
Creating a story together: add one sentence! PsilocybeLarvae English Only 73 14.07.2005 16:47
ARMENIAN STUDENTS, WHO GAVE INTERVIEW TO NEW YORK TIMES, SAY THEIR WORDS WERE DISTORT acid News 19 17.12.2004 12:52


На правах рекламы:
реклама

All times are GMT. The time now is 06:18.


Powered by vBulletin® Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.