-4

i already have this

std::string str_1, str_2, str_3, str_4, str_5, str_6, str_7, str_8;
std::string str;
std::getline(std::cin,str);
std::istringstream iss(str);
iss >> str_1 >> str_2 >> str_3 >> str_4 >> str_5 >> str_6 >> str_7 >> str_8;

input: h e l l o (in one string)

output: str_1 =h str_2 =e str_3=l str_4=l str_5=o

this is what im trying to achieve

input: hello (in a single string using cin)

output: h e l l o (in 5 seperate strings)

i need the input to be in one string beacus the amount of characters the user inputs varys

cin >> str_1 >> str_2 >> str_3 >> str_4 >> str_5 >> str_6 >> str_7 >> str_8 >> endl;

wont work beacus you have to declare all the variables

jelle66
  • 15
  • 1
  • 5
  • 8
    There doesn't appear to be a reason you actually need to split the string. Just iterate over it and print each character followed by a space. done. – NathanOliver Jul 20 '17 at 18:10
  • 4
    Why not read into single characters instead of strings? – Some programmer dude Jul 20 '17 at 18:10
  • 2
    why not `std::copy(std::istreambuf_iterator(std::cin), std::istreambuf_iterator(), std::ostream_iterator(std::cout, " "));`? – jaggedSpire Jul 20 '17 at 18:18
  • seriously though, if you want them in different strings why not iterate over the source string and construct a string from each character, pushing the results into a vector or something? – jaggedSpire Jul 20 '17 at 18:25
  • Also, define *single character.* Is this a single `char`? A single ASCII character? A single unicode code point? A single grapheme? – jaggedSpire Jul 20 '17 at 18:35

3 Answers3

1

Why not just iterate over a string and print it like this?

string str1;
cin >> str1;

for(int i=0; i<str1.length(); i++)
    cout << str[i] << " ";
cout << endl;
Danyal Imran
  • 1,862
  • 7
  • 17
0
string a = "hello"; 
cout << a[1];

Try iterating through the a[for different values of]

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

using namespace std;

typedef struct _ARRAY_ITEMS
{
  char item[10];
  int iPosicion;
} ARRAY_ITEMS;


int tokens(char * string, char token, ARRAY_ITEMS * outItems){
  char cItem[50];
  char TempItem[50];
  int x = 0,i;
  ARRAY_ITEMS items;

  memset(cItem, 0x00, sizeof(cItem));
  memset(TempItem, 0x00, sizeof(TempItem));

  sprintf( cItem,"%s", string);
  for(i = 0; i <= strlen(cItem); i++){
    if (cItem[i] != token){
    sprintf( TempItem,"%s%c",TempItem, cItem[i]);
    }else{
      memset( &items, 0x00, sizeof( ARRAY_ITEMS ) );
      sprintf( items.item,"%s",TempItem);
      items.iPosicion = x;
      memcpy( &outItems[x], &items, sizeof( ARRAY_ITEMS ) );
      memset(TempItem, 0x00, sizeof(TempItem));
      x++;
    }
  }
  memset( &items, 0x00, sizeof( ARRAY_ITEMS ) );
  sprintf( items.item,"%s",TempItem);
  items.iPosicion = x;
  memcpy( &outItems[x], &items, sizeof( ARRAY_ITEMS ) );
  return x;
}

int main() {
  ARRAY_ITEMS items[10];
    int iLenPos = 0;

    // iLenPos = tokens("01x02x03",'x', items);  //split for ","
   // iLenPos = tokens("01,02,03",',', items); //split for ","
    iLenPos = tokens("01.02.03",'.', items);  //split for "."

    for ( int i = 0; i <= iLenPos; ++i)
      {
        printf("POSICION %d numero %s\n",items[i].iPosicion, items[i].item);
      }
    //print POSICION 0 numero 01
    //print POSICION 1 numero 02
    //print POSICION 2 numero 03
  return 0;
}