Determine if a string has all the same characters: Difference between revisions

Content added Content deleted
Line 291: Line 291:


=={{header|BASIC}}==
=={{header|BASIC}}==
==={{header|GWBASIC}}===
Works with BASICA
<lang GWBASIC>
10 'SAVE"SAMECHAR", A
20 DEFINT A-Z
30 DATA ""," ","2","333",".55","tttTTT","4444 444k", "FIN"
40 ' Main program cycle
50 CLS
60 PRINT "Program SameChar"
70 PRINT "Determines if a string has the same character or not."
80 PRINT
90 WHILE S$<>"FIN"
100 READ S$
110 IF S$="FIN" THEN 150
120 GOSUB 190 ' Revision subroutine
130 PRINT "'";S$;"' of length";LEN(S$);
140 IF I<2 THEN PRINT "contains all the same character." ELSE PRINT "is different at possition";STR$(I);": '";DC$; "' (0x"; HEX$(ASC(DC$)); ")"
150 WEND
160 PRINT
170 PRINT "End of program run."
180 END
190 ' DifChar subroutine
200 C$ = LEFT$(S$,1)
210 I = 1
220 DC$=""
230 WHILE I<LEN(S$) AND DC$=""
240 IF MID$(S$,I,1)<>C$ THEN DC$=MID$(S$,I,1) ELSE I=I+1
250 WEND
260 IF DC$="" THEN I=1
270 RETURN</lang>
{{out}}
<pre>
Program SameChar
Determines if a string has the same character or not.

'' of length 0 contains all the same character.
' ' of length 3 contains all the same character.
'2' of length 1 contains all the same character.
'333' of lenght 3 contains all the same character.
'.55' of length 3 is different at possition 2: '5' (0x35)
'tttTTT' of length 6 is different at possition 4: 'T' (0x54)
'4444 444k' of length 9 is different at possition 5: ' ' (0x20)

End of program run.
</pre>

==={{header|QuickBASIC}}===
==={{header|QuickBASIC}}===
Works with QBASIC, VB-DOS, PDS 7.x
Works with QBASIC, VB-DOS, PDS 7.x