0

How to split a string and store the words in a separate array without using strtok or istringstream and find the greatest word?? I am only a beginner so I should accomplish this using basic functions in string.h like strlen, strcpy etc. only. Is it possible to do so?? I've tried to do this and I am posting what I have done. Please correct my mistakes.

#include<iostream.h>
#include<stdio.h>
#include<string.h>
void count(char n[])
{
    char a[50], b[50];
    for(int i=0; n[i]!= '\0'; i++)
    {
        static int j=0;
        for(j=0;n[j]!=' ';j++)
        {
            a[j]=n[j];
        }
        static int x=0;
        if(strlen(a)>x)
        {
            strcpy(b,a);
            x=strlen(a);
        }
    }
    cout<<"Greatest word is:"<<b;
}

int main( int, char** )
{
    char n[100];
    gets(n);
    count(n);
}
Grimm The Opiner
  • 1,778
  • 11
  • 29
Punith
  • 11
  • 1
  • 1
  • 6
  • for spliting the string check this http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c?rq=1 – dmSherazi Nov 06 '13 at 12:46
  • 2
    Notice two things: 1.) That's more C than C++ - the tag 'C++' is confusing here. 2.) is deprecated - better use instead and write 'std::cout' or 'using namespace std;' at the top of your program – Constantin Nov 06 '13 at 12:51
  • Is the target array supposed to equal the source array minus white space? Because that's a trivial exercise. Or is it supposed to be an array of literal strings (ie: `char*`s)? (Of course, you should be using vectors and stl strings but this looks like homework, in which you're expected to suffer.) – Grimm The Opiner Nov 06 '13 at 12:59

4 Answers4

5

The code in your example looks like it's written in C. Functions like strlen and strcpy originates in C (although they are also part of the C++ standard library for compatibility via the header cstring).

You should start learning C++ using the Standard Library and things will get much easier. Things like splitting strings and finding the greatest element can be done using a few lines of code if you use the functions in the standard library, e.g:

// The text
std::string text = "foo bar foobar";

// Wrap text in stream.
std::istringstream iss{text};
// Read tokens from stream into vector (split at whitespace).
std::vector<std::string> words{std::istream_iterator<std::string>{iss}, std::istream_iterator<std::string>{}};
// Get the greatest word.
auto greatestWord = *std::max_element(std::begin(words), std::end(words), [] (const std::string& lhs, const std::string& rhs) { return lhs.size() < rhs.size(); });

Edit: If you really want to dig down in the nitty-gritty parts using only functions from std::string, here's how you can do to split the text into words (I leave finding the greatest word to you, which shouldn't be too hard):

// Use vector to store words.
std::vector<std::string> words;
std::string text = "foo bar foobar";

std::string::size_type beg = 0, end;
do {
    end = text.find(' ', beg);
    if (end == std::string::npos) {
        end = text.size();
    }
    words.emplace_back(text.substr(beg, end - beg));
    beg = end + 1;
} while (beg < text.size());
Felix Glas
  • 15,065
  • 7
  • 53
  • 82
0

I would write two functions. The first one skips blank characters for example

const char * SkipSpaces( const char *p )
{
    while ( *p == ' ' || *p == '\t' ) ++p;

    return ( p );
}

And the second one copies non blank characters

const char * CopyWord( char *s1, const char *s2 )
{
    while ( *s2 != ' ' && *s2 != '\t' && *s2 != '\0' ) *s1++ = *s2++;
    *s1 = '\0';

    return ( s2 );
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

try to get a word in a small array(obviously no word is >35 characters) you can get the word by checking two successive spaces and then put that array in strlen() function and then check if the previous word was larger then drop that word else keep the new word
after all this do not forget to initialize the word array with '\0' or null character after every word catch or this would happen:-
let's say 1st word in that array was 'happen' and 2nd 'to' if you don't initialize then your array will be after 1st catch :
happen
and 2nd catch :
*to*ppen

  • Why is no word more than 20 chars? `antidisestablishmentarianism`, `Supercalifragilisticexpialidocious`. Quite apart from that this answer needs some serious attention as I doubt anyone gets what you're trying to describe - and on top of that I'm not sure we yet know what the questioner is even trying to achieve. – Grimm The Opiner Nov 06 '13 at 13:34
0

Try this. Here ctr will be the number of elements in the array(or vector) of individual words of the sentence. You can split the sentence from whatever letter you want by changing function call in main.

#include<iostream>
#include<string>
#include<vector>
using namespace std;
void split(string s, char ch){
    vector <string> vec;
    string tempStr;
    int ctr{};
    int index{s.length()};
    for(int i{}; i<=index; i++){
        tempStr += s[i];
        if(s[i]==ch || s[i]=='\0'){
            vec.push_back(tempStr);
            ctr++;
            tempStr="";
            continue;
        }
    }
    for(string S: vec)
        cout<<S<<endl;
}
int main(){
    string s;
    getline(cin, s);
    split(s, ' ');
    return 0;
}