SlideShare a Scribd company logo
PROGRAMMING
IN
LUA
(FUNCTIONS WITH STRING AND
ARRAY)
INDEX
Program to accept the string and count number of vowels in it.
Program to accept the string and count number words whose first letter is vowel.
Program to accept the string and check it’s a palindrome or not
Program to accept the number from user and search it in an array of 10 numbers using linear search.
Program to accept the numbers in an array unsorted order and display it in selection sort.
Program to swap first element with last, second to second last and so on (reversing elements)
Program to create a function name swap2best() with array as a parameter and swap all the even
index value.
Program to print the left diagonal from a 2D array
Program to swap the first row of the array with the last row of the array
Program to print the lower half of the 2d array.
Program to accept the string and count number of vowels in it.
function vowelcount(sent)
k=string.len(sent)
x=1
count=0
while(x<=k)
do
ch=string.sub(sent,x,x)
if(ch=='a' or ch=='e' or ch=='i' or ch=='u' or ch=='o')
then
count=count+1
end
x=x+1
end
print("no of vowels are = "..count)
end
vowelcount("my first program of vowel")
------------------output---------------------------
no of vowels are = 6
Program to accept the string and count number words whose first letter is vowel.
function vowelcount(sent)
k=string.len(sent)
x=1
count=0
flag=1
while(x<=k)
do
ch=string.sub(sent,x,x)
if(flag==1)
then
ch=string.sub(sent,x,x)
if(ch=='a' or ch=='e' or ch=='i' or ch=='u' or ch=='o')
then
count=count+1
flag=0
end
end
if(ch==' ')
then
x=x+1
ch=string.sub(sent,x,x)
if(ch=='a' or ch=='e' or ch=='i' or ch=='u' or ch=='o')
then
count=count+1
end
end
x=x+1
end
print("no of vowels are = "..count)
end
vowelcount("its my irst program of vowel word in our")
-------------------output-------------------
no of vowels are = 5
Program to accept the string and check it’s a palindrome or not
function checkpal(nm)
k=string.len(nm)
for x=1,math.floor(string.len(nm)/2),1
do
if(string.sub(nm,x,x) == string.sub(nm,k,k))
then
flag=1
else
flag=-1
break
end
k=k-1
end
return flag
end
nm=io.read()
t=checkpal(nm)
if(t==1)
then
print("Its a pal")
else
print("Its not a pal")
end
--------------output----------------
madam
Its a pal
Program to accept the number from user and search it in an array of 10 numbers using linear search.
no={5,10,25,8,6,53,4,9,12,14}
x=1
pos=-1
print("enter the number to search")
search=tonumber(io.read())
while(x<=10)
do
if(no[x]==search)
then
pos=x
break
end
x=x+1
end
if(pos<0)
then
print("no not found")
else
print("no found at "..pos)
end
----------------------output----------------------
enter the number to search
6
no found at 5
Program to accept the numbers in an array unsorted order and display it in selection sort.
no={5,10,25,8,6,53,4,9,12,14}
x=1
tmp=0
print("before sorting")
while(x<=10)
do
io.write(no[x].." ")
x=x+1
end
print()
x=1
while(x<=10)
do
y=x+1
while(y<=10)
do
if(no[x]>no[y])
then
tmp=no[x]
no[x]=no[y]
no[y]=tmp
end
y=y+1
end
x=x+1
end
print("After sorting")
x=1
while(x<=10)
do
io.write(no[x].." ")
x=x+1
end
------------------output----------------------
before sorting
5 10 25 8 6 53 4 9 12 14
After sorting
4 5 6 8 9 10 12 14 25 53
program to swap first element with last, second to second last and so on (reversing elements)
no={5,10,25,8,6,53,4,9,12,14}
x=1
tmp=0
print("Before........")
while(x<=10)
do
io.write(no[x].." ")
x=x+1
end
x=1
print()
y=10
for x=1,math.floor(10/2)
do
tmp=no[x]
no[x]=no[y]
no[y]=tmp
y=y-1
end
print("nAfter........")
x=1
while(x<=10)
do
io.write(no[x].." ")
x=x+1
end
------------------output-------------------------
Before........
5 10 25 8 6 53 4 9 12 14
After........
14 12 9 4 53 6 8 25 10 5
Program to create a function name swap2best() with array as a parameter and swap all the even index value.
function swap2best(no)
x=1
tmp=0
print("Before........")
while(x<=10)
do
io.write(no[x].." ")
x=x+1
end
x=1
print()
for x=1,10,2
do
tmp=no[x]
no[x]=no[x+1]
no[x+1]=tmp
end
print("nAfter........")
x=1
while(x<=10)
do
io.write(no[x].." ")
x=x+1
end
end
no={10,20,30,40,50,60,70,80,90,110}
swap2best(no)
-------------------output--------------------
Before........
10 20 30 40 50 60 70 80 90 110
After........
20 10 40 30 60 50 80 70 110 90
Program to print the left diagonal from a 2D array
function display(no,N)
x=1
tmp=0
print("Before........")
while(x<=N)
do
y=1
while(y<=N)
do
io.write(no[x][y].."t")
y=y+1
end
print()
x=x+1
end
end
function displayleftdiagonal(no,N)
for x=1,N
do
print(no[x][x])
end
end
no={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}}
display(no,4)
displayleftdiagonal(no,4)
------------------output------------------
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
1
6
11
16
Program to swap the first row of the array with the last row of the array
function display(no,R,C)
x=1
tmp=0
print(".....................")
while(x<=R)
do
y=1
while(y<=C)
do
io.write(no[x][y].."t")
y=y+1
end
print()
x=x+1
end
end
function swapfirstwithlastR(no,R,C)
last=R
for x=1,C
do
tmp=no[1][x]
no[1][x]=no[last][x]
no[last][x]=tmp
end
end
no={{1,2,3,4,1,5,7},{5,6,7,8,8,9,4},{9,10,11,12,11,23,5},{13,14,15,16,1,2,8}}
display(no,4,7)
swapfirstwithlastR(no,4,7)
display(no,4,7)
------------------output--------------------
.....................
1 2 3 4 1 5 7
5 6 7 8 8 9 4
9 10 11 12 11 23 5
13 14 15 16 1 2 8
.....................
13 14 15 16 1 2 8
5 6 7 8 8 9 4
9 10 11 12 11 23 5
1 2 3 4 1 5 7
Program to print the lower half of the 2d array.
function display(no,R,C)
x=1
tmp=0
print(".....................")
while(x<=R)
do
y=1
while(y<=C)
do
io.write(no[x][y].."t")
y=y+1
end
print()
x=x+1
end
end
function lowerhalf(no,R,C)
for x=1,R
do
for y=1,C
do
if(x>=y)
then
io.write(no[x][y].." ")
end
end
print()
end
end
no={{1,2,3,4,1,5,7},{5,6,7,8,8,9,4},{9,10,11,12,11,23,5},{13,14,15,16,1,2,8}}
display(no,4,7)
lowerhalf(no,4,7)
-----------------output-------------------------
1 2 3 4 1 5 7
5 6 7 8 8 9 4
9 10 11 12 11 23 5
13 14 15 16 1 2 8
1
5 6
9 10 11
13 14 15 16
Programming in lua STRING AND ARRAY

