Jump to content

Unable to get desired output

- - - - -

  • Please log in to reply
4 replies to this topic

#1
arunjib

arunjib

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
i cannot find out the error in output
Input : arunjib
required output : ArunjiB
output getting : ABarunjib
please help me

public class caps

{

    public static void func(String x)

    {

int i, len;

char y;

String z, x1;

z=" ";

x1=" ";

len=x.length();

for(i=0;i<len;i++)

{

y=x.charAt(i);

if(i==0 || i==len-1)

{

z=Character.toString(y);  // converting character to string

x1=z.toUpperCase();   // converting string to upper case

x=x1+x;

}

}

System.out.println(x);

}

}


#2
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US

arunjib said:

if(i==0 || i==len-1)

{

z=Character.toString(y);  // converting character to string

x1=z.toUpperCase();   // converting string to upper case

x=x1+x;

}

It's the 'x=x1+x;' . What that does, is it appends a leading x1 to the beginning of the string; that is, I'm sure, not what you want.

Try something more like this:
[COLOR=#8888FF]char a []; // or however you declare an array of characters in Java [/COLOR]

if(i==0 || i==len-1)

{

z=Character.toString(y);  // converting character to string

x1=z.toUpperCase();   // converting string to upper case

[COLOR=#8888FF]a[i]= x1;     // Use the value we just came up with. [/COLOR]

} else { 

[COLOR=#8888FF]a[i]= x[i];   // Just use the default value of what it was. [/COLOR]

} 


#3
arunjib

arunjib

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
i cannot understand

RhetoricalRuvim said:

It's the 'x=x1+x;' . What that does, is it appends a leading x1 to the beginning of the string; that is, I'm sure, not what you want.

Try something more like this:
[COLOR=#8888FF]char a []; // or however you declare an array of characters in Java [/COLOR]

if(i==0 || i==len-1)

{

z=Character.toString(y);  // converting character to string

x1=z.toUpperCase();   // converting string to upper case

[COLOR=#8888FF]a[i]= x1;     // Use the value we just came up with. [/COLOR]

} else { 

[COLOR=#8888FF]a[i]= x[i];   // Just use the default value of what it was. [/COLOR]

} 


#4
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
Which part(/s) don't you understand?

x=x1+x is the same as assigning x1 to x, then appending the former x to that. In other words, let's say x is equal to 'abcd' ; let x1 be '1234' ; `x=x1+x` would first make x be '1234' , because that's what x1 is, and then it would append 'abcd' to that, because that's what x was; so x would be x1+x = '1234'+'abcd' = '1234abcd' .

What I'm saying is why not just have a separate array of characters, where we would save the capitalized version of the string's character - if it's the first or the last character - or just copy the corresponding character in the string to the array - otherwise.

By the way, I did make a bit of a mistake in the code; it's supposed to be this:
[COLOR=#8888FF]char a []; // or however you declare an array of characters in Java [/COLOR]

if(i==0 || i==len-1)

{

z=Character.toString(y);  // converting character to string

x1=z.toUpperCase();   // converting string to upper case

[COLOR=#8888FF]a[i]= x1;     // Use the value we just came up with. [/COLOR]

} else { 

[COLOR=#8888FF]a[i]= x[/COLOR][COLOR=#FF1111].charAt (i)[/COLOR][COLOR=#8888FF];   // Just use the default value of what it was. [/COLOR]

}

And then you'll have an array, a [], after that code, with all the characters for the new string; I'm not sure how to join those together into a string, but I think it's either a split () or an implode () method.

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
In my opinion, using loops would make this more complicated than without.
    public static void func(String x) {
        if(x==null || x.length() == 0) {
            return;
        }
        x = x.substring(0,1).toUpperCase() + x.substring(1, x.length()-1) + x.substring(x.length()-1).toUpperCase();
        System.out.println(x);
    }



------
From char array to String is just:
char[] chars;
...
String result = new String(chars);





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users