gpt4 book ai didi

c++ - 为考试而死记硬背——我错过了什么(C++ 字符串操作)

转载 作者:行者123 更新时间:2023-11-28 03:58:47 25 4
gpt4 key购买 nike

用一些示例问题(我没有解决方案)在字符串操作上为 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);

}

最佳答案

正如人们所说,使用 std::string 将使事情变得更加简单和安全。如果您必须使用 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>将使这里的事情变得更容易。

关于c++ - 为考试而死记硬背——我错过了什么(C++ 字符串操作),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1970302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com