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. Managed C++/CLI
  4. Array and String

Array and String

Scheduled Pinned Locked Moved Managed C++/CLI
questiondata-structuresbeta-testing
8 Posts 4 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.
  • P Offline
    P Offline
    pourang
    wrote on last edited by
    #1

    char alpha[10] = "abcdefg"; string beta = "abcdefg"; alpha[1]= '\0'; gives the following result : alpha = a beta[1]='\0'; gives the following result : beta = a cdefg How can i use '\0' in strings as i use them in an array? I mean iwant that '\0' replace 'bcdefg' in string not just 'b'. Thanks

    U G C 3 Replies Last reply
    0
    • P pourang

      char alpha[10] = "abcdefg"; string beta = "abcdefg"; alpha[1]= '\0'; gives the following result : alpha = a beta[1]='\0'; gives the following result : beta = a cdefg How can i use '\0' in strings as i use them in an array? I mean iwant that '\0' replace 'bcdefg' in string not just 'b'. Thanks

      U Offline
      U Offline
      ubriela
      wrote on last edited by
      #2

      #include void main() { char string1[10]="abcdef"; char string2[10]="abcdef"; string1[1]='\0'; string2[1]='\0'; printf("%s\n%s",string1,string2); } result: a a ---- i don't think it is wrong:rolleyes:

      ubri

      G P 2 Replies Last reply
      0
      • P pourang

        char alpha[10] = "abcdefg"; string beta = "abcdefg"; alpha[1]= '\0'; gives the following result : alpha = a beta[1]='\0'; gives the following result : beta = a cdefg How can i use '\0' in strings as i use them in an array? I mean iwant that '\0' replace 'bcdefg' in string not just 'b'. Thanks

        G Offline
        G Offline
        George L Jackson
        wrote on last edited by
        #3

        Apparently, you are printing the result with the following: std::cout << beta << std::endl. If you print it with std::cout << beta.c_str() << std::endl, you get the desired print out. However, if you actually want to remove the characters following 'a' you can do this:

        #include <iostream>
        #include <string>
         
        int _tmain(int argc, _TCHAR* argv[])
        {
        std::string beta = "abcdefg";
        beta.erase(beta.begin() + 1, beta.end());
         
        std::cout << "Result #1: " << beta << std::endl;
        std::cout << "Result #2: " << beta.c_str() << std::endl;
         
        return 0;
        }

        Also, please post C++ questions in the C++ forum and not the C++/CLI forum.

        "We make a living by what we get, we make a life by what we give." --Winston Churchill

        P 1 Reply Last reply
        0
        • U ubriela

          #include void main() { char string1[10]="abcdef"; char string2[10]="abcdef"; string1[1]='\0'; string2[1]='\0'; printf("%s\n%s",string1,string2); } result: a a ---- i don't think it is wrong:rolleyes:

          ubri

          G Offline
          G Offline
          George L Jackson
          wrote on last edited by
          #4

          Unfortunately, he is trying to modify a std::string and not a char array string.

          "We make a living by what we get, we make a life by what we give." --Winston Churchill

          1 Reply Last reply
          0
          • G George L Jackson

            Apparently, you are printing the result with the following: std::cout << beta << std::endl. If you print it with std::cout << beta.c_str() << std::endl, you get the desired print out. However, if you actually want to remove the characters following 'a' you can do this:

            #include <iostream>
            #include <string>
             
            int _tmain(int argc, _TCHAR* argv[])
            {
            std::string beta = "abcdefg";
            beta.erase(beta.begin() + 1, beta.end());
             
            std::cout << "Result #1: " << beta << std::endl;
            std::cout << "Result #2: " << beta.c_str() << std::endl;
             
            return 0;
            }

            Also, please post C++ questions in the C++ forum and not the C++/CLI forum.

            "We make a living by what we get, we make a life by what we give." --Winston Churchill

            P Offline
            P Offline
            pourang
            wrote on last edited by
            #5

            Hi. Thanks for the tip. Pourang

            { in a land with no bird, no spring. My first journey was a return 0; }

            1 Reply Last reply
            0
            • P pourang

              char alpha[10] = "abcdefg"; string beta = "abcdefg"; alpha[1]= '\0'; gives the following result : alpha = a beta[1]='\0'; gives the following result : beta = a cdefg How can i use '\0' in strings as i use them in an array? I mean iwant that '\0' replace 'bcdefg' in string not just 'b'. Thanks

              C Offline
              C Offline
              Chetan Patel
              wrote on last edited by
              #6

              You are little bit consfused with char[] and std :: string Please Clear about that.

              Best Regards, Chetan Patel

              P 1 Reply Last reply
              0
              • U ubriela

                #include void main() { char string1[10]="abcdef"; char string2[10]="abcdef"; string1[1]='\0'; string2[1]='\0'; printf("%s\n%s",string1,string2); } result: a a ---- i don't think it is wrong:rolleyes:

                ubri

                P Offline
                P Offline
                pourang
                wrote on last edited by
                #7

                Thanks for the tip :-)

                { in a land with no bird, no spring. My first journey was a return 0; }

                1 Reply Last reply
                0
                • C Chetan Patel

                  You are little bit consfused with char[] and std :: string Please Clear about that.

                  Best Regards, Chetan Patel

                  P Offline
                  P Offline
                  pourang
                  wrote on last edited by
                  #8

                  Hi. I got my answer but you're right. I'm reading the c++ Primer + and there are different chapters to cover String. I did not read the chapter which explains in details. It sucks to be a beginner :-):)

                  { in a land with no bird, no spring. My first journey was a return 0; }

                  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