2014-02-16 3 views
-3

У меня есть строка «это тест». Я хочу изменить его на «test a is this». Мы возьмем одну строку как «Это тест». После изменения его строка должна быть «испытание на это»Как отменить слова в предложении без использования встроенной функции

#include <stdio.h> 

char *reverse(char *p); 
void main() { 
    char p[100] = "this is a test"; 
    char *s = reverse(p); 
    printf("%s", s); 
} 

выход - испытание на это.

+4

Прочитайте контрольный список вопросов о переполнении стека (http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist). Вы также можете узнать, что такое [SSCCE] (http://sscce.org/). –

+2

'void main'? а также почему тег C++? –

+0

@ Duck; К сожалению! Забыл об этом. – haccks

ответ

1

Я хотел бы сделать это следующим образом:

#include <boost/regex.hpp> 
#include <iostream> 
#include <string> 

using namespace std; 

int main(int argc, char*argv[]){ 
    string xStr; 
    cin >> xStr; 

    boost::regex xRegEx("(\\S+)"); 
    vector<string> words; 

    boost::sregex_iterator xIt(xStr.begin(), xStr.end(), xRegEx); 
    boost::sregex_iterator xInvalidIt; 
    while(xIt != xInvalidIt) 
     words.push_back(*xIt++); 

    for(std::vector<string>::iterator it = words.rbegin(); it != words.rend(); ++it) { 
     cout << *it << " "; 
    } 

    return 0; 
} 
+1

Использование функций повышения, а не встроенных, соответствует требованиям, которые я предполагаю. –

+0

Регулярные выражения? ** Whyyyyyyy? ** Почему люди всегда решают вещи с помощью регулярных выражений? Это пахнет как Perl ... – Manu343726

+0

Потому что мы можем :) –

1

Вы можете использовать следующий алгоритм:

  1. Разделить строку на слова в массив строк (скажем, А).
  2. Печать A в обратном порядке.
0

Одним из способов может быть, чтобы разделить предложение, используя stringstream, а затем просто скопировать слова в обратном порядке:

std::string get_reversed(const std::string& str) 
{ 
    std::stringstream input(ss); 
    std::vector<std::string> temp; 
    std::stringstream output; 

    std::copy(std::istream_iterator<std::string>(input , " ") ,   
       std::istream_iterator<std::string>() , 
       std::back_inserter(temp) 
      ); 

    std::reverse_copy(std::begin(temp) , 
         std::end(temp) , 
         std::ostream_iterator<std::string>(output , " ") 
        ); 

    return output.str();   
} 

Нет регулярных выражений, без наддува: Только стандартной библиотеки контейнеры и алгоритмы;)

1

Вот простую стратегию по изменению порядка слов в строке.

  • Реверс строки (предложение)
  • Реверс назад каждое слово в строке

Давайте разберем задачу на следующие функции:

int mystrlen(char *s); // find the length of the string 
char *rev_substr(char *s, int len); // reverse the substring s of length len 
char *rev_sentence(char *s); // reverse the order of words in the string s 

Вот определения функции :

int mystrlen(char *s) { 
    int i = 0; 
    while(*s) { 
     i++; 
     s++; 
    } 
    return i; 
} 

char *rev_substr(char *s, int len) { 
    int i = 0; 
    int j = len - 1; 
    char temp; 
    while(i < j) { 
     temp = s[i]; 
     s[i] = s[j]; 
     s[j] = temp; 
     i++; 
     j--; 
    } 
    return s; 
} 

char *rev_sentence(char *s) { 
    int i, j = 0; 
    int len = mystrlen(s); 
    rev_substr(s, len); // reverse the whole string 
    for(i = 0; i <= len; i++) { 
     // a word is delimited by a space or the null character 
     if(s[i] == ' ' || s[i] == '\0') { 
      rev_substr(s+j, i-j); // reverse back each word 
      j = i + 1; // j is the index of the first letter of the next word 
     } 
    } 
    return s; 
} 

Пример реализации:

int main(void) { 
    char s[] = "this is a test"; 
    printf("%s\n", rev_sentence(s)); 
    return 0; 
} 
0

Попробуйте это:

#include <iostream> 
#include <cstring> 
#include <cstdlib> 
using namespace std; 

char* reverseString(char* inStr) 
{ 
    int numChars = strlen(inStr);    // Length of input string 
    char* outStr = (char*)malloc(numChars+1); // Allocate 1 + string length so there is space to fit '\0' 
    char* wordStart = &inStr[numChars];  // Pointer to the start of a word. Initialized to '\0' character of the input 
    char* wordEnd = wordStart-1;    // Pointer to the end of word. Initialized to the last character of the input 
    char* destination = outStr;    // Pointer to the start of a new word in the reversed string 
    char* srcStart;       // Temporary pointer 
    char delimiter;       // Word delimiter set to '\0' when the first word of input is copied, set to ' ' for 
               // all other words in the input string 
    while (--wordStart >= inStr) 
    { 
     if (*wordStart == ' ') 
     { 
      srcStart = wordStart+1;   // start at the first letter following the space  
      delimiter = ' ';     // add a space at the end of the copied word 
     } 
     else if (wordStart == inStr) 
     { 
      srcStart = wordStart;    // start at the first letter of the input string 
      delimiter = '\0';     // add the null terminator to mark the end of the string 
     } 
     else 
     { 
      srcStart = NULL;     // not at word boundary, mark NULL so the logic below skips the copy operation 
     } 

     if (srcStart) 
     { 
      for (char* src = srcStart; src <= wordEnd; ++src, ++destination) 
      { 
       *destination = *src; 
      } 
      *destination = delimiter; 
      destination++; 
      wordEnd = wordStart-1; 
     } 
    } 

    return outStr; 
} 

int main(int argc, char** argv) 
{ 
    if (argc < 2) 
    { 
     cout << "Please provide an input string!..." << endl; 
     return -1; 
    } 
    cout << "Original String = " << argv[1] << endl 
     << "Reversed String = " << reverseString(argv[1]) << endl; 

    return 0; 
} 

Это делает понести затраты на создание новой строки, так что это не самый эффективный маршрут, но он получает работу.

+0

Кстати, еще одно ограничение в вышеупомянутой реализации заключается в том, что оно не обрабатывает периоды в конце предложение правильно. – DigitalEye

0

Программа, использующая рекурсивный вызов.

int count=0; 
void w_rev(char *p) 
{ 
    if((*p)!= '\0') 
    { 
     if((*p) !=' ') 
     { 
      w_rev(p+1); 
      printf("%c",*p); 
     } 
     count++; 
    } 
} 
void w_rev_call(char * p) 
{ 
    while(*(p+count) != '\0' ) 
     { 
      w_rev(p+count); 
      printf(" "); 
     } 
} 

Если arr это имя массива, то оператор w_rev_call(arr) даст желаемый результат.

Смежные вопросы