为了考试而临时抱佛脚——我错过了什么(C+;+;字符串操作) 为C++考试填鸭,在字符串MANIP上有一些示例问题(我没有解决方案)-下面是今天的手工作业。虽然它工作良好-如果你有明显的失误/更好的做事方式,那就太棒了,请给我一个简短的提示,我需要学习一些C++快速:
谢谢为了考试而临时抱佛脚——我错过了什么(C+;+;字符串操作) 为C++考试填鸭,在字符串MANIP上有一些示例问题(我没有解决方案)-下面是今天的手工作业。虽然它工作良好-如果你有明显的失误/更好的做事方式,那就太棒了,请给我一个简短的提示,我需要学习一些C++快速:,c++,recursion,string,C++,Recursion,String,谢谢 #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
#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);
}
#包括
#包括
#包括
//#包括“words.h”
使用名称空间std;
无效反向(const char un_rev[],char rev[]);
void clean(const char in_string[],char out_string[]);
//生成字符串的“清理”副本并传递到递归比较
布尔比较(常量字符输入_1[],常量字符输入_2[]);
//递归位
bool compare_recur(字符输入_1[],字符输入_2[]);
布尔回文(常量字符输入[]);
//下面的func没有混合CAPP!
整数最小位置(整数起始位置,字符输入[]);
无效排序(字符输入[]);
布尔字谜(常量字符输入_1[],常量字符输入_2[]);
int main(){
/***问题1***/
字符反转[9];
反向(“lairepmi”,反向);
不能反转字符串:
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string sampleString("sample string");
reverse(sampleString.begin(), sampleString.end());
cout << "reverse:" << sampleString << endl;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符串示例字符串(“示例字符串”);
反向(sampleString.begin(),sampleString.end());
cout正如人们所说,使用std::string将使事情变得更加简单和安全。如果必须使用C风格的字符串,下面是对代码的一些评论:
1.在许多地方,您的代码相当于
buf[len+1] = '\0';
这应该是
buf[len] = '\0';
2.回文
函数不返回值
<P>3。由于数组大小不恒定:,该代码(出现在多个地方的变体)不是标准C++。
char cinput[len+1];
对于可变大小的阵列,您需要动态分配它们:
char *cinput = new char[len+1];
//... use the array ...
delete[] cinput;
当然,std::string
甚至是std::vector
都会让事情变得更简单。为什么不在C++中使用std::string而不是使用C样式的字符串呢?看起来这就是赋值,OP必须通过使用C样式的字符串来实现这个功能。你为什么要在clean函数中去掉数字呢?@sam they d尽管在这个问题上,理论上你可以使用Cstring,但大多数情况下,你是不允许的,所以我想我最好学习如何制作这些函数。@op你知道上面的许多函数声明是不安全的。你的函数没有办法知道目标array对于函数输出来说足够大。这就是为什么strcpy也应该避免的原因。