1747984 Members
4662 Online
108756 Solutions
New Discussion юеВ

Re: C or script help

 
SOLVED
Go to solution
Kent Ostby
Honored Contributor

C or script help

Hi --

I haven't written a lot of C and I'm looking for help on a particular task.

I want to read in a file and print out a numerical representation of it. In particular, I'd like to have every non-space character represented by a number and then have each word represented by the sum of it's letters.

I'm not concerned what the numbers are that represent the letters (i.e. if there is a routine out there that will do this using some standard, then that's fine).

For instance, if a script used a numbering format of a=1 to z=26 then I'd want the input line of.

hello world

to output as

52 72

I can figure out how to read in lines into a C program okay, the part I don't know how to do is the conversion from a character to a numerical represenation.

If anyone can post a C code or script that will do what I'm looking for, it would be greatly appreciated and you will get points (I've got a 100% record on that).

Best regards,

Oz


"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
11 REPLIES 11
curt larson_1
Honored Contributor

Re: C or script help

how to do is the conversion from a character to a numerical represenation:

doesn't sprintf do that.
or how about the atoi function call

here is a somewhat trival script, i.e every character is mapped to one, or just prints out the number of characters in each word.

cat file |
awk '{
for ( i=1;i<=NF;i++) {
l=length($i);
if ( i = 1 ) printf("%d",l);
else printf(" %d",l);
}
printf("\n",);
}'

you might have to work with that to handle empty lines and multiple spaces, i.e more then one blank in a row.
Victor Fridyev
Honored Contributor

Re: C or script help

Hi,

You can try to work with
od -b:

echo hello |od -b
0000000 150 145 154 154 157 012
0000006

This command gives you octal for each character in the input

HTH
Entities are not to be multiplied beyond necessity - RTFM
Francisco J. Soler
Honored Contributor
Solution

Re: C or script help

Hi Kent, in awk you can make a script like this:
-------------------- s.awk ---------------
BEGIN {
v["a"]=1; v["b"]=2; v["c"]=3; v["d"]=4; v["e"]=5
v["f"]=6; v["g"]=7; v["h"]=8; v["i"]=9; v["j"]=10
v["k"]=11; v["l"]=12; v["m"]=13; v["n"]=14; v["o"]=15
v["p"]=16; v["q"]=17; v["r"]=18; v["s"]=19; v["t"]=20
v["u"]=21; v["v"]=22; v["w"]=23; v["x"]=24; v["y"]=25
v["z"]=26;
v["0"]=27; v["1"]=28; v["2"]=29; v["3"]=30; v["4"]=31
v["5"]=32; v["6"]=33; v["7"]=34; v["8"]=35; v["9"]=36
w=0
}

{
for (i=1;i<=NF;i++) {
for (j=1;j<=length($i);j++) {
c=substr($i,j,1)
w+=v[c]
}
printf("%d ",w) ; w=0
}
printf("\n")
}
---------- end s.awk -----------

You can do now, awk -f s.awk file

nevertheless, in C code the ascii value of a character is 'c' for example printf("%d",'0') gives you the number 48

Frank.
Linux?. Yes, of course.
curt larson_1
Honored Contributor

Re: C or script help

to use the ascii values with francisco's awk script I used this for the begin

BEGIN {
for (i=0;i<256;i++){
a=sprintf("%c",i);
v[a]=i;

and if you'd like to do the same thing as a script:

cat d1|
while read line
do
set -A var $line
numWords=${#var[*]}
x=0
while (( x < $numWords ))
do
word="${var[$x]}"
lengthWord=${#word}
count=0
typeset -L1 char
y=0
while (( $y < lengthWord ))
do
if (( $y == 0 )) ;then
char=$word
else
eval typeset -L$y start
start=$word
last=${word#$start}
char=$last
fi
printf "%d\n" $char | read value
((count=$count+value))
(( y = $y +1))
done
if (( $x == 0 )) ;then
print "$count\c"
else
print -- " $count\c"
fi
(( x = $x +1))
done
print
done
curt larson_1
Honored Contributor

Re: C or script help

and for a little C code

include
include

void main(void)
{
FILE *fp;
int charValue;
int count;

/*open your file, something like this
*/
if ( (fp = fopen ("test", "r")) == NULL ) {
printf("test couldn't be opened\n");
exit(1);
}

while (!feof(fp)) {
count=0
while (!isspace(charValue=getc(fp) )
count=count+charValue;
ungetc(charValue,fp);
while (isspace(charValue=getc(fp) )
/* you'll need to print out your value
* here with appropriate formating
*/
printf("%d",count);
count=0;
/* you'll need to test for newline
* so you can terminate your line
*/
ungetc(charValue,fp);
}
}
Amit Agarwal_1
Trusted Contributor

Re: C or script help

Here is a C code for the same.

==================
#include
#include

int main() {

FILE *fp;
char buf[LINE_MAX+1];

fp = fopen("filename.dat", "r");
buf[LINE_MAX]='\0';
fscanf(fp, "%s", buf);
while(!feof(fp)) {
int i=0, sum=0;
for(i=0; i if((buf[i] >= 'A') && (buf[i] <= 'Z')) {
sum += (buf[i] - 'A' + 1);
continue;
}
if((buf[i] >= 'a') && (buf[i] <= 'z')) {
sum += (buf[i] - 'a' + 1);
continue;
}
}
printf("%d ", sum);
fscanf(fp, "%s", buf);
}
printf("\n");
fclose(fp);
}
==================
I hope this helps.
-Amit
Kent Ostby
Honored Contributor

Re: C or script help

Thanks for all the ideas so far.

I will try them out in the next few days and post points as I go.

For anyone else posting, please note that I really want to print out the word totals versus the letter totals although I do appreciate the various suggestions.
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Stuart Browne
Honored Contributor

Re: C or script help

There's something very simple you need to remember.

Each and every letter is made up of an ASCII character which is the same on all computers, so you don't need to worry about setting up array's of character values, as they all already have one, jsut with a given off-set.

For instance, the character 'A' = 65. The character 'a' = 97.

Simply put, if the 7th bit is on (but not 8th), it's a letter. The 6th bit determines the case (on = lower, off = upper). The first 5 bits determine it's value (1-31). The 5 characters slack are brackes, brackets and other special characters (i.e. 64 = @, 96 - `).

Armed with this, you should very simply be able to loop through the characters, breaking at chr32, adding the individual digits, taking into account the bit-offset's.

int a = 'a';
printf( "%d\n", a );

Remember, code like this is prefectly ok.
One long-haired git at your service...
Muthukumar_5
Honored Contributor

Re: C or script help

You can easily do with c programming as,

#include

main() {

FILE *fp;
char c;
int i=0;

/* File test.log has to be there */

fp=fopen("test.log","r");

if ( !fp ) {

printf ("No file is available as test.log\n");
exit (1);

}


/* If loops for string format and sum */

while ((c=getc(fp))!=EOF){

if ( c != '\n' && c != ' ' ) {
printf ("%c",c);
}

i=i+c;

if ( c == ' ' || c == '\n' ) {
printf ("=%d ",i);
}

if ( c == '\n') {
printf ("%c",c);
}
if ( c == ' ') {
printf ("'%c'=1 ",c);
}

}
}


hth.
Easy to suggest when don't know about the problem!