0

I am trying to convert a string, for example, "how are you doing?" into an array without any comma space or any punctuation, which is like {'h','o','w','a','r','e','y','o','u','d','o','i','n','g'} This is the function I wrote below, but it does not work.

#include <iostream>
#include <cstring>
#include <string>
char split(string str){
        char array[80];
        for (int i=0; i<str.length();i++){
                if (str[i]==","||str[i]=="."||str[i]==" ")
                        delete str[i];
                else
                        array[i]=str.substr(i,1);
        }
        return (array[80]);
}
9
  • 1
    Expand on "does not work". Commented Mar 20, 2014 at 22:15
  • 4
    A string already is an array of characters. Commented Mar 20, 2014 at 22:17
  • 2
    Main error is "" around "," etc. and use str[i] instead of str.substr(i,i+1) to get a char. Commented Mar 20, 2014 at 22:19
  • 3
    @user783920, I don't think there really is one main error. I count around 10 separate ones. Commented Mar 20, 2014 at 22:19
  • 1
    you're returning array[80], which is just one char. is that what you want? maybe return array. Commented Mar 20, 2014 at 22:20

4 Answers 4

1

First, some notes on your code:

  1. <cstring> (i.e. <string.h>) is useless, since you don't use anything from the <cstring> header in your code.

  2. Since string is an input read-only parameter, you can pass it by const &.

  3. Since you want an array of chars, you can either return a vector<char> (or even a string...).

To solve your problem, you can simply iterate through the characters of the input string, and if they are alphabetical and numeric characters (i.e. no space, no comma, etc.), you can add them to the output result vector of chars.

You may want to consider the following sample commented code:

#include <ctype.h>      // For isalnum()
#include <iostream>     // For console output
#include <string>       // For std::string
#include <vector>       // For std::vector
using namespace std;

vector<char> split(const string& str)
{
    vector<char> result;

    // For each character in the string
    for (char ch : str)
    {
        // Copy only alphabetical characters and numeric digits
        if (isalnum(ch))
        {
            result.push_back(ch);
        }
    }

    return result;
}

int main()
{
    vector<char> result = split("How are you doing?");
    cout << "{ ";
    for (char ch : result)
    {
        cout << "'" << ch << "' "; 
    }
    cout << "}" << endl;
}

Output:

{ 'H' 'o' 'w' 'a' 'r' 'e' 'y' 'o' 'u' 'd' 'o' 'i' 'n' 'g' }

If you like a more "functional" style, you can use the std::copy_if() algorithm, with some code like this:

#include <algorithm> // For std::copy_if()
#include <iterator>  // For std::back_inserter

....
const string str = "How are you doing?";
vector<char> result;
copy_if(
    str.begin(), str.end(),                // copy source
    back_inserter(result),                 // copy destination
    [](char ch) { return isalnum(ch); }    // when to copy the character
);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! It is very helpful, but the problem is that I did not get that far yet in my class. I think the professor of my class only allows us to use <string> <iostream> and some simple libraries. But I will definitely write this one down for my future study!
0

You are reinventing the wheel

std::string has methods to help you

for example

size_t found = str.find_last_of(".");
str.replace(found,1," ");

2 Comments

Looks more like a simple copy_if.
Yes, a copy_if is just around the corner :D
0

You can also use simple iteration using char* to go through the string and pick out what you need.

I am not sure what's the reason for returning array[80] frm split but that is not relevant to your question.

#include <iostream>
#include <string>
#include <ctype.h>

char split(std::string str)
{
   char array[80];
   char const* cp = str.c_str();
   int i = 0;
   for (; *cp; ++cp )
   {
      if ( isalnum(*cp) )
      {
         array[i++] = *cp;
      }
   }
   array[i] = '\0';

   cp = array;
   for (; *cp; ++cp )
   {
      std::cout << "'" << *cp << "' ";
   }
   std::cout << std::endl;

   return (array[80]);
}

int main()
{
   split("What are you having for lunch today?");
}

Output:

'W' 'h' 'a' 't' 'a' 'r' 'e' 'y' 'o' 'u' 'h' 'a' 'v' 'i' 'n' 'g' 'f' 'o' 'r' 'l' 'u' 'n' 'c' 'h' 't' 'o' 'd' 'a' 'y'

Comments

0

Another simple solution:

I change return type to string, If you insist you can take the c_str() of return value to const char*

string is already like vector of characters.

string split(string str)
{
    size_t i=0;
    while (string::npos != (i=str.find_first_of(",. ",i))){
        str.erase(i,1);
    }
    return str;
}

You use it like this:

string s = split("bla bla bla");

if you want the pointer:

cout<<s.c_str()<<endl;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.