Print string as x pattern

General FreeBASIC programming questions.
Post Reply
ganache
Posts: 47
Joined: Aug 04, 2016 9:25

Print string as x pattern

Post by ganache »

My attempt

Code: Select all

Dim As  String s
Dim  As  Integer i,  j
Input  " enter string with odd no of characters:",s
For i = 0 to len(s)-1
For j = 0 to len(s) - 1
If i  = j or i + j  =len(s) - 1  then
Print chr(i),  ' semicolon 
Else 
Print " ", 
End if
Next j
Next i
Sleep
There will be a point of intersection ( if i = j)
But how to print the relevant character of the string. I tried
Converting between ascii values and char but it doesn't seem to work.
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: Print string as x pattern

Post by srvaldez »

is this what you are after?

Code: Select all

'Print string of odd length in ‘X’ format
'https://www.geeksforgeeks.org/print-string-of-odd-length-in-x-format/
'transllated to FB by srvaldez
Sub printPattern(Byval s As String, Byval length As Long)
      
    ' i and j are the indexes of characters  
    ' to be displayed in the ith iteration 
    ' i = 0 intially and go upto length of 
    ' string 
    ' j = length of string initially  
    ' in each iteration of i, we increment 
    ' i and decrement j, we print character 
    ' only of k==i or k==j 
    Dim As Long j
    For i As Long = 0 To length-1 
        j = length -1 - i
        For k As Long = 0 To length-1
            If k = i Or k = j Then 
                Print Chr(s[k]); 
            Else
                Print " ";
            End If
        Next 
        Print        
    Next
End Sub 

printPattern("ganache", Len("ganache"))
output

Code: Select all

g     e
 a   h
  n c
   a
  n c
 a   h
g     e
ganache
Posts: 47
Joined: Aug 04, 2016 9:25

Re: Print string as x pattern

Post by ganache »

Thanks, srvaldez. The sub and printPattern seem to be throwing up a few errors. But anyways, thanks. I'll see if I can resolve the issues.
Post Reply