0
public class RemoveSpaceFromString {

    public static void main(String args[])
    {
        Scanner s1=new Scanner(System.in);
        String str="";
        System.out.println("enter the string");
        str=s1.nextLine();
        int l=str.length();
        char ch[]=str.toCharArray();

        for(int i=0;i<l;i++)
        {
            if(ch[i]==' ')
            {
                for(int k=i;k<l-1;k++)
                {
                    ch[k]=ch[k+1];
                }
                l--;
                i--;
            }
        }
        String str2=new String(ch);
        System.out.println(str2);
    }
}

ouptput :

enter the string
my name is abc
mynameisabcccc

how to remove the extra 'c' from the end

13
  • 4
    The question in title doesnt match the question in the post. Do you wanna remove whitespace or the c? Commented Mar 5, 2018 at 14:31
  • 1
    What's wrong with using str.replaceAll()...? Commented Mar 5, 2018 at 14:32
  • 1
    Do you have to work on char array? String class provides replace method which does what you want. Commented Mar 5, 2018 at 14:32
  • 1
    @Sedrick replaceAll uses regex. Your example doesn't need that, replace(" ", "") will do the same (it also replaces all occurrences of " " with "" despite the lack of All suffix). Commented Mar 5, 2018 at 14:37
  • 3
    @Dan Are you reading the code or only the output?? Trimming is only necessary because the logic of the loop is wrong Commented Mar 5, 2018 at 14:37

2 Answers 2

1

just use

str = s1.nextLine().replaceAll(" ", "");;

I saw that @cricket already pointed that out in the comments of this question! He also suggested using regex like this:

str = str.replaceAll("\\s", "");   // for only one white space
str = str.replaceAll("\\s+", "");  // for multiple white spaces

But to answer your original question how to remove white spaces from a string by using a character array:

In you original example you shift each char-element to the right, but the last element always stays the same! So if you want to get the String again of this char-array, you would have to decrement the length by the number it got smaller (due to the removed white spaces)!

Here's the code (look at the penultimate line (the second last line)):

    Scanner s1=new Scanner(System.in);

    System.out.println("enter the string");
    String str=s1.nextLine();
    int l=str.length();

    char ch[]=str.toCharArray();


    for(int i=0;i<l;i++)
    {
        if(ch[i]==' ')
        {
            for(int k=i;k<l-1;k++)
            {
                ch[k]=ch[k+1];
            }
            l--;
            i--;
        }
    }
    String str2=new String(ch,0,l);
    System.out.println(str2);
Sign up to request clarification or add additional context in comments.

4 Comments

...or str = s1.nextLine().replace(" ", ""); since we don't really need regex syntax support.
But assuming multiple spaces are possible replaceAll("\\s+", "") is more efficient
ch.length-(ch.length-l) or just l
@Nahuel Haha yes I am sorry
0

Just instead of

String str2 = new String(ch);

do

String str2 = new String(ch, 0, l);

Or reduce all to

String str2 = str.replace(" ", "");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.