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.
  • D Offline
    D Offline
    Dejavu
    wrote on 10 May 2016, 03:06 last edited by Dejavu 5 Oct 2016, 03:16
    #1

    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

    ? 1 Reply Last reply 10 May 2016, 04:57
    0
    • J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 10 May 2016, 04:28 last edited by
      #2

      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];
      

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

      1 Reply Last reply
      1
      • D Dejavu
        10 May 2016, 03:06

        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 10 May 2016, 04:57 last edited by A Former User 5 Oct 2016, 04:58
        #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
        • D Offline
          D Offline
          Dejavu
          wrote on 10 May 2016, 09:16 last edited by Dejavu 5 Oct 2016, 09:18
          #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
          • J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 10 May 2016, 10:26 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
            • D Offline
              D Offline
              Dejavu
              wrote on 10 May 2016, 17:33 last edited by
              #6

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

              ? 1 Reply Last reply 10 May 2016, 17:45
              0
              • D Dejavu
                10 May 2016, 17:33

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

                ? Offline
                ? Offline
                A Former User
                wrote on 10 May 2016, 17:45 last edited by
                #7

                @Dejavu You're welcome ;-)

                D 1 Reply Last reply 10 May 2016, 18:22
                1
                • ? A Former User
                  10 May 2016, 17:45

                  @Dejavu You're welcome ;-)

                  D Offline
                  D Offline
                  Dejavu
                  wrote on 10 May 2016, 18:22 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 10 May 2016, 18:38 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
                    • D Offline
                      D Offline
                      Dejavu
                      wrote on 10 May 2016, 18:55 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 10 May 2016, 19:01
                      1
                      • D Dejavu
                        10 May 2016, 18:55

                        @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 10 May 2016, 19:01 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"

                        D 1 Reply Last reply 10 May 2016, 19:06
                        1
                        • mrjjM mrjj
                          10 May 2016, 19:01

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

                          D Offline
                          D Offline
                          Dejavu
                          wrote on 10 May 2016, 19:06 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 10 May 2016, 19:06
                          1
                          • D Dejavu
                            10 May 2016, 19:06

                            @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 10 May 2016, 19:06 last edited by
                            #13

                            @Dejavu
                            Super :)

                            1 Reply Last reply
                            1

                            1/13

                            10 May 2016, 03:06

                            • Login

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