2

How to implement an array of strings with different sizes, i.e. an array stringar[4]={"black","red","blue","green"}. Also, how to access individual letters/characters of each element in C++? Edit: This is what I tried in CPP. But it gives the same output (i.e. 0) for all inputs. code:

#include <bits/stdc++.h>
using namespace std;
int main()
{   int n,i,j,p,q,f=0,count=0,key,m;
    char s[100][100];
    cin>>n;
    for( i = 0; i < n; i++)
      for (j = 0; j < 100; j++)
        cin >> s[i][j];
      for(i=0;i<n;i++)
        {
            for (j = 0; j < 100; j++) 
            {
                key = s[i][j];
                for (p = i + 1; p < n; p++) 
                {
                    for (q = 0; q < m; q++) 
                    {
                        if (key == s[p][q]) {f = 1;break;}
                        else {f = 0;continue;}
                    }
                }
                if (f == 1)
                    count++;
            }
        }
    cout<<count;        
    return 0;
}
4
  • 8
    Does using std::vector<std::string> count as "implementing" it for you? Commented Feb 25, 2019 at 9:34
  • If your question is answered, please mark it as 'answered', PGreen_xyz. Commented Feb 25, 2019 at 12:37
  • I am actually new to use stackoverflow. So how to mark it as answered? @Trantor Commented Feb 25, 2019 at 12:52
  • Navigate to the particular answer you consider solved your problem best, at the left side are the Up/Down "arrows" and the counter. Below, there is a hook you can set and unset to mark "the" answer. Commented Feb 25, 2019 at 13:01

3 Answers 3

2

In the end, you need to imagine that as a Matrix with variable sizes.

[b][l][a][c][k]

[r][e][d]

[b][l][u][e]

[g][r][e][n]

 std::vector<std::string> stringar;
    stringar.push_back( "black" );
    stringar.push_back( "red" );

    char letter = stringar[ 0 ][ 1 ]; 
    char letter2 = stringar[ 1 ][ 2 ];

letterwill have the letter in position 1 of the array in the matrix of position 0:

Array in position 0: black. letter in position 1: l

Array in position 1: red. Letter in position 2: d

[b][l][a][c][k]

[r][e][d]

Edit:

As you asked me in the comments, here it is a rough implementation, so you can see how it is done! (excuse my printfinstead of coutbut I was not able to use it.

int rows = 4; //black, red, blue, green ==> 4 elements
    int cols[4] = { 5, 3, 4, 5 }; // 5 letters for black, 3 letters for red, etc.
    int** matrix = new int*[rows];
    for (int i = 0; i < rows; ++i)
        matrix[i] = new int[cols[i]];

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols[i];j++) {
            matrix[i][j] = 0;
            printf("%d", matrix[i][j]);
        }
        printf("\n");
    }

This will show this output, that is what you are looking for (instead of 0s, whatever minerals you need).

00000 (black) 000 (red) 0000 (blue) 00000 (green)

The output:

Sign up to request clarification or add additional context in comments.

11 Comments

This was really helpful. Actually the question I was solving is as follows: A mineral is called a gemstone if it occurs at least once in each of the rocks in the array collection. Given a list of minerals embedded in each of John's rocks, display the number of types of gemstones he has in his collection. For example, the array of mineral composition strings arr=[abc,abc,bc] The minerals b and c appear in each composite, so there are 02 gemstones.
So, can you put more insight into the approach to be adopted to solve this problem?
wouldn't there be 3 gemstones and not 2? as abc, abc and bc, all three have b and c, right? Let's get the question right then: a mineral can be called, for example "abc", AND it is a gemstone if it contains, X in it, for example, a, or b, or c in this case right? @PGreen_xyz
Yes, I am really sorry i couldn't post the full question because of limit. So, each element in the array(abc,abc,a) defines the composition of a rock. Here a,b,c individually are different minerals in the rock. Since b and c are the only minerals present in each rock, b and c are gemstones. So there are two gemstones (b and c).
Remember, this is a "matrix" (well, it is and it is not, but to understand it better, it is). So to access the members, a double loop would be the thing(first loop for each word, second loop for each character of the word). CARE, the problem here is the length of the second loop, as it varies. You could change that at the end of the 1st loop, once every iteration of the second has been done! I'll try to implement it tomorrow, but try it by yourself today! @PGreen_xyz
|
1

Try this:

std::vector<std::string> VectorOfStrings;
VectorOfStrings.push_back( "blue" );
VectorOfStrings.push_back( "red" );

char letter = VectorOfStrings[ 1 ][ 0 ]; // access 'r'

Comments

1

You can access the strings and individual characters in a number of ways. Here are three examples:

#include <iostream>
#include <vector>

int main() {
    // dynamic array-like collection of strings:
    std::vector<std::string> stringar = {"black","red","blue","green"};

    // access using index
    for(size_t vidx = 0; vidx<stringar.size(); ++vidx) {
        std::cout << "Accessing the contents of " << stringar[vidx] << "\n";
        for(size_t sidx = 0; sidx<stringar[vidx].size(); ++sidx) {
            std::cout << " " << stringar[vidx][sidx] << "\n";
        }
    }

    // access using iterators
    for(std::vector<std::string>::iterator vit = stringar.begin(); vit!=stringar.end(); ++vit) {
        std::cout << "Accessing the contents of " << *vit << "\n";
        for(std::string::iterator sit = (*vit).begin(); sit!=(*vit).end(); ++sit) {
            std::cout << " " << *sit << "\n";
        }
    }

    // access using range based for loop
    for(const auto& str : stringar) {
        std::cout << "Accessing the contents of " << str << "\n";
        for(auto ch : str) {
            std::cout << " " << ch << "\n";
        }
    }
}

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.