More Related Content

PDF
Python programming : Standard Input and Output
PDF
7 Habits For a More Functional Swift
PDF
Tuples in Python
PDF
Atomically { Delete Your Actors }
PDF
Map, Reduce and Filter in Swift
PDF
The Ring programming language version 1.7 book - Part 26 of 196
PDF
Programs in array using SWIFT
PDF
Python Cheat Sheet
Python programming : Standard Input and Output
7 Habits For a More Functional Swift
Tuples in Python
Atomically { Delete Your Actors }
Map, Reduce and Filter in Swift
The Ring programming language version 1.7 book - Part 26 of 196
Programs in array using SWIFT
Python Cheat Sheet

What's hot (20)

PPTX
Python programming -Tuple and Set Data type
PPTX
Kotlin collections
PPTX
Groovy puzzlers по русски с Joker 2014
PDF
Introduction to Scala
PDF
PPT
Python легко и просто. Красиво решаем повседневные задачи
PPTX
Iterative1
PDF
ScalaMeter 2014
PDF
Itroroduction to R language
PDF
Scala collections
PDF
Scala Parallel Collections
PPTX
Functional Programming in Swift
PPTX
Kotlin standard
PDF
Python programming : List and tuples
PPTX
Print input-presentation
PDF
Scala Functional Patterns
PDF
Scala. Introduction to FP. Monads
PDF
The Ring programming language version 1.2 book - Part 12 of 84
PDF
Beginners python cheat sheet - Basic knowledge
 
