M
M
Matef2020-11-19 04:24:57
C++ / C#
Matef, 2020-11-19 04:24:57

How to compare words?

in general, I'm trying to learn c ++, there is a learning task:

Given a string with words separated by spaces. Is it true that all words in a string are the same length?


Here is the code I ended up writing:
#include <iostream>
#include <cstring>

using namespace std;


main() {
    
    char text[0];
    int len = strlen(text);
    cout << "text ";
    cin.getline(text, len);
      
      
    for(int i = 0; i < len; i++){
        for(int j = 0; j < len; j++){
            
            if(text[i] == text[j]){
                cout<<"Все слова равны" << endl;
                break;
            }else{
                cout<<"Слова не ровны" << endl;
            }
            
        }
    }
    
}


Please help, what am I doing wrong!?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AkiroToshiro, 2020-11-19
@Matef

First, enter the number of words, and then the line itself, you can just one word at a time

#include <iostream>
#include <cstring>
#include <vector>
using namespace std;


int main() {
    vector<string> words;
    int words_count;
    cin >> words_count;
    string tmp;
    for(int i = 0;i < words_count; i++) {
        cin >> tmp;
        words.push_back(tmp);
    }
    bool check = true;
    for(int i = 0;i<words_count;i++) {
        if(words[i].length() != words[0].length())
        {
            check = false;
        }
    }
    if(check) {
        cout << "Yes";
    } else {
        cout << "No";
    }

    return 0;

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question