write a program to reverse a string without using array?

2008-12-15 7:53 am

回答 (6)

2008-12-15 8:05 am
✔ 最佳答案
Assuming you're using Java:

Read the string one letter at a time from the last letter to the first using a for loop, a control variable, and the .length() method. Each iteration, concatenate each letter to the new (backwards) string using the charAt() method. Your for loop should take this form:

for(int x = string1.length()-1; x>=0; x--){

string2=string2+string1.charAt(x);

}
2008-12-15 9:02 am
Hi,
If u r using C then include string.h header file, then use strrev(s); function to reverse the string pointed to by s. s is a char pointer.
char *s;
strrev(s);
2008-12-15 8:35 am
Another way to archive the same results without any logic implementation:

String strA = "apple";
StringBuffer strBuf = new StringBuffer(strA);
strA = strBuf.reverse().toString();
System.out.println(strA);
2017-01-19 7:11 pm
1
2016-10-17 9:58 am
that's all approximately swapping. the least puzzling change feels like this: c = a; a = b; b = c; This swaps archives between 'a' and 'b'. (There are different techniques of swapping that don't use a 0.33 variable, yet enable's depart them out for now.) So what you want to do is desperate up a for loop that is going a million/2 way in the process the array: for (int i = 0; i < arraySize / 2; i++) { int swapstorage; //exchange from int as suited swapstorage = array[i]; array[ararySize - i] = array[i]; array[i] = swapstorage } reliable success.
2008-12-15 8:50 am
Hello;

Each character of the string must be referenced using the string commands, and a for next loop. You can use things like

length of string
midstring
and a simple for next loop (reverse counting) to make it work

test$ = 'TEST'
for x = len(X$) to 1 step -1
individual_char = midstring(test$,x,1)
print individual_char
next x

In this case the midstring is (stringname, position in sting from left, number of characters).

I would point out though that technically a string is considered to be an array of characters in most languages.

Hope that helps,

Bill


收錄日期: 2021-05-01 01:05:03
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20081214235353AANNZFO

檢視 Wayback Machine 備份