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#
  4. how can i write functions in String class without using any tools?

how can i write functions in String class without using any tools?

Scheduled Pinned Locked Moved C#
questioncsharptoolshelp
36 Posts 10 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.
  • C Christian Graus

    Yeah, it's homework. I don't have a problem with trying to point him in the right direction, he was up front about it and he's not asking for a solution. It sounds messy tho.

    Christian Graus Please read this if you don't understand the answer I've given you. If you're still stuck, ask me for more information.

    P Offline
    P Offline
    Paul Conrad
    wrote on last edited by
    #19

    Christian Graus wrote:

    It sounds messy tho.

    Gearing him up for real world maintenance projects, I suppose.

    "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

    C L 2 Replies Last reply
    0
    • P Paul Conrad

      Christian Graus wrote:

      It sounds messy tho.

      Gearing him up for real world maintenance projects, I suppose.

      "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #20

      *grin*

      Christian Graus Please read this if you don't understand the answer I've given you. If you're still stuck, ask me for more information.

      1 Reply Last reply
      0
      • C Christian Graus

        I think either the teacher is an idiot, or the OP is wrong in his understanding

        Christian Graus Please read this if you don't understand the answer I've given you. If you're still stuck, ask me for more information.

        P Offline
        P Offline
        Paul Conrad
        wrote on last edited by
        #21

        Christian Graus wrote:

        the teacher is an idiot

        Might be a safe bet. I don't even subject my CS students to worthless crap assignments. Assignments I push on them have some real meaning to them.

        "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

        C 1 Reply Last reply
        0
        • P Paul Conrad

          Sajjad Izadi wrote:

          have to write that till next two days

          Better get going then.

          "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #22

          I think you need to re-read the assignment. You seem to be saying 'turn a string into a byte array without using any methods on the string class that would give you access to the contents of the string'.

          Christian Graus Please read this if you don't understand the answer I've given you. If you're still stuck, ask me for more information.

          P 1 Reply Last reply
          0
          • P Paul Conrad

            Christian Graus wrote:

            the teacher is an idiot

            Might be a safe bet. I don't even subject my CS students to worthless crap assignments. Assignments I push on them have some real meaning to them.

            "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #23

            Yeah, they should be learning things they can use, although I am always for students writing a list class that they will never use, for example.

            Christian Graus Please read this if you don't understand the answer I've given you. If you're still stuck, ask me for more information.

            P 1 Reply Last reply
            0
            • C Christian Graus

              I think you need to re-read the assignment. You seem to be saying 'turn a string into a byte array without using any methods on the string class that would give you access to the contents of the string'.

              Christian Graus Please read this if you don't understand the answer I've given you. If you're still stuck, ask me for more information.

              P Offline
              P Offline
              Paul Conrad
              wrote on last edited by
              #24

              Didn't you mean to direct this to Sajjad? I'm not the one doing the assignment :-\

              "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

              C 1 Reply Last reply
              0
              • P Pete OHanlon

                So - are you meant to use telepathy? Your professor has effectively ruled out all useful constructs.

                Deja View - the feeling that you've seen this post before.

                My blog | My articles

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #25

                I usually suggest prayer when people ask stuff like this.

                Christian Graus Please read this if you don't understand the answer I've given you. If you're still stuck, ask me for more information.

                1 Reply Last reply
                0
                • C Christian Graus

                  Yeah, they should be learning things they can use, although I am always for students writing a list class that they will never use, for example.

                  Christian Graus Please read this if you don't understand the answer I've given you. If you're still stuck, ask me for more information.

                  P Offline
                  P Offline
                  Paul Conrad
                  wrote on last edited by
                  #26

                  Christian Graus wrote:

                  always for students writing a list class that they will never use

                  Sure, it primes them up for writing code that never gets used because boss says otherwise, and writing something like a list class teaches the fundamentals of OOP.

                  Christian Graus wrote:

                  they should be learning things they can use

                  Exactly, since they will walk out of the classroom with, hopefully sharper skillsets that can be of value to them. I've had students in the past, who earned lower than average grades thank me for increasing their skillsets, though their academic grade doesn't necessarily reflect it.

                  "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                  1 Reply Last reply
                  0
                  • P Paul Conrad

                    Didn't you mean to direct this to Sajjad? I'm not the one doing the assignment :-\

                    "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                    C Offline
                    C Offline
                    Christian Graus
                    wrote on last edited by
                    #27

                    Oh, I hate it when people do that...

                    Christian Graus Please read this if you don't understand the answer I've given you. If you're still stuck, ask me for more information.

                    P 1 Reply Last reply
                    0
                    • S Sajjad Izadi

                      no but it's my homework :) and i have to write that till next two days!

                      C Offline
                      C Offline
                      Christian Graus
                      wrote on last edited by
                      #28

                      I think you need to re-read the assignment. You seem to be saying 'turn a string into a byte array without using any methods on the string class that would give you access to the contents of the string'.

                      Christian Graus Please read this if you don't understand the answer I've given you. If you're still stuck, ask me for more information.

                      1 Reply Last reply
                      0
                      • C Christian Graus

                        Oh, I hate it when people do that...

                        Christian Graus Please read this if you don't understand the answer I've given you. If you're still stuck, ask me for more information.

                        P Offline
                        P Offline
                        Paul Conrad
                        wrote on last edited by
                        #29

                        Oh well, no worries :-D

                        "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                        1 Reply Last reply
                        0
                        • P Paul Conrad

                          Christian Graus wrote:

                          It sounds messy tho.

                          Gearing him up for real world maintenance projects, I suppose.

                          "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #30

                          :laugh:

                          P 1 Reply Last reply
                          0
                          • L Lost User

                            :laugh:

                            P Offline
                            P Offline
                            Paul Conrad
                            wrote on last edited by
                            #31

                            I'll admit I have some code that is a maintenance nightmare, considered "job security" to a certain extent :rolleyes:

                            "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                            1 Reply Last reply
                            0
                            • S Sajjad Izadi

                              how can i make my string to char[] without using any built-in classes or prepared methods, += and = or 'foreach' in C#? in an other expression, i want to (have to) write trim, insert, concat, countOf(char c) and etc without using any of methods or classes or foreach and even += or + in C#. how is that? please help, thanks.

                              G Offline
                              G Offline
                              Guffa
                              wrote on last edited by
                              #32

                              Without using the built in members of the String class, you can't access it's data.

                              Despite everything, the person most likely to be fooling you next is yourself.

                              P 1 Reply Last reply
                              0
                              • C Christian Graus

                                I think either the teacher is an idiot, or the OP is wrong in his understanding

                                Christian Graus Please read this if you don't understand the answer I've given you. If you're still stuck, ask me for more information.

                                D Offline
                                D Offline
                                Dave Kreskowiak
                                wrote on last edited by
                                #33

                                Why does it have to be "or"?? Why not "and"?? ;)

                                A guide to posting questions on CodeProject[^]
                                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                     2006, 2007, 2008

                                1 Reply Last reply
                                0
                                • G Guffa

                                  Without using the built in members of the String class, you can't access it's data.

                                  Despite everything, the person most likely to be fooling you next is yourself.

                                  P Offline
                                  P Offline
                                  PIEBALDconsult
                                  wrote on last edited by
                                  #34

                                  I was just wondering whether or not you can with unsafe code... could be fun... :suss: Howsabout this :-D :

                                  private unsafe static void
                                  F
                                  (
                                  string S
                                  ,
                                  out char[] A
                                  )
                                  {
                                  int index = 0 ;

                                  fixed ( char\* s = S )
                                  {
                                      while ( s \[ index \] != '\\0' )
                                      {
                                          index++ ;
                                      }
                                  
                                      A = new char \[ index \] ;
                                  
                                      fixed ( char\* a = A )
                                      {
                                          while ( index >= 0 )
                                          {
                                              a \[ index \] = s \[ index \] ;
                                  
                                              index-- ;
                                          }
                                      }
                                  }
                                  
                                  return ;
                                  

                                  }

                                  Or lambda expressions?

                                  modified on Monday, July 7, 2008 11:47 PM

                                  1 Reply Last reply
                                  0
                                  • S Sajjad Izadi

                                    how can i make my string to char[] without using any built-in classes or prepared methods, += and = or 'foreach' in C#? in an other expression, i want to (have to) write trim, insert, concat, countOf(char c) and etc without using any of methods or classes or foreach and even += or + in C#. how is that? please help, thanks.

                                    P Offline
                                    P Offline
                                    PIEBALDconsult
                                    wrote on last edited by
                                    #35

                                    It could be an exercise intended to show that it can't be done. But probably it's poor communication.

                                    1 Reply Last reply
                                    0
                                    • S Sajjad Izadi

                                      how can i make my string to char[] without using any built-in classes or prepared methods, += and = or 'foreach' in C#? in an other expression, i want to (have to) write trim, insert, concat, countOf(char c) and etc without using any of methods or classes or foreach and even += or + in C#. how is that? please help, thanks.

                                      P Offline
                                      P Offline
                                      PIEBALDconsult
                                      wrote on last edited by
                                      #36

                                      Here are Left and Right Trim methods that don't access any methods of the String class, but I hope this isn't what the teacher is expecting.

                                      private unsafe static void
                                      LeftTrim
                                      (
                                      string Subject
                                      ,
                                      System.Collections.Generic.HashSet<char> Chars
                                      )
                                      {
                                      fixed ( char* subject = Subject )
                                      {
                                      int* length = (int*) subject - 1 ;
                                      int front = 0 ;
                                      int back = 0 ;

                                          while
                                          (
                                              ( front < \*length )
                                          &&
                                              ( Chars.Contains ( \*(subject + front) ) )
                                          )
                                          {
                                              front++ ;
                                          }
                                      
                                          while ( front < \*length )
                                          {
                                              \*(subject + back++) = \*(subject + front++) ;
                                          }
                                      
                                          \*length = back ;
                                      }
                                      
                                      return ;
                                      

                                      }

                                      private unsafe static void
                                      RightTrim
                                      (
                                      string Subject
                                      ,
                                      System.Collections.Generic.HashSet<char> Chars
                                      )
                                      {
                                      fixed ( char* subject = Subject )
                                      {
                                      int* length = (int*) subject - 1 ;

                                          while
                                          (
                                              ( \*length >= 0 )
                                          &&
                                              ( Chars.Contains ( \*(subject + \*length - 1) ) )
                                          )
                                          {
                                              (\*length)-- ;
                                          }
                                      }
                                      
                                      return ;
                                      

                                      }

                                      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