जॉइन Examsbook
860 0

प्र: Write a method to fill all the spaces in a string with '%20'

  • उत्तर देखेंउत्तर छिपाएं
  • Workspace

उत्तर :
व्याख्या :

public class Test {     public void replaseSpaces(char[] str, int length) {         int spaceCount = 0, newLength = 0, i = 0;         for(i = 0; i < length; i++) {             if (str[i] == ' ')                  spaceCount++;         }           newLength = length + (spaceCount * 2);         str[newLength] = '\0';         for(i = length - 1; i >= 0; i--) {             if (str[i] == ' ') {                 str[newLength - 1] = '0';                 str[newLength - 2] = '2';                 str[newLength - 3] = '%';                 newLength = newLength - 3;             }             else {                 str[newLength - 1] = str[i];                 newLength = newLength - 1;             }         }         System.out.println(str);     }       public static void main(String[] args) {         Test tst = new Test();         char[] ch = {'t', 'h', 'e', ' ', 'd', 'o', 'g', ' ', ' ', ' ', ' ', ' ', ' '};         int length = 6;         tst.replaseSpaces(ch, length);       } }

क्या आपको यकीन है

  त्रुटि की रिपोर्ट करें

कृपया संदेश दर्ज करें
त्रुटि रिपोर्ट सफलतापूर्वक जमा हुई