+ 1
How to remove spaces in a string without using any inbuilt methods or function in java
6 Réponses
+ 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);
+ 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
+ 1
Loop on all chars of the string, and build another one copying char by char without including spaces.
0
Please specify language
Not to do on Sololearn Q&A
https://code.sololearn.com/W26q4WtwSP8W/?ref=app
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
0
But it will not work if u give a "tab" spaces