Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QString to char*
Forum Updated to NodeBB v4.3 + New Features

QString to char*

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 4 Posters 4.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • DejavuD Dejavu

    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

    ? Offline
    ? Offline
    A Former User
    wrote on last edited by A Former User
    #3

    @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!

    1 Reply Last reply
    2
    • DejavuD Offline
      DejavuD Offline
      Dejavu
      wrote on last edited by Dejavu
      #4

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

      1 Reply Last reply
      0
      • jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #5

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

        list[i] = ...
        

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • DejavuD Offline
          DejavuD Offline
          Dejavu
          wrote on last edited by
          #6

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

          ? 1 Reply Last reply
          0
          • DejavuD Dejavu

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

            ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #7

            @Dejavu You're welcome ;-)

            DejavuD 1 Reply Last reply
            1
            • ? A Former User

              @Dejavu You're welcome ;-)

              DejavuD Offline
              DejavuD Offline
              Dejavu
              wrote on last edited by
              #8

              @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));
              }

              1 Reply Last reply
              0
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #9

                @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
                }

                1 Reply Last reply
                1
                • DejavuD Offline
                  DejavuD Offline
                  Dejavu
                  wrote on last edited by
                  #10

                  @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.

                  mrjjM 1 Reply Last reply
                  1
                  • DejavuD Dejavu

                    @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.

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #11

                    @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"

                    DejavuD 1 Reply Last reply
                    1
                    • mrjjM mrjj

                      @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"

                      DejavuD Offline
                      DejavuD Offline
                      Dejavu
                      wrote on last edited by
                      #12

                      @mrjj thank u so much.

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

                      mrjjM 1 Reply Last reply
                      1
                      • DejavuD Dejavu

                        @mrjj thank u so much.

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

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #13

                        @Dejavu
                        Super :)

                        1 Reply Last reply
                        1

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved