Avatar of yjchong514
yjchong514Flag for United States of America asked on

String Processing

Dear EE members,

I would like to establish: http://codingbat.com/prob/p121596

Here is my code:
public String altPairs(String str) { //01 23 45 67 89 i=0,1 i=i+4,i+5
  String newStr = "";
  int counter = 0;
 
  while(counter<newStr.length()){
    if(counter==0||counter==1) newStr += str.charAt(0);  
   
    counter=counter+4;
    newStr += str.charAt(counter);
  }  
 
  return newStr;
}

Regards,

yjchong514
Java

Avatar of undefined
Last Comment
yjchong514

8/22/2022 - Mon
Peter Kwan

Your code have several problems:

1.
  while(counter<newStr.length())  // This loop will be skipped since newStr.length() is 0 at the beginning

should be

  while(counter<str.length())

2. The logic in the loop could be as follows:

   newStr += str.charAt(counter);
   if (counter+1 < str.length())             // check if the input string has only 1, 5, 9, ... etc  characters
            newStr += str.charAt(counter);
   counter = counter + 4;
ASKER
yjchong514


The logic has failed in the test.
for_yan

public String altPairs(String str) {

  String s0="";
int count = 0;

While(true){
if(count == str.length())return s0;
if(count%4 == 0 || count %4 == 1) s0 += str.charAt(count);
count++;

}

return s0;
}
Your help has saved me hundreds of hours of internet surfing.
fblack61
for_yan

while should be of course lower case

public String altPairs(String str) {

  String s0="";
int count = 0;

While(true){
if(count == str.length())return s0;
if(count%4 == 0 || count %4 == 1) s0 += str.charAt(count);


}

return s0;
}
for_yan

Could not test from my mobile device and could not make while lowere case - it for some reason makes it upper case when I click  post
ASKER CERTIFIED SOLUTION
CEHJ

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
yjchong514

Dear for_yan: The second return stmt is unreachable. Perhaps, the code all stuck in infinite loop.
Take your time. :-)

public String altPairs(String str) {
String s0="";
int count = 0;

While(true){
if(count == str.length())return s0;
if(count%4 == 0 || count %4 == 1) s0 += str.charAt(count);


}

return s0;
}

Dear CEHJ:
Yes, the code is working.Thanks !

Regards,

yjchong514
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
for_yan

When pasting it lost count++; line as the last line in the loop
for_yan

public String altPairs(String str) {

  String s0="";
int count = 0;

While(true){
if(count == str.length())return s0;
if(count%4 == 0 || count %4 == 1) s0 += str.charAt(count);

count++;
}

return s0;
}
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
yjchong514

for_yan:
can you explain the purpose of
if(count == str.length())return s0;

thank you.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
yjchong514

Thanks, for_yan.
ASKER
yjchong514

Wish everyone have a nice working days. :-)
CEHJ

Also avoid uncounted loops of the for(;;) or while(true) sort - they make your code less readable and can lead to nasty errors
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
CEHJ

:) There is however, a part of for_yan's code that's superior to mine, and you should adopt it. e.g.
public String altPairs(String str) {
  StringBuilder result = new StringBuilder();
  for(int i = 0;i < str.length();i++) {
      if((i % 4 == 0) || (i % 4 == 1)) {
        result.append(str.charAt(i));
      }
  }
  return result.toString();        
}

Open in new window

ASKER
yjchong514

Thanks !