Help with char/string

So i have this task to so search the text file for lines with more than 1 word.
and i cant figure out how to search my string array like that.
any help? i've searched the web, but i could not find how do that with a string array.
Text file :

5
Peru
Los Angeles
El Paso
Boston
Mesa

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  #include <iostream>
#include <fstream>
#include <string>

using namespace std;
void cityArray(string cities[], int & n);

int main()
{
    string cities[30];
    char str;
    int n;
    int k=0;
    cityArray(cities,n);
    //What now?



    return 0;
}



void cityArray(string cities[], int & n)
{
    ifstream fd("duomenys.txt");
    fd>>n;
    fd.ignore(256,'\n');

    for(int i= 0; i<n; i++)
    {
        getline(fd,cities[i]);
    }
}
count the number of spaces?

I mean, what is a word?
A word has a few characters and has an end, how may an end look like?
i know to count spaces, but internet examples do it only with one string line, not an array, so im confused :P
Then look up how to iterate through arrays ;)

When using C++11 you could do it like this:
1
2
3
4
for(const auto& str : cities) 
{
    // do something with each string
}


Otherwise you can just iterate through them using the index
1
2
3
4
5
for(int i = 0; i < n; ++i) 
{
    std::string str = cities[i];
    // do something with each string
}
1
2
3
4
5
6
7
8
9
10
	for(int i = 0; i< n; ++i)
    {
        string str = cities[i];
        for(int j=0; str[j]!= '\0'; j++)
        {     
            if (str[j] == ' ') //Checking for spaces
            {
                words++;
            }
        }

is this loop correct for checking spaces in lines?
Last edited on

Remember a string is a list of characters:
Counting spaces will not suffice if there is more than one space character between words.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void cityArray(string cities[], int &n);

int main()
{
    string cities[30];
    int n;
    int spaceCount=0;
    cityArray(cities,n);
    char char1;

    //What now?

    for(int i = 0; i < n; ++i)
    {
        spaceCount = 0;  // reset counter for next string of words
        for (int j = 0 ; j<cities[i].size(); j++)
        {
            char1 = cities[i][j]; // get next character in cities[i]
            cout << char1;        // print char

            if (char1 == ' ')     // check if char is a space
                spaceCount++;
        }
        cout << endl;
        cout << "spaceCount =" << spaceCount << endl;
    }

    return 0;
}



void cityArray(string cities[], int & n)
{
    ifstream fd("duomenys.txt");

    fd >> n;

    fd.ignore(256,'\n');

    for(int i= 0; i<n; i++)
    {
        getline(fd,cities[i]);
    }
}

Last edited on
@MisterWhite you should use std::getline to read the entire line and then check whether there are any spaces in the resultant string. This can be done by iterating through the characters of the string and checking for space using the isspace() function.
Last edited on
@CodeWriter the code works, thank you, but i dont understand this part
1
2
if (char1 == ' ')     // check if char is a space
                spaceCount++;
, i mean you check if the line is a space, how does that work ? :D
Last edited on
No it is not checking to see if the line is a space, it is checking to see if the current character at position j within the line is a space. The outer loop is for the lines and the inner loop for the characters in each line.

..0123456 <--- j
0 Peru
1 Los Angeles
2 El Paso
3 Boston
4 Mesa


The i value is indexing each line and the j value is indexing each character in a line
Last edited on
Topic archived. No new replies allowed.