[英]cramming for the exams - what am i missing (C++ string manipulation)
cramming for a c++ exam, on string manip with some example questions (to which i dont have solutions) - below is today's handiwork. 在字符串操作上进行C ++考试,并附带一些示例问题(我没有解决方案)-下面是当今的著作。 although it works fine - would be awesome if any obvious lapses / better way of doing things occur to you, just drop me a quick note, i need to learn me some C++ fast :)
虽然效果很好-如果您遇到任何明显的失误/更好的处理方法,那将非常棒,请给我一个快速注释,我需要快速学习一些C ++ :)
thanks! 谢谢!
#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);
}
As people have said, using std::string will make things much easier and safer. 正如人们所说的那样,使用std :: string将使事情变得更加容易和安全。 If you must use C-style strings, here is some critique of your code:
如果必须使用C风格的字符串,则对您的代码有一些批评:
1. In many places you have code equivalent to 1.在许多地方,您拥有与
buf[len+1] = '\0';
This should be 这应该是
buf[len] = '\0';
2. palindrome
function doesn't return a value. 2.
palindrome
函数不返回值。
3. This code (variations of which appear in several places) is not standard C++, since the array size is not constant: 3.由于数组大小不是恒定的,因此此代码(其变化在多个地方出现)不是标准的C ++:
char cinput[len+1];
For variable-sized arrays, you need to allocate them dynamically: 对于可变大小的数组,您需要动态分配它们:
char *cinput = new char[len+1];
//... use the array ...
delete[] cinput;
Of course, std::string
or even std::vector<char>
will make things easier here. 当然,
std::string
甚至std::vector<char>
在这里会使事情变得更容易。
Reverse a string: 反转字符串:
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string sampleString("sample string");
reverse(sampleString.begin(), sampleString.end());
cout << "reverse:" << sampleString << endl;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.