How to remove spaces in a string without using any inbuilt methods or function in java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to remove spaces in a string without using any inbuilt methods or function in java

16th Dec 2018, 12:07 PM
Mohib Ulla Khan
Mohib Ulla Khan - avatar
6 Answers
+ 6
find below the code snippets as per the algorithm by KrOW String s1="Hello World !!"; String s2=""; int l=s1.length(); for(int i=0;i<l;i++) { if(s1.charAt(i)!=' ') s2=s2+s1.charAt(i); else continue; } System.out.print(s2);
16th Dec 2018, 12:27 PM
Rishi Anand
Rishi Anand - avatar
+ 2
Here we do not give (and is not allowed ask for) solutions ready to be "used" by those who are lazy with homeworks. I dont know what you search, but i like clearify that Happy Coding
16th Dec 2018, 12:57 PM
KrOW
KrOW - avatar
+ 1
Loop on all chars of the string, and build another one copying char by char without including spaces.
16th Dec 2018, 12:11 PM
KrOW
KrOW - avatar
0
Please specify language Not to do on Sololearn Q&A https://code.sololearn.com/W26q4WtwSP8W/?ref=app
16th Dec 2018, 12:11 PM
Gordon
Gordon - avatar
0
// An efficient C++ program to remove all spaces // from a string #include <iostream> using namespace std;    // Function to remove all spaces from a given string void removeSpaces(char *str) {     // To keep track of non-space character count     int count = 0;        // Traverse the given string. If current character     // is not space, then place it at index 'count++'     for (int i = 0; str[i]; i++)         if (str[i] != ' ')             str[count++] = str[i]; // here count is                                    // incremented     str[count] = '\0'; }    // Driver program to test above function int main() {     char str[] = "g  eeks   for ge  eeks  ";     removeSpaces(str);     cout << str;     return 0; } Python
16th Dec 2018, 12:12 PM
Zeshan Ahmed
Zeshan Ahmed - avatar
0
But it will not work if u give a "tab" spaces
17th Jul 2020, 11:27 AM
Shrish Jha
Shrish Jha - avatar