PDF
Are we ready to Go?
Python programming -Tuple and Set Data type
Kotlin collections
Groovy puzzlers по русски с Joker 2014
Introduction to Scala
Python легко и просто. Красиво решаем повседневные задачи
Iterative1
ScalaMeter 2014
Itroroduction to R language
Scala collections
Scala Parallel Collections
Functional Programming in Swift
Kotlin standard
Python programming : List and tuples
Print input-presentation
Scala Functional Patterns
Scala. Introduction to FP. Monads
The Ring programming language version 1.2 book - Part 12 of 84
Beginners python cheat sheet - Basic knowledge
 
Are we ready to Go?
Ad

Similar to Programming in lua STRING AND ARRAY (20)

PPTX
Introduction to python programming ( part-3 )
DOC
Devry cis 170 c i lab 5 of 7 arrays and strings
DOC
Devry cis 170 c i lab 5 of 7 arrays and strings
PDF
C Programming Project
DOCX
COMPUTER SCIENCE CLASS 12 PRACTICAL FILE
PDF
Rexx summary
DOCX
Please Please Please Read the instructions and do everything li.docx
PDF
Python for text processing
PPT
Devry cis-170-c-i lab-5-of-7-arrays-and-strings
PPT
Devry cis-170-c-i lab-5-of-7-arrays-and-strings
PDF
Devry cis 170 c i lab 5 of 7 arrays and strings
PDF
C Programming Interview Questions
PPTX
LIST IN PYTHON-PART 4[SEARCHING IN LIST]
PDF
pyton Notes9
PDF
ProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdf
PDF
07-Strings( string Functions and parameters C).pdf
PPTX
Python_Unit_III.pptx
ODP
Python course Day 1
PPTX
Class 28: Entropy
DOC
Cis 170 c ilab 5 of 7 arrays and strings
Introduction to python programming ( part-3 )
Devry cis 170 c i lab 5 of 7 arrays and strings
Devry cis 170 c i lab 5 of 7 arrays and strings
C Programming Project
COMPUTER SCIENCE CLASS 12 PRACTICAL FILE
Rexx summary
Please Please Please Read the instructions and do everything li.docx
Python for text processing
Devry cis-170-c-i lab-5-of-7-arrays-and-strings
Devry cis-170-c-i lab-5-of-7-arrays-and-strings
Devry cis 170 c i lab 5 of 7 arrays and strings
C Programming Interview Questions
LIST IN PYTHON-PART 4[SEARCHING IN LIST]
pyton Notes9
ProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdf
07-Strings( string Functions and parameters C).pdf
Python_Unit_III.pptx
Python course Day 1
Class 28: Entropy
Cis 170 c ilab 5 of 7 arrays and strings
Ad

More from vikram mahendra (20)

PPTX
Communication skill
PDF
Python Project On Cosmetic Shop system
PDF
Python Project on Computer Shop
PDF
PYTHON PROJECT ON CARSHOP SYSTEM
PDF
BOOK SHOP SYSTEM Project in Python
PPTX
FLOW OF CONTROL-NESTED IFS IN PYTHON
PPTX
FLOWOFCONTROL-IF..ELSE PYTHON
PPTX
FLOW OF CONTROL-INTRO PYTHON
PPTX
OPERATOR IN PYTHON-PART1
PPTX
OPERATOR IN PYTHON-PART2
PPTX
USE OF PRINT IN PYTHON PART 2
PPTX
DATA TYPE IN PYTHON
PPTX
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
PPTX
USER DEFINE FUNCTIONS IN PYTHON
PPTX
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
PPTX
INTRODUCTION TO FUNCTIONS IN PYTHON
PPTX
Python Introduction
PPTX
GREEN SKILL[PART-2]
PPTX
GREEN SKILLS[PART-1]
PPTX
Dictionary in python
Communication skill
Python Project On Cosmetic Shop system
Python Project on Computer Shop
PYTHON PROJECT ON CARSHOP SYSTEM
BOOK SHOP SYSTEM Project in Python
FLOW OF CONTROL-NESTED IFS IN PYTHON
FLOWOFCONTROL-IF..ELSE PYTHON
FLOW OF CONTROL-INTRO PYTHON
OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART2
USE OF PRINT IN PYTHON PART 2
DATA TYPE IN PYTHON
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
INTRODUCTION TO FUNCTIONS IN PYTHON
Python Introduction
GREEN SKILL[PART-2]
GREEN SKILLS[PART-1]
Dictionary in python

