-1

[EDITED] i want to write a function to split given string in reverse order and store it in string array , something like:

 string* splitStr(string s, char c,int& ssize) {
    int size = s.size();
    string* ss = new string[size];

    int count = 0;
    ssize = 0;

    for (int j = 0; j < size; j++) {
        if (s.at(j) == c) { 
            count++; 
        }
    }

    ssize = ++count;
    for (int i = 0; i<size; i++) {
        if (s.at(i) == c) { ssize--; continue; }
        else { ss[ssize] += s.at(i); }
    }
    ssize = count;
    return ss;
}

Sample program:

string s = "this is some damn";
    int size = 0;
    string* ss = splitStr(s, ' ', size);
    for (int i = 0; i < size; i++) {
        cout << ss[i] << "\n";
    }
    system("PAUSE");

Output:

(this is empty line) 
damn
some
is

it's just a rough attempt , but generally it's very unreliable approach don't you think ? what could be the best possible solution is this case , without using any other data type except string,char,int,float ?

Zulqurnain Jutt
  • 1,083
  • 3
  • 15
  • 41

3 Answers3

0

Solution based an boost http://www.boost.org/doc/libs/1_61_0/doc/html/string_algo/usage.html#idp398633360 and reverse iterator http://en.cppreference.com/w/cpp/container/vector

#include <iostream>
#include<boost/algorithm/string.hpp>
#include <vector>

int main()
{
    std::string const str{"bla=blub"};
    std::vector<std::string> elems;

    boost::split(elems, str, boost::is_any_of("="));

    for(auto const& elem : elems )
        std::cout << elem << "\n";

    for(auto it=elems.rbegin(); it!=elems.rend(); ++it)
        std::cout << *it << "\n";

    return 0;
}
Roby
  • 2,011
  • 4
  • 28
  • 55
0

Read Kernighan and Ritchie

#include <string.h>

void reverse(char s[])

{

int length = strlen(s) ;
int c, i, j;

  for (i = 0, j = length - 1; i < j; i++, j--)
  {
    c = s[i];
    s[i] = s[j];
    s[j] = c;
  }
}
-2
 #include <stdio.h>

 void strrev(char *p)
 {
 char *q = p;
 while (q && *q) ++q;
 for (--q; p < q; ++p, --q)
    *p = *p ^ *q,
    *q = *p ^ *q,
    *p = *p ^ *q;
 }

 int main(int argc, char **argv)
 {
    do {
        printf("%s ", argv[argc - 1]);
        strrev(argv[argc - 1]);
       printf("%s\n", argv[argc - 1]);
    } while (--argc);

 return 0;
 }

This is XOR-swap thing. Take care to note that you must avoid swapping with self, because a^a==0.

Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104