- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: C or script help
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-03-2005 05:24 PM
тАО07-03-2005 05:24 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-03-2005 06:24 PM
тАО07-03-2005 06:24 PM
Re: C or script help
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-03-2005 08:14 PM
тАО07-03-2005 08:14 PM
Re: C or script help
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-03-2005 08:16 PM
тАО07-03-2005 08:16 PM
Solution-------------------- 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.
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-03-2005 10:03 PM
тАО07-03-2005 10:03 PM
Re: C or script help
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
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-03-2005 10:40 PM
тАО07-03-2005 10:40 PM
Re: C or script help
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);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-03-2005 10:43 PM
тАО07-03-2005 10:43 PM
Re: C or script help
==================
#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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-04-2005 12:50 AM
тАО07-04-2005 12:50 AM
Re: C or script help
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-04-2005 11:41 AM
тАО07-04-2005 11:41 AM
Re: C or script help
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-04-2005 09:45 PM
тАО07-04-2005 09:45 PM
Re: C or script help
#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.