Recently uploaded (20)

PPTX
L1 - Introduction to python Backend.pptx
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Digital Strategies for Manufacturing Companies
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Nekopoi APK 2025 free lastest update
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
Reimagine Home Health with the Power of Agentic AI​
PPT
Introduction Database Management System for Course Database
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
Introduction to Artificial Intelligence
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
System and Network Administraation Chapter 3
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
assetexplorer- product-overview - presentation
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
Computer Software and OS of computer science of grade 11.pptx
L1 - Introduction to python Backend.pptx
PTS Company Brochure 2025 (1).pdf.......
Digital Strategies for Manufacturing Companies
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Nekopoi APK 2025 free lastest update
Navsoft: AI-Powered Business Solutions & Custom Software Development
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Design an Analysis of Algorithms II-SECS-1021-03
Odoo Companies in India – Driving Business Transformation.pdf
Reimagine Home Health with the Power of Agentic AI​
Introduction Database Management System for Course Database
2025 Textile ERP Trends: SAP, Odoo & Oracle
Wondershare Filmora 15 Crack With Activation Key [2025
Introduction to Artificial Intelligence
Design an Analysis of Algorithms I-SECS-1021-03
System and Network Administraation Chapter 3
Operating system designcfffgfgggggggvggggggggg
assetexplorer- product-overview - presentation
Upgrade and Innovation Strategies for SAP ERP Customers
Computer Software and OS of computer science of grade 11.pptx

Programming in lua STRING AND ARRAY

  • 2. INDEX Program to accept the string and count number of vowels in it. Program to accept the string and count number words whose first letter is vowel. Program to accept the string and check it’s a palindrome or not Program to accept the number from user and search it in an array of 10 numbers using linear search. Program to accept the numbers in an array unsorted order and display it in selection sort. Program to swap first element with last, second to second last and so on (reversing elements) Program to create a function name swap2best() with array as a parameter and swap all the even index value. Program to print the left diagonal from a 2D array Program to swap the first row of the array with the last row of the array Program to print the lower half of the 2d array.
  • 3. Program to accept the string and count number of vowels in it. function vowelcount(sent) k=string.len(sent) x=1 count=0 while(x<=k) do ch=string.sub(sent,x,x) if(ch=='a' or ch=='e' or ch=='i' or ch=='u' or ch=='o') then count=count+1 end x=x+1 end print("no of vowels are = "..count) end vowelcount("my first program of vowel") ------------------output--------------------------- no of vowels are = 6
  • 4. Program to accept the string and count number words whose first letter is vowel. function vowelcount(sent) k=string.len(sent) x=1 count=0 flag=1 while(x<=k) do ch=string.sub(sent,x,x) if(flag==1) then ch=string.sub(sent,x,x) if(ch=='a' or ch=='e' or ch=='i' or ch=='u' or ch=='o') then count=count+1 flag=0 end end if(ch==' ') then x=x+1 ch=string.sub(sent,x,x) if(ch=='a' or ch=='e' or ch=='i' or ch=='u' or ch=='o') then count=count+1 end end x=x+1 end print("no of vowels are = "..count) end vowelcount("its my irst program of vowel word in our") -------------------output------------------- no of vowels are = 5
  • 5. Program to accept the string and check it’s a palindrome or not function checkpal(nm) k=string.len(nm) for x=1,math.floor(string.len(nm)/2),1 do if(string.sub(nm,x,x) == string.sub(nm,k,k)) then flag=1 else flag=-1 break end k=k-1 end return flag end nm=io.read() t=checkpal(nm) if(t==1) then print("Its a pal") else print("Its not a pal") end --------------output---------------- madam Its a pal
  • 6. Program to accept the number from user and search it in an array of 10 numbers using linear search. no={5,10,25,8,6,53,4,9,12,14} x=1 pos=-1 print("enter the number to search") search=tonumber(io.read()) while(x<=10) do if(no[x]==search) then pos=x break end x=x+1 end if(pos<0) then print("no not found") else print("no found at "..pos) end ----------------------output---------------------- enter the number to search 6 no found at 5
  • 7. Program to accept the numbers in an array unsorted order and display it in selection sort. no={5,10,25,8,6,53,4,9,12,14} x=1 tmp=0 print("before sorting") while(x<=10) do io.write(no[x].." ") x=x+1 end print() x=1 while(x<=10) do y=x+1 while(y<=10) do if(no[x]>no[y]) then tmp=no[x] no[x]=no[y] no[y]=tmp end y=y+1 end x=x+1 end print("After sorting") x=1 while(x<=10) do io.write(no[x].." ") x=x+1 end ------------------output---------------------- before sorting 5 10 25 8 6 53 4 9 12 14 After sorting 4 5 6 8 9 10 12 14 25 53
  • 8. program to swap first element with last, second to second last and so on (reversing elements) no={5,10,25,8,6,53,4,9,12,14} x=1 tmp=0 print("Before........") while(x<=10) do io.write(no[x].." ") x=x+1 end x=1 print() y=10 for x=1,math.floor(10/2) do tmp=no[x] no[x]=no[y] no[y]=tmp y=y-1 end print("nAfter........") x=1 while(x<=10) do io.write(no[x].." ") x=x+1 end ------------------output------------------------- Before........ 5 10 25 8 6 53 4 9 12 14 After........ 14 12 9 4 53 6 8 25 10 5
  • 9. Program to create a function name swap2best() with array as a parameter and swap all the even index value. function swap2best(no) x=1 tmp=0 print("Before........") while(x<=10) do io.write(no[x].." ") x=x+1 end x=1 print() for x=1,10,2 do tmp=no[x] no[x]=no[x+1] no[x+1]=tmp end print("nAfter........") x=1 while(x<=10) do io.write(no[x].." ") x=x+1 end end no={10,20,30,40,50,60,70,80,90,110} swap2best(no) -------------------output-------------------- Before........ 10 20 30 40 50 60 70 80 90 110 After........ 20 10 40 30 60 50 80 70 110 90
  • 10. Program to print the left diagonal from a 2D array function display(no,N) x=1 tmp=0 print("Before........") while(x<=N) do y=1 while(y<=N) do io.write(no[x][y].."t") y=y+1 end print() x=x+1 end end function displayleftdiagonal(no,N) for x=1,N do print(no[x][x]) end end no={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}} display(no,4) displayleftdiagonal(no,4) ------------------output------------------ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 6 11 16
  • 11. Program to swap the first row of the array with the last row of the array function display(no,R,C) x=1 tmp=0 print(".....................") while(x<=R) do y=1 while(y<=C) do io.write(no[x][y].."t") y=y+1 end print() x=x+1 end end function swapfirstwithlastR(no,R,C) last=R for x=1,C do tmp=no[1][x] no[1][x]=no[last][x] no[last][x]=tmp end end no={{1,2,3,4,1,5,7},{5,6,7,8,8,9,4},{9,10,11,12,11,23,5},{13,14,15,16,1,2,8}} display(no,4,7) swapfirstwithlastR(no,4,7) display(no,4,7) ------------------output-------------------- ..................... 1 2 3 4 1 5 7 5 6 7 8 8 9 4 9 10 11 12 11 23 5 13 14 15 16 1 2 8 ..................... 13 14 15 16 1 2 8 5 6 7 8 8 9 4 9 10 11 12 11 23 5 1 2 3 4 1 5 7
  • 12. Program to print the lower half of the 2d array. function display(no,R,C) x=1 tmp=0 print(".....................") while(x<=R) do y=1 while(y<=C) do io.write(no[x][y].."t") y=y+1 end print() x=x+1 end end function lowerhalf(no,R,C) for x=1,R do for y=1,C do if(x>=y) then io.write(no[x][y].." ") end end print() end end no={{1,2,3,4,1,5,7},{5,6,7,8,8,9,4},{9,10,11,12,11,23,5},{13,14,15,16,1,2,8}} display(no,4,7) lowerhalf(no,4,7) -----------------output------------------------- 1 2 3 4 1 5 7 5 6 7 8 8 9 4 9 10 11 12 11 23 5 13 14 15 16 1 2 8 1 5 6 9 10 11 13 14 15 16