Jump to content
Sign in to follow this  
Madi

Need to move white spaces around text string in both sides

Recommended Posts

Need your help please.

I am trying to move the preceding/trailing spaces/dots from one side of a text string to the other side.


There are several ways to remove spaces from the string, but the problem is that I do not want to flip them (copy them to another string in backward order) as this will reverse the letters too. Also, I wont to preserve the spaces between the words inside the string.


- I wish to move the spaces from one side to the other.

- The order of letters in the string must be intact (shouldn't read : GNITSET.TSUJ)

- the spaces/dots between the word have to be kept.

- Faster and neater routine (for long strings)


The following listing -in addition of being clumsy, it will leave one dot/space. (Example of moving spaces/dots from left to right)


10 DIM S$(20),D$(20)

20 S$="........JUST.TESTING"

100 I=LEN(S$)

110 POSITION 4,10:? S$,1

120 X=1:V=1

130 IF S$(X,X+1)<>".." THEN D$(V,V)=S$(X,X+1):V=V+1

135 X=X+1

140 IF X=I THEN D$(LEN(D$)+1)=S$(X):GOTO 160

150 GOTO 130

160 FOR X=LEN(D$)+1 TO 20:D$(X,X)=".":NEXT X

170 POSITION 4,12:? D$,LEN(D$)

2q2lqwj.jpg


Thank you

madi


Edit: I managed to do it with the addition of another 3 IF/Then lines but still full of junk codes.

Share this post


Link to post
Share on other sites

All you need to do is find the start of the first word (non-space).

Copy that to a temp string then remove the leading spaces.

Then add the temp string to that.

 

Whatever way you go about it, there'll be tests and flags needed to cater for certain types of cases like no leading spaces, no words in the string etc.

The trick is that you probably won't do it in as few operations as you'd think but overall it shouldn't be a complex solution.

Share this post


Link to post
Share on other sites

Hmmm,

 

since I am no programmer, my lazy solution would be to switch between "...just testing" and "just testing...".

So the Basic program would use S1$="just testing..." and S2$="...just testing" and then the program would switch between displaying these two strings. But I am quite sure this is not what you are looking for (but maybe its good for a laugh)... ;-) ;-) ;-)

Edited by CharlieChaplin

Share this post


Link to post
Share on other sites

All you need to do is find the start of the first word (non-space).

Copy that to a temp string then remove the leading spaces.

Then add the temp string to that.

...

Thank you Rybags for your continual support to ALL including newbies :D

I will be able to follow your kind instructions to remove the leading spaces, but, ..

I couldn't visualized it when it comes to the trailing spaces.. :?

