views:

238

answers:

2

cramming for a c++ exam, on string manip with some example questions (to which i dont have solutions) - below is today's handiwork. 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 :)

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);  

}
A: 

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;

}
DaMacc
+2  A: 

As people have said, using std::string will make things much easier and safer. If you must use C-style strings, here is some critique of your code:


1. In many places you have code equivalent to

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

This should be

buf[len] = '\0';

2. palindrome function doesn't return a value.


3. This code (variations of which appear in several places) is not standard C++, since the array size is not constant:

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.

interjay