I'm not familiar with java, I'm writing the code in C, may be it can help u...
/*to reverse an entered string*/
#include
main()
{
char a[50],temp;
int i,j=0,k=0;
clrscr();
printf("give the string");
gets(a);
for(i=0;a[i]!=NULL;i++)
{
j++;
}
j--;
while(k<=j)
{
temp=a[k];
a[k]=a[j];
a[j]=temp;
k++;
j--;
}
puts(a);
getch();
}
2006-10-26 20:47:15
·
answer #1
·
answered by Innocence Redefined 5
·
0⤊
0⤋
Copy the string into a character array and read the array from the last element to the first element. Copy each element of the array into another character array inserting the copied element in the next available empty element of the resultant character array as you go. i.e. The last element of the first character array would become the first element of the resulting character array and so forth. This should reverse the string. Once complete, move the contents of the second character array into another string.
For this you will need 2 strings, 2 counters (one decrementing and one incrementing) and two character arrays.
I won't write the code for you since if you are doing a course that requires some coding in Java then I would have thought that you should at least have some coding experience.
Hope this helps
2006-10-26 20:36:22
·
answer #2
·
answered by jools 3
·
0⤊
0⤋
while you're basically prohibited from using Mid and StrLen, you are able to desire to apply the StrRev function. except, of direction, you quite meant which you will't use ANY predefined purposes.
2016-10-16 11:10:52
·
answer #3
·
answered by winstanley 4
·
0⤊
0⤋
Use StringBuffer:
public static void main(String[] args) {
String input = null;
if (args.length > 0){
input = args[0];
} else {
input = "How now brown cow?";
}
StringBuffer sb = new StringBuffer(input);
sb.reverse();
String output = sb.toString();
System.out.println(output);
}
2006-10-30 09:45:44
·
answer #4
·
answered by vincentgl 5
·
0⤊
0⤋