Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problem using strings

Problem using strings

Scheduled Pinned Locked Moved C / C++ / MFC
helpasp-netcomquestion
18 Posts 11 Posters 0 Views 1 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.
  • V Victor Nijegorodov

    Why not using the std::string with all its and other std methods? :zzz: :confused:

    R Offline
    R Offline
    Rodrigo Lourenco
    wrote on last edited by
    #7

    I'm working with C

    1 Reply Last reply
    0
    • J Joe Woodbury

      This line of code puzzles me:

      *(s + i) != '\0' < len

      It probably should be:

      i < len

      A second problem is that you are doing the copy only on !isupper, when you should be doing it always as soon as an uppercase character is detected. A third problem is the strcpy() at the end. It's pointless. A fourth problem is that you are returning a pointer to the a stack value which becomes invalid because the call to printf is likely overwriting it and is likely causing a fault.

      R Offline
      R Offline
      Rodrigo Lourenco
      wrote on last edited by
      #8

      You are correct, that was one mistake, but it should be *(s + i) != '\0' because I am searching for the end of line.

      1 Reply Last reply
      0
      • L Lost User

        Try this:

        char* my_strdelupper(char* s){
        int i, len;
        char* sl;

        len = strlen(s);
        sl = (char\*)malloc(len+1); // space for the converted string
        i = 0; // index to the converted string buffer
        while (\*s != '\\0'){
            if (!isupper(\*s)){
                sl\[i++\] = \*s; // copy lower case letters
            }
            s++;
        }
        sl\[i\] = '\\0';
        printf("%s\\n", sl);
        
        return sl;
        

        }

        R Offline
        R Offline
        Rodrigo Lourenco
        wrote on last edited by
        #9

        The stupid thing is that I can't use malloc, so I made a char array with fixed size to overcame that problem.

        1 Reply Last reply
        0
        • D David Crow

          If I were to hazard a guess, I'd say it has to do with aux being destroyed (i.e., going out of scope) once my_strdelupper() returns.

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

          R Offline
          R Offline
          Rodrigo Lourenco
          wrote on last edited by
          #10

          I think you are correct, but I tried to copy aux to s and didn't work, now I changed the code to send an array, them befour I leave I copy using srtcpy aux to s, and it's working.

          1 Reply Last reply
          0
          • K k5054

            You have: char *s3 = "...". Although s3 is declared as char * the type of the string is const char *. Use char s3[] = "..." instead.

            R Offline
            R Offline
            Rodrigo Lourenco
            wrote on last edited by
            #11

            It worked thanks

            1 Reply Last reply
            0
            • R Rodrigo Lourenco

              I'm trying to change a given string in a function, but I send a string, try to change it directly and get a core dump, so I tried to create a string in the function, but it doesn't send the correct string. This function must eliminate all Capital letter's it's a simple one, but I am making a mistake and don't see where. Can any one help me please.

              #include #include #include #include #define false 1
              #define true 0

              char* my_strdelupper(char* s){

                int i,j, len;
                char aux\[100\];
                char debuging;
              
                len = strlen(s);
                j = 0;
                for (i = 0; \*(s + i) != '\\0' < len; i++){
                    if (!isupper(\*(s+i))){
                        printf("%c",\*(s+i));
                        debuging = \*(s + i);
                        \*(aux + j) = debuging;
                       j++;
                    }
                }
                \*(aux + j) = '\\0';
                printf("\\n");
                puts(aux);
              
                return strcpy(aux, aux);
              

              }

              int main()
              {
              char *s3="Quem quer casar com um programador?";
              printf("%s\n",my_strdelupper(s3)); /* aria, sabes que é s o meu "rande mor " */

              return 0;
              }

              R Offline
              R Offline
              Rodrigo Lourenco
              wrote on last edited by
              #12

              Thank you, for all, it was a great help. The problem was solved by:

              #include #include #include #include #define false 1
              #define true 0

              char* my_strdelupper(char* s){

                int i,j, len;
                char aux\[100\];
                char debuging;
              
                len = strlen(s);
                j = 0;
                for (i = 0; \*(s + i) != '\\0'; i++){
                    if (!isupper(\*(s+i))){
                        debuging = \*(s + i);
                        \*(aux + j) = debuging;
                       j++;
                    }
                }
                \*(aux + j) = '\\0';
              
                strcpy(s, aux);
                return s;
              

              }

              int main()
              {
              char s3[]="Maria, TU sabes que TU éS o meu \"Grande Amor\"";
              printf("%s\n",my_strdelupper(&s3[0])); /* aria, sabes que é s o meu "rande mor " */

              return 0;
              }

              J CPalliniC 2 Replies Last reply
              0
              • R Rodrigo Lourenco

                Thank you, for all, it was a great help. The problem was solved by:

                #include #include #include #include #define false 1
                #define true 0

                char* my_strdelupper(char* s){

                  int i,j, len;
                  char aux\[100\];
                  char debuging;
                
                  len = strlen(s);
                  j = 0;
                  for (i = 0; \*(s + i) != '\\0'; i++){
                      if (!isupper(\*(s+i))){
                          debuging = \*(s + i);
                          \*(aux + j) = debuging;
                         j++;
                      }
                  }
                  \*(aux + j) = '\\0';
                
                  strcpy(s, aux);
                  return s;
                

                }

                int main()
                {
                char s3[]="Maria, TU sabes que TU éS o meu \"Grande Amor\"";
                printf("%s\n",my_strdelupper(&s3[0])); /* aria, sabes que é s o meu "rande mor " */

                return 0;
                }

                J Offline
                J Offline
                jschell
                wrote on last edited by
                #13

                As a note - you define 'len' but you don't use it. Presumably the assignment does not say otherwise, using that would make your code simpler.

                1 Reply Last reply
                0
                • R Rodrigo Lourenco

                  Thank you, for all, it was a great help. The problem was solved by:

                  #include #include #include #include #define false 1
                  #define true 0

                  char* my_strdelupper(char* s){

                    int i,j, len;
                    char aux\[100\];
                    char debuging;
                  
                    len = strlen(s);
                    j = 0;
                    for (i = 0; \*(s + i) != '\\0'; i++){
                        if (!isupper(\*(s+i))){
                            debuging = \*(s + i);
                            \*(aux + j) = debuging;
                           j++;
                        }
                    }
                    \*(aux + j) = '\\0';
                  
                    strcpy(s, aux);
                    return s;
                  

                  }

                  int main()
                  {
                  char s3[]="Maria, TU sabes que TU éS o meu \"Grande Amor\"";
                  printf("%s\n",my_strdelupper(&s3[0])); /* aria, sabes que é s o meu "rande mor " */

                  return 0;
                  }

                  CPalliniC Offline
                  CPalliniC Offline
                  CPallini
                  wrote on last edited by
                  #14

                  Note you don't need extra storage, the function may change the string in place:

                  char * remove_upper( char * s)
                  {
                  char * p, *q;

                  for (p = q = s; *p != '\0'; ++p)
                  if ( ! isupper(*p) )
                  *q++ = *p;

                  *q = '\0';
                  return s;
                  }

                  In testa che avete, signor di Ceprano?

                  P J 2 Replies Last reply
                  0
                  • CPalliniC CPallini

                    Note you don't need extra storage, the function may change the string in place:

                    char * remove_upper( char * s)
                    {
                    char * p, *q;

                    for (p = q = s; *p != '\0'; ++p)
                    if ( ! isupper(*p) )
                    *q++ = *p;

                    *q = '\0';
                    return s;
                    }

                    P Offline
                    P Offline
                    Peter_in_2780
                    wrote on last edited by
                    #15

                    Taken from K&R 1st ed? Certainly has that feel> :laugh: ;P

                    Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

                    CPalliniC 1 Reply Last reply
                    0
                    • P Peter_in_2780

                      Taken from K&R 1st ed? Certainly has that feel> :laugh: ;P

                      Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

                      CPalliniC Offline
                      CPalliniC Offline
                      CPallini
                      wrote on last edited by
                      #16

                      It is the Wonderful C programming language, of course has such a feel. In any case, were Dennis and Brian the ones who plagiarized my programs. :-D

                      In testa che avete, signor di Ceprano?

                      1 Reply Last reply
                      0
                      • V Victor Nijegorodov

                        Why not using the std::string with all its and other std methods? :zzz: :confused:

                        M Offline
                        M Offline
                        Member_14494321
                        wrote on last edited by
                        #17

                        As I have not clear idea about the std but yes this library used in most of the cases. [Fixed] Dell Printer Error 016 302 | Error Code 0x[^] helped me to get the solution.

                        1 Reply Last reply
                        0
                        • CPalliniC CPallini

                          Note you don't need extra storage, the function may change the string in place:

                          char * remove_upper( char * s)
                          {
                          char * p, *q;

                          for (p = q = s; *p != '\0'; ++p)
                          if ( ! isupper(*p) )
                          *q++ = *p;

                          *q = '\0';
                          return s;
                          }

                          J Offline
                          J Offline
                          John R Shaw
                          wrote on last edited by
                          #18

                          I was begging to think no one here was thinking. Thanks

                          INTP "Program testing can be used to show the presence of bugs, but never to show their absence." - Edsger Dijkstra "I have never been lost, but I will admit to being confused for several weeks. " - Daniel Boone

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups