traversing a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting traversing a string
# 1  
Old 08-02-2012
traversing a string

I am writing a script which will read a word and say how many vowels and consonants does the word contain. but i dont know how to traverse a string in shell scripting. if it was in C i'd have done something like this:

Code:
cout<<"plz enter the word"<<endl;
cin>>word;
int consonants, vowels;
for(int i=0;i<word.size();i++)
{

if(word[i] is a consonant)
consonants ++;
else
vowels++;

}

but i have no idea how to use for loop like that in shell.
thanks in advance Smilie
# 2  
Old 08-02-2012
Some shells support slicing a variable's value. If yours does not, you can always print one character per line, grep and count.

An example for the vowel count:
Code:
printf '%s\n' "$word" | fold -bw1 | grep -c '[aeiou]'

If you specify which shell you're using, you may get more specific suggestions.

By the way, that's C++, not C (a nitpick, I know Smilie).

Regards,
Alister
# 3  
Old 08-02-2012
Depends which shell. Some shells have substring operators, some don't. Some have for-loops, some don't.

In bash or a new-enough ksh:

Code:
for ((N=0; N<${#VAR}; N++))
do
        echo "${VAR:$N:1}"
done

See the String Operations chart in the Advanced Bash Scripting Guide. A few of them might be bash-specific but quite a few of them are common to many Bourne shells.
This User Gave Thanks to Corona688 For This Post:
# 4  
Old 08-02-2012
it's bash scripting
Code:
#!/bin/bash

echo "Please enter your word: "
read WORD

consonants=0
vowels=0

that's what i have done so far. Smilie my bad that was c++ not c Smilie

---------- Post updated at 05:21 PM ---------- Previous update was at 04:50 PM ----------

thanks corona Smilie problem solved now Smilie
# 5  
Old 08-02-2012
This should work with any POSIX-conforming shell (assuming you are only counting upper- and lower-case a, e, i, o, and u as vowels and don't care about vowels with diacritical marks):
Code:
while read word
do
	alphas=$(printf "%s\n" "$word"|tr -d -C "[:alpha:]")
	vowels=$(printf "%s\n" "$alphas"|tr -d -C "AEIOUaeiou")
	vc=${#vowels}
	cc=$(( ${#alphas} - vc ))
	if [ $cc -eq 1 ]
	then cplural=""
	else cplural="s"
	fi
	if [ $vc -eq 1 ]
	then vplural=""
	else vplural="s"
	fi
	printf "\"%s\" contains %d vowel%s and %d consonant%s\n" \
		"$word" $vc "$vplural" $cc "$cplural"
	printf "Enter another word (ctl-D to quit):"
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Traversing member of structure of vector C++

Hello, I want to loop thru a vector composed of many entries as structure, which contains sequenceID and sequence. At looping, delete any structure if the sequence is a perfect-match substring of another sequence of any other structure, so that the resulted vector contains only unique sequences.... (1 Reply)
Discussion started by: yifangt
1 Replies

2. Programming

How to reverse a linked list by traversing only once.?

Program to reverse a linked list by traversing only once. (1 Reply)
Discussion started by: VSSajjan
1 Replies

3. Homework & Coursework Questions

Problem while traversing directories

I was given to create a backup of all files in a given directory(command line argument) into say /home/vishal/back and the back up files must be accordingly to the extension of the file i.e pdf files are saved in back/pdf doc files back/doc etc . I gave a recursive function to traverse through the... (1 Reply)
Discussion started by: davis7son
1 Replies

4. Programming

Traversing in Array of pointers

Please find the below program. the requirement and description of the program also given: ganesh@ubuntu:~/my_programs/c/letusc/chap9$ cat fa.c.old /* Program : write a program to count the number of 'e' in thefollowing array of pointers to strings: char *s = { "We will teach you how... (12 Replies)
Discussion started by: ramkrix
12 Replies

5. Shell Programming and Scripting

Find directory name while traversing subdirectories

Hi, I have a parent directory in which I have sub directories of different depth /usr/usr1/user2/671 /usr/usr1/672 /usr/user2/user1/673 /usr/user2/user3/user4/674 And I need the names of all the directories that which starts only with 6 in a file. Thanks, (12 Replies)
Discussion started by: arun_maffy
12 Replies

6. UNIX for Dummies Questions & Answers

script for traversing directory

hi please suggest a korn script which will traverse all subdirectory in the current directory? (2 Replies)
Discussion started by: ilayans
2 Replies

7. Programming

Vector Traversing

Hi i have the following structure struct S { char Mod_num; char val; char chr_nm_cd; } I am reading a 2GB file and inserting into the structure and writing into a vector. I feel like only vector will be a right option. I tried with multimap but it is memory intensive and hence i... (1 Reply)
Discussion started by: dhanamurthy
1 Replies

8. UNIX for Dummies Questions & Answers

Traversing Pascal/Delphi code using GVim

Hi, I am using GVim as editor... and i am viewing code of pascal/ delphi.. my problem is its difficult to use GVim as source code browser.... normally ctags helps to traverse in vim for c/c++.. is there anything like that for pascal/ delphi to minimise this complication of each... (0 Replies)
Discussion started by: SankarV
0 Replies

9. Shell Programming and Scripting

Traversing thru dirs and deleting files based on date

Hi Folks I am pretty new to unix and shellscripting. I need help on writing logic on traversing recursively through a set of directories under a top-level folder and delete files(mostly text) which are 1 month old. Can you people help me on this? Thanks a lot Ravi (5 Replies)
Discussion started by: ravi2082
5 Replies

10. UNIX for Dummies Questions & Answers

Traversing a file system

I'm pretty new at this UNIX stuff, and this may be a simple question but I'm kind of stuck :confused: Let's say I have a large directory structure of .essay files, where I saved all of the essays that I did over the last few years. Not all of the .essay files are in the same directory (all... (1 Reply)
Discussion started by: hooj
1 Replies
Login or Register to Ask a Question