QString to char*



  • Hi everyone. I'm trying to insert into the array char* elements (characters), which is repeated in the string, then I'm trying to count the number of characters and after counting to remove duplicate characters. I want to display all the characters that were in the strings and their counters. But, this code is not written characters. Those characters that are removed are output as "0". Help me please.

    code:

    QByteArray ba;
    char *str, *str2;
    double *countchar;

    for(int i = 0; i<strlens;i++)
    {
        countchar = new double[i];
    }
    

    QString string2 = "bodya_krasava_epta"; // b - 1, o - 1, d - 1, y - 1, a - 5, "-" - 2, k - 1 e.t.c
    ui->textEdit->append(string2);
    ba = string2.toLatin1();
    str = ba.data();
    str2 = ba.data();
    for(int i = 0; i<strlens; i++) // strlens = 18
    {
    countchar[i] = string2.count(str[i]);
    string2.remove(QString(str[i]));
    str2[i] = str[i];
    ui->textEdit->append(QString::number(countchar[i]));
    ui->textEdit->append(string2);
    }

    Result should be like:
    for i = 0:
    [0] odya_krasava_epta // b - 1
    [1] dya_krasava_epta // o - 1
    [2] ya_krasava_epta // d - 1
    [3] a_krasava_epta // y - 1
    [4] _krsv_ept // a - 5
    [5] krsvept // - - 2
    [6] rsvept // k - 1
    [7] svept // r - 1
    [8] vept // s - 1
    [9] ept // v - 1
    [10] pt // e - 1
    [11] t // p - 1
    [12] // t - 1


  • Moderators

    I don't really understand what you want to do. But some notes.

    Why do you use double for a counter (countchar)? Just use int.
    str2[i] = str[i]; Both pointers point to the same char array, so this assignment will not change anything.
    Why do you use char* at all? You can access characters in QString using [] operator.
    If you want to remove a character from a QString just do:

    string2.remove(str[i]);
    

    This one does not make any sense:

    for(int i = 0; i<strlens;i++)
    {
        countchar = new double[i];
    }
    

    What is strlens? Why do you allocate memory in a loop (You produce memory leaks in this loop)? Why is countchar a pointer?
    Why not just do:

    int countchar[strlens];
    

  • Moderators

    @Dejavu Hi, and welcome to the Qt forum!

    #include <QString>
    #include <QList>
    #include <QDebug>
    #include <QPair>
    
    typedef QPair<QChar, int> YourPair;
    typedef QList<YourPair> YourList;
    
    YourList computeYourHomework(const QString &s) {
        QList<QChar> list;
        for (int i=0; i<s.size(); i++) {
            const QChar c(s.at(i));
            if (!list.contains(c)) {
                list << c;
            }
        }
        YourList result;
        for (int i=0; i<list.size(); i++) {
            const QChar c(list.at(i));
            const int count = s.count(c);
            result << YourPair(c,count);
        }
        return result;
    }
    
    void printYourHomework( const YourList &list )
    {
        for (int i=0; i<list.size(); i++) {
            const YourPair pair = list.at(i);
            qDebug() << QString("'%1' - %2").arg(pair.first).arg(pair.second);
        }
    }
    
    int main(int argc, char *argv[])
    {
        Q_UNUSED(argc)
        Q_UNUSED(argv)
        const YourList result = computeYourHomework("bodya_krasavka_epta");
        printYourHomework(result);
        return 0;
    }
    

    Cheers!



  • Thank's a lot for help. How i can get a access to values in QList to edit characters or counter?


  • Moderators

    See here http://doc.qt.io/qt-5/qlist.html

    list[i] = ...
    


  • Thanks a lot to all and thank you so much @Wieland


  • Moderators

    @Dejavu You're welcome ;-)



  • @Wieland sorry for new stupid question)
    When I try to display the text in textEdit I got this error: C2227: left of '->textEdit' must point to class/struct/union/generic type

    void printYourHomework( const YourList &list)
    {
    double *mynum, *freq, diap;
    double *cum_freq;
    double temp_cum = 0.0;
    for (int i=0; i<list.size(); i++)
    {
    mynum = new double[i];
    freq = new double[i];
    cum_freq = new double[i];
    }
    for (int i=0; i<list.size(); i++) {
    const YourPair pair = list.at(i);
    MainWindow::ui->textEdit->append(QString("'%1' - %2").arg(pair.first).arg(pair.second));
    mynum[i] = pair.second;
    freq[i] = mynum[i]/18;
    MainWindow::ui->textEdit->append(QString::number(freq[i], 'f', 8));
    }


  • Qt Champions 2016

    @Dejavu said:

    MainWindow::ui->textEdit->

    That might be the reason.
    You cannot access mainwindows UI outside of mainwindow.

    So easy fix is to move the functions to mainwindow
    ( remember to add it to the mainwin.h file too)

    YourList MainWindow::computeYourHomework(const QString &s) {
    ..
    ui->textEdit->append(xxx
    }



  • @mrjj thank you for the reply
    I add it to mainwin file:

    private slots:
    YourList computeYourHomework(const QString &s);
    void printYourHomework(const YourList &list);

    and then i received errors:
    http://prntscr.com/b2hmp6

    I add function in mainwindow.cpp.


  • Qt Champions 2016

    @Dejavu
    Hi
    Maybe dont list it as slots. ( not the real error)
    just put it under
    private:

    Then u also need to make sure it knows
    YourList type
    so u must move that over mainwindow class
    or put in its on file and
    #include "YourList .h"



  • @mrjj thank u so much.

    I fix this problem. I just add #include in header and now it works.
    Thanks a lot :)


  • Qt Champions 2016

    @Dejavu
    Super :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.