2009-12-28 4 views
0

Cram 시험을위한 cramming, 몇 가지 예제 질문이있는 문자열 조작 (솔루션이없는 경우) - 아래는 오늘날의 수작업입니다. 그것이 잘 작동하더라도 - 어떤 명백한 결실/일을하는 더 좋은 방법이 당신에게 발생하면 끝내 주겠다. 그냥 빠른 메모를 남겨주고, 나는 약간의 C++을 배울 필요가있다. :)시험을위한 크램 밍 - 내가 무엇을 놓치고 있습니까 (C++ 문자열 조작)

감사합니다!

#include <iostream> 
#include <cctype> 
#include <cstring> 

//#include "words.h" 

using namespace std; 

void reverse(const char un_rev[], char rev[]); 
void clean(const char in_string[], char out_string[]); 
//produces 'cleaned' copy of the string and passes on to recursive compare 
bool compare(const char input_1[], const char input_2[]); 
//the recursive bit 
bool compare_recur(char input_1[], char input_2[]); 
bool palindrome(const char input[]); 
//no mixed capps for func below! 
int min_pos(int starting_pos, char input[]); 
void sort(char input[]); 
bool anagram(const char input_1[], const char input_2[]); 



int main() { 

    /*** QUESTION 1 ***/ 
    char reversed[9]; 
    reverse("lairepmi", reversed); 
    cout << "'lairepmi' reversed is '" << reversed << "'" << endl; 
    reverse("desserts", reversed); 
    cout << "'desserts' reversed is '" << reversed << "'" << endl << endl; 


    /*** QUESTION 2 **/ 
    cout << "The strings 'this, and THAT......' and 'THIS and THAT!!!' are "; 
    if (!compare("this, and THAT......", "THIS and THAT!!!")) 
    cout << "NOT "; 
    cout << "the same" << endl << " (ignoring punctuation and case)" << endl; 

    cout << "The strings 'this, and THAT' and 'THIS, but not that' are "; 
    if (!compare("this, and THAT", "THIS, but not that")) 
    cout << "NOT "; 
    cout << "the same" << endl << " (ignoring punctuation and case)" << endl << endl; 

    /*** QUESTION 3 **/ 

    cout << "The string 'rotor' is "; 
    if (!palindrome("rotor")) 
    cout << "NOT "; 
    cout << "a palindrome." << endl; 

    cout << "The string 'Madam I'm adam' is "; 
    if (!palindrome("Madam I'm adam")) 
    cout << "NOT "; 
    cout << "a palindrome." << endl; 
    cout << "The string 'Madam I'm not adam' is "; 
    if (!palindrome("Madam I'm not adam")) 
    cout << "NOT "; 
    cout << "a palindrome." << endl << endl; 

    /*** QUESTION 4 **/ 

    cout << "The string 'I am a weakish speller!' is "; 
    if (!anagram("I am a weakish speller!", "William Shakespeare")) 
    cout << "NOT "; 
    cout << "an anagram of 'William Shakespeare'" << endl; 

    cout << "The string 'I am a good speller!' is "; 
    if (!anagram("I am a good speller!", "William Shakespeare")) 
    cout << "NOT "; 
    cout << "an anagram of 'William Shakespeare'" << endl; 

    return 0; 
} 



void reverse(const char* un_rev, char rev[]) { 

    int len = 0; 
    len = strlen(un_rev); 
    int i = 0; 

    rev[len+1] = '\0'; //null terminate string 

    while (len >= 0) { 
    rev[i] = un_rev[len -1]; 
    i++; 
    len--; 
    } 
} 

void clean(const char in_string[], char out_string[]) { 
    int n =0; 

    for (int i = 0; in_string[i]; i++) { 

    if (isalpha(in_string[i])) { 
     out_string[n] = toupper(in_string[i]); 
     n++; 
    } 
    } 
    out_string[n] = '\0'; 
    // cout << "out: " << out_string; 
} 


bool compare(const char input_1[], const char input_2[]) { 

    //cleaned copies of string 
    int len1 = strlen(input_1); 
    int len2 = strlen(input_2); 

    char cinput_1[len1+1]; 
    cinput_1[len1+1] = '\0'; 
    char cinput_2[len2+1]; 
    cinput_2[len2+1] = '\0'; 

    clean(input_1, cinput_1); 
    clean(input_2, cinput_2); 

    return(compare_recur(cinput_1, cinput_2)); 

} 

//recursive bit of the compare function 
//possibly work into a single func? 
bool compare_recur(char input_1[], char input_2[]) { 

    if (!(*input_1) || !(*input_2)) { 
    return true; 
    } else if (*input_1 != *input_2) { 
    return false; 
    } 

    return compare_recur(++input_1, ++input_2); 

} 


bool palindrome(const char input[]) { 

    int len = strlen(input); 

    char cinput[len+1]; 
    cinput[len+1] = '\0'; 

    reverse(input, cinput); 
    compare(input, cinput); 

} 


int min_pos(int starting_pos, char input[]) { 
    int min = starting_pos; 
    char min_char = input[starting_pos]; 

    for (int i = starting_pos; input[i]; i++) { 

    if (input[i] < min_char) { 
     min = i; 
     min_char = input[i]; 
    } 
    } 
    return min; 
} 


void sort(char input[]) { 
    char temp; 
    int the_min = 0; 

    for(int i = 0; input[i]; i++) { 
    the_min = min_pos(i, input); 

    if ((the_min) != i) { 
     //swap 
     temp = input[the_min]; 
     input[the_min] = input[i]; 
     input[i] = temp; 

    } 

    } 
} 


bool anagram(const char input_1[], const char input_2[]) { 

    int len1 = strlen(input_1); 
    int len2 = strlen(input_2); 

    char cinput_1[len1+1]; 
    cinput_1[len1+1] = '\0'; 
    char cinput_2[len2+1]; 
    cinput_2[len2+1] = '\0'; 

    clean(input_1, cinput_1); 
    clean(input_2, cinput_2); 

    sort(cinput_1); 
    sort(cinput_2); 

    return compare(cinput_1, cinput_2); 

} 
+0

왜 C++에서 C 스타일 문자열 대신 std :: string을 사용하지 마십시오 –

+1

이것은 C 스타일의 문자열을 사용하여 OP가이 기능을 구현해야하는 것처럼 보입니다. –

+0

왜 청소 기능에서 수치를 제거하고 있습니까? –

답변

0
문자열을 역

: 일이 훨씬 쉽고 안전하게 만들 것입니다 표준 : : 문자열을 사용하여

사람들이 말했듯이
#include <string> 
#include <iostream> 
#include <algorithm> 
using namespace std; 

int main() 
{ 
    string sampleString("sample string"); 
    reverse(sampleString.begin(), sampleString.end()); 
    cout << "reverse:" << sampleString << endl; 

} 
2

. 당신은 C 스타일의 문자열을 사용해야하는 경우, 여기에 몇 가지 코드의 비판이다 : 많은 장소에서


1.

buf[len+1] = '\0'; 

해당하는 코드를이해야한다는

buf[len] = '\0'; 

2. palindrome 함수가 값을 반환하지 않습니다.


3.이 코드 (변화의 여러 위치에 표시) 배열의 크기가 일정하지 않기 때문에, 표준 C++되지 않습니다 :

char cinput[len+1]; 

가변 크기 배열를 들어, 할당해야 동적으로 :

물론
char *cinput = new char[len+1]; 
//... use the array ... 
delete[] cinput; 

, std::string 또는 std::vector<char> 여기 일을 더 쉽게 만들 것입니다.