Boy.. , you are stuck with a newbie. :(

 

madi

Edited by Madi

Share this post


Link to post
Share on other sites

Thank you Irgendwer for your kind input

I realized that the routine solves the space moving from right to left, but it may not do much when it comes to moving the spaces/dots from left to right without modifications.

May be because of my English, I was not able to explain it correctly.

The routine should be able to do movements of spaces/dots from both sides of the string.

The idea is related to input/writing directions (left to right and right to left) that are expected in English vs Farsi/Arabic

 

Regards

madani

Share this post


Link to post
Share on other sites

I realized that the routine solves the space moving from right to left, but it may not do much when it comes to moving the spaces/dots from left to right without modifications.

 

I thought the code illustrated the exchange principle quite well. However, I've attached the cleaned-up, fully functional version:

 

Code:

post-7778-0-22426400-1416744708_thumb.png

 

Result:

post-7778-0-41842800-1416744667_thumb.png

 

Basic source:

STRSWAP.zip

  • Like 1

Share this post


Link to post
Share on other sites

Trust me Irgendwer. Your code is more than simple and clear and it is fully understandable. It is neat and can be utilized in the string viewing/browsing without changing the original string. For example:

I could rearrange the string for printing viewing purposes (left to right direction and vice versa) and keep the original string intact for further handling.

 

But, as you can see from the following 2 listings , the problem doesn't reside in the manipulation of the string only. The string (S$ in these listings or STRING$ in yours) might have leading/trailing spaces/dots. depending on the typing direction.

 

=======================================================

First listing: The String S$ with leading spaces/dots.

S$: "...............JUST.TESTING"

===========

10 DIM W$(40)

20 DIM S$(40):S$="...............JUST.TESTING"

30 I=1:? S$

40 IF S$(I,I)="." THEN I=I+1

50 IF S$(I,I)<>"." THEN V=I-1:L=LEN(S$):GOTO 70

60 GOTO 40

70 FOR X=I TO LEN(S$):W$(LEN(W$)+1)=S$(X,X):NEXT X

80 FOR X=1 TO V:W$(LEN(W$)+1)=CHR$(46)

:NEXT X

100 ?: ? W$

===========

The result String W$ will have trailing spaces/dots

W$: "JUST.TESTING..............."

 

=======================================================

Second listing: The String S$ with trailing spaces/dots.

S$: "JUST.TESTING........."

===========

10 DIM W$(40)

20 DIM S$(40):S$="JUST.TESTING........." :? S$

30 I=LEN(S$)

40 IF S$(I,I)="." THEN I=I-1

50 IF S$(I,I)<>"." THEN V=I:L=LEN(S$):

GOTO 70

60 GOTO 40

70 FOR X=I+1 TO L:W$(LEN(W$)+1)=S$(X,X):NEXT X

80 FOR X=1 TO V:W$(LEN(W$)+1)=S$(X,X):NEXT X

100 ?: ? W$

===========

The result String W$ will have leading spaces/dots

W$: ".........JUST.TESTING"

 

=======================================================

Finally, after few hours of try/error, I reached to the above listings :D

Although they are noobish, but will do the job.

However it will be nice if some one improves them.

 

Thank you very much for your kind explaining/teaching

 

madi

TRANSFER.zip

Edited by Madi

Share this post


Link to post
Share on other sites

Give this code a try. I can write the second half if you don't want the small challenge of writing it yourself

 

10 DIM S$(40):DIM W$(40)
20 I=0:S$="............JUST.TESTING"
30 I=I+1
40 IF S$(I,I)="." THEN GOTO 30
50 W$=S$(I,LEN(S$))
60 PRINT W$

 

Bob

Share this post


Link to post
Share on other sites
But, as you can see from the following 2 listings , the problem doesn't reside in the manipulation of the string only. The string (S$ in these listings or STRING$ in yours) might have leading/trailing spaces/dots. depending on the typing direction.

 

Sorry, I don't get the difference. Depending on the setting in the 'WLEFT'-flag in my code you can produce the results of both of your routines. If you like to work on a copy, then just supply that copy in STRING$ before 'GOSUB'ing to 1000.

 

Sorry for being unable to fetch you requirements fully.

Edited by Irgendwer

Share this post


Link to post
Share on other sites

 

Sorry, I don't get the difference. Depending on the setting in the 'WLEFT'-flag in my code you can produce the results of both of your routines. If you like to work on a copy, then just supply that copy in STRING$ before 'GOSUB'ing to 1000.

 

Sorry for being unable to fetch you requirements fully.

You are absolutely right.

When I looked at the code with spaces located on the opposite side, I only got dotted lines similar to the following image:

I even changed the value of WLEFT in several compinations and all gave same result. So I concluded that the code only works with one configuration (not both sides). :skull:

r29vsm.jpg

 

After reading your reply, I had to re-examine the code again (Since you are the Pro ;) ).

Please have a look at some images with different WLEFT flags and spaces/dots arrangements. You will notice that the modified STRING$ is being modified again which will give dotted lines even if the WLEFT flag is set correctly:

kdvpxl.jpg

 

t7hfuo.jpg

 

2unxelv.jpg

 

1zbx5jd.jpg

 

Hence, it is clear that the content of the STRING$ must be kept intact before it undergoes another manipulation. implementing a recovery Temporary string does the fix.. as seen in the following image:

9stimq.jpg

 

Conclusion:

I learned several things from this post. One of them, Is not to jump to conclusion. :spidey:

Thank you very much for being very patient and wish you the best :thumbsup: :thumbsup: .

 

madi

Edited by Madi

Share this post


Link to post
Share on other sites
Please have a look at some images with different WLEFT flags and spaces/dots arrangements. You will notice that the modified STRING$ is being modified again which will give dotted lines even if the WLEFT flag is set correctly:

 

Ok, now I see. Contrary to the first version, I optimized the second one under the assumption you have always to shift the chars around. By reestablishing the real exchange pattern from the first version, everything works like you expect:

 

post-7778-0-26483000-1416815487_thumb.png

 

Edit: Alternatively you could introduce a pre-check:

1045 IF EXCPOS=I THEN RETURN

(since there is nothing to swap) or an even more performance friendly pre-check at the beginning of the sub-routine:

999 IF NOT LEN(STRING$) OR STRING$(1,1)<>WSPACE$ RETURN

(then you have to GOSUB 999 or (better) shift the numbers... ;) )

STRSWAP.zip

Edited by Irgendwer

Share this post


Link to post
Share on other sites

Excellent piece of code Irgendwer. It fits my needs.

Actually, I found another use for it >> SORTING..

When space is added unintentionally head of (or after) an input string, the code just moves it to the other side. i.e. (..BEFORE.SORTING...)

becomes (BEFORE.SORTING.....) after the sorting.

I was testing the code for sometime now and it proved its value.

Thank you once again.

madi

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...