Program to find palindrome from a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Program to find palindrome from a file
# 1  
Old 02-11-2009
Program to find palindrome from a file

hello i came up with this ..The problematic part that i am facing is in underline and bold format.

Code:
exec<file
while read line
do
	set $line
	i=1
	while [ $i -le $# ]
	do
		str=$i  #problematic i want the positional  parameter but here it takes integer value.
		p=1
		q=`expr length $str`
		flag=1
		while [ $p -lt $q ]
		do
		if [ `expr substr $str $p 1` != `expr substr $str $q 1` ]
			then
				flag=0
				break
			fi
			p=`expr $p + 1`
			q=`expr $q - 1`

		done
		if [ $flag -eq 1 ]
		then
			echo $str
		fi
		i=`expr $i + 1`
		
	done
done

Although instead of
Code:
 while [ $i -le $# ]

i could have used
Code:
 for i in $*

. But still what if i want to do it the above way? There must be a way?

Last edited by salman4u; 02-11-2009 at 07:58 AM..
# 2  
Old 02-11-2009
man rev

Code:
#!/bin/ksh
# Find palindromes
cat filename|while read LINE
do
        REVERSE=`echo "${LINE}"|rev`
        if [ "${LINE}" = "${REVERSE}" ]
        then
                echo "Palidrome: ${LINE}"
        fi
done

# 3  
Old 02-11-2009
thanks methyl !
but it would be more helpful to me if you could help me solve the problem i mentioned above! How to distinguish between a variable holding variable and one holding positional parameters?
# 4  
Old 02-11-2009
I guess that you have more than one word per input line in your data.

See "shift" in the man page for your shell. It's a way to work your way along positional parameters one-by-one.

There are easier methods but I now recognise that you are trying out shell commmands.
# 5  
Old 02-11-2009
i know i have to use shift if i have more than 9 words per line but that's not an issue i will do it once i know how to access positional parameters. see the below given line

Code:
str=$i

when i use this $i i get an integer value of loop counter instead of the the strings of file. My only and only question is how to access those strings
# 6  
Old 02-11-2009
Try:
eval str=\$$i
# 7  
Old 02-11-2009
thanks Perderabo Smilie you did my job. Methyl was giving me all kind of answers except for the one which i wanted to know
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Where can I find the program that runs the -wc command?

Hey guys, I was wondering. When I enter a command in the terminal -wcl for a word count, where is that program located in the kernel? (7 Replies)
Discussion started by: Circuits
7 Replies

2. AIX

how to find which program that update a specific file

Hello Would you tell me how to find which program that update a specific file? I am implementing migration project. My machines OS are AIX. It is because lack of documentation, some program cannot working properly on new machine. We found the root cause of this problem is that some data... (4 Replies)
Discussion started by: cstsang
4 Replies

3. Shell Programming and Scripting

Perl program for find one entry and put in different file

Hi I have a file name1 xxxxx name1 xxxxx name1 yyyyy name 1 zzzzz name1 Uniprot Id 1234 name2 sssss name2 eeeee name2 bengamine name2 Uniprot Id 3456 ......................and so on I have to capture Uniprot IDs only in a separate file so that output contain only ... (20 Replies)
Discussion started by: manigrover
20 Replies

4. Shell Programming and Scripting

How to find the matched numbers between 2 text file using perl program??

hi dudes, I nee you kind assistance, I have to find the matched numbers from 2 text files and output of matched numbers should be in another text file.. I do have text files like this , for example File 1 787 665*5-p 5454 545-p 445-p 5454*-p File 2 5455 787 445-p 4356 2445 144 ... (3 Replies)
Discussion started by: sureshraj
3 Replies

5. UNIX for Dummies Questions & Answers

Find the directory in which a program is running

Hi, I'm wondering if this is possible. I run several FORTRAN programs in different directories that have the same name (say program_X). I use the command 'top' to find the programs running and the time taken. I'm wondering if it's possible to the directory in which these programs are... (1 Reply)
Discussion started by: lost.identity
1 Replies

6. Shell Programming and Scripting

Please can you help me find a solution in this program in unix ?

Write shell script to read 3 numbers and print them in revers and print wither they are polyndrome numbers or not ????? (2 Replies)
Discussion started by: lovelorn_mn
2 Replies

7. Shell Programming and Scripting

find function name in a program file

Hello All, Is there any way to find a function name in a program file using perl. for example, there is a file called Test.C in that . . void function1(..) { <some code> } int function2(..) { . . sam() /*RA abc100*/ ... .. .. xyz()/*RA abc201*/ .. (8 Replies)
Discussion started by: Parthiban
8 Replies

8. Shell Programming and Scripting

Program to find CPU,memory and I/O utilization

Can anyone please help me regarding this .sh script: The shell script should monitor CPU,memory and I/O utilization continuously after a defined time interval and should write it in an output file like excell. (1 Reply)
Discussion started by: Subhayan
1 Replies

9. UNIX for Dummies Questions & Answers

Palindrome

Hi, Is there a way to check a word for a palindrome in Unix? Thanks. (1 Reply)
Discussion started by: guelpth
1 Replies

10. Shell Programming and Scripting

check for a palindrome

how do i check whether an input string is a palindrome or not ? (1 Reply)
Discussion started by: doubtful
1 Replies
Login or Register to Ask a Question