Skip to content

pdwallace/string-walker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

StringWalker - Object-Oriented String Navigation

A Python class that provides an object-oriented way to walk through strings, get characters at specific positions, and replace characters.

Features

  • String Navigation: Walk forward and backward through a string
  • Character Access: Get characters at current position or specific positions
  • Character Replacement: Replace characters at specific positions or current position
  • Position Management: Move to specific positions, start, or end of string
  • Bounds Checking: Safe navigation with bounds checking

Usage

Basic Usage

from string_walker import StringWalker

# Create a StringWalker with a string
walker = StringWalker("Hello, World!")

# Get current position and character
print(walker.get_current_position())  # 0
print(walker.get_current_character())  # 'H'

# Walk forward
walker.walk_forward(3)
print(walker.get_current_character())  # 'l'

# Walk backward
walker.walk_backward(1)
print(walker.get_current_character())  # 'e'

# Get character at specific position
char = walker.get_character_at(7)  # 'W'

# Replace character at position
walker.replace_character_at(7, 'X')
print(walker.get_string())  # "Hello, Xorld!"

Available Methods

Navigation Methods

  • walk_forward(steps=1): Move forward by specified number of steps
  • walk_backward(steps=1): Move backward by specified number of steps
  • go_to_position(position): Move to a specific position
  • go_to_start(): Move to the beginning of the string
  • go_to_end(): Move to the end of the string

Character Access Methods

  • get_current_character(): Get character at current position
  • get_character_at(position): Get character at specific position
  • get_current_position(): Get current position

Character Replacement Methods

  • replace_character_at(position, new_char): Replace character at specific position
  • replace_current_character(new_char): Replace character at current position

Utility Methods

  • get_string(): Get the current string
  • get_length(): Get the length of the string
  • is_at_start(): Check if at the beginning
  • is_at_end(): Check if at the end

Running the Demo

To see the StringWalker in action, run:

python3 string_walker.py

This will execute the demonstration code that shows all the features of the StringWalker class.

CLI Application

The StringWalker is also available as a command-line interface for interactive use:

Basic Usage

# Start with an initial string
python3 string_walker_cli.py "Hello World"

# Start without initial string (will prompt for one)
python3 string_walker_cli.py

# Run a quick demo
python3 string_walker_cli.py --demo

# Show help
python3 string_walker_cli.py --help

CLI Commands

Once in the interactive CLI, you can use these commands:

  • help or h - Show help message
  • status or s - Show current status
  • forward <steps> or f <steps> - Walk forward (default: 1 step)
  • backward <steps> or b <steps> - Walk backward (default: 1 step)
  • goto <position> or g <pos> - Go to specific position
  • start - Go to start of string
  • end - Go to end of string
  • get <position> - Get character at specific position
  • replace <pos> <char> - Replace character at position
  • current <char> - Replace character at current position
  • string - Show the current string
  • length - Show string length
  • clear - Clear the screen
  • quit, q, or exit - Exit the application

CLI Examples

walker> f 3                    # Walk forward 3 steps
walker> b                      # Walk backward 1 step
walker> g 5                    # Go to position 5
walker> get 7                  # Get character at position 7
walker> replace 3 X            # Replace character at position 3 with 'X'
walker> current Y              # Replace current character with 'Y'

Running Tests

To run comprehensive tests and examples, use:

python3 test_examples.py

This will run various test scenarios including:

  • Basic operations
  • Navigation methods
  • Character replacement
  • Bounds checking
  • Edge cases (empty strings, single characters)

Interactive Demo

The test_examples.py file also includes an interactive demo. To use it, uncomment the last line in the file and run:

python3 test_examples.py

This will allow you to interactively walk through any string you provide.

Example Output

=== StringWalker Demo ===
Initial string: 'Hello, World!'
Length: 13
Current position: 0
Current character: 'H'

Walking forward 3 steps...
Position: 3, Character: 'l'

Walking backward 1 step...
Position: 2, Character: 'l'

Getting character at position 7...
Character at position 7: 'W'

Replacing character at position 7 with 'X'...
Updated string: 'Hello, Xorld!'

Going to position 10...
Position: 10, Character: 'd'

Replacing current character with 'Y'...
Updated string: 'Hello, XorlY!'

Going to start...
Position: 0, Character: 'H'

Going to end...
Position: 12, Character: '!'

=== Final State ===
StringWalker at position 12 (char: '!') of 'Hello, XorlY!'

Error Handling

The StringWalker class includes bounds checking:

  • Navigation methods return True if successful, False if out of bounds
  • Character access methods return None if the position is out of bounds
  • Replacement methods return False if the position is out of bounds or the new character is invalid

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors