views:

90

answers:

2

I have a task to implement "void makeAmbigram(char*)" that will print on screen ambigram of latin string or return something like 'ambigram not possible'. Guess it's just about checking if string contains only of SNOXZHI and printing string backwards. Or am I wrong ?

I'm a complete noob when dealing with cpp so that's what I've created :

#include <iostream>
using namespace std;

char[]words;
char[]reversed;

char[] ret_str(char* s)
{
    if(*s != '\0')
         ret_str(s+1);

    return s;
}

void makeAmbigram(char* c)
{
 /* finding chars XIHNOZS and printing ambigram */ 
}

int main()
{
   cin>>words;
   reversed = ret_str(words);
   makeAmbigram(reversed);
   return 0;
}

I can reverse string but how to check if my reversed string contains only needed chars ? I've found some function but it's hard or even imposible to implement it for greater amount of chars : www.java2s.com/Code/C/String/Findcharacterinstringhowtousestrchr.htm

+2  A: 

You need to allocate space in your arrays or use std::vector. The arrays word and reversed are just pointers and no space is allocated. The C++ language does not support dynamic arrays; however, the STL provides std::vector which dynamically allocates space as required.

Change:

char[]words;
char[]reversed;

To:

#define MAX_LETTERS 64
char words[MAX_LETTERS + 1]; // + 1 for terminating nul character ('\0')
char reversed[MAX_LETTERS + 1];

Or:

#include <string>
std::string words;
std::string reversed;

Or:

#include <vector>
std::vector<char> words;
std::vector<char> reversed;

As far as the ambigram rules go, you need to talk to your instructor. Also, if this is homework, add a tag indicating so.

Hint: The std::string data type has some reverse iterators which may be of use to you.

Thomas Matthews
A: 

std::string has an entire family of member functions along the lines of find_first_of. You can pass in a string containing all the letters your ambigram test requires, and they'll find whether any of those letters are present in the source string.

The complete list of string functions is available here.

Kristo