Tutorialspoint
Problem
Solution
Submissions

Replace all spaces with underscores.

Certification: Basic Level Accuracy: 40% Submissions: 5 Points: 5

Write a C# program to implement the ReplaceSpaces(string s) function, which replaces all spaces in a given string with underscores. The function should return the modified string.

Example 1
  • Input: s = "Hello World"
  • Output: "Hello_World"
  • Explanation: The string "Hello World" contains one space character.
    • The space character is replaced with an underscore.
    • The resulting string is "Hello_World".
Example 2
  • Input: s = " Programming is fun "
  • Output: "__Programming__is__fun__"
  • Explanation: The string " Programming is fun " contains multiple spaces, including leading and trailing spaces.
    • All space characters are replaced with underscores.
    • The resulting string is "__Programming__is__fun__".
Constraints
  • 0 ≤ s.length ≤ 1000
  • s consists of English letters (lower-case and upper-case), digits (0-9), spaces, and basic punctuation
  • Time Complexity: O(n)
  • Space Complexity: O(n)
StringsControl StructuresTech MahindraD. E. Shaw
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Iterate through each character in the string
  • Create a new string by replacing spaces with underscores
  • Consider using built-in string methods for efficient replacement
  • Handle both single and multiple consecutive spaces correctly


Submitted Code :