Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(1) Write a function that will return in a structure the following characteristi

ID: 646756 • Letter: #

Question

(1) Write a function that will return in a structure the following characteristics of a given string:

1) string length (use strlen),
2) number of upper case characters
3) number of lower case characters,
4) number of digits

5) number of blank spaces

6) number of non-alphanumeric characters

(2) Write a program to compute various offensive statistics on baseball. The following information will be available on each player:

Number of Walks (BB), Strike Outs (SO), Hit by Pitch (HP), Sac Flys (SF), Singles,

Doubles (2B), Triples (3B), and Home Runs (HR) as well as Number of At Bats (AB). Based on this information, develop a set of functions within your program that will compute the following:

Total Bases, Batting Average, Home Run Ratio, Strike Out Ratio, On Base Percentage, and Slugging Average. You do not need to be a baseball fan to do this ...

All the information you need in terms of the formulas and explanations can be found at:

http://www.baseball-almanac.com/stats.shtml

Note: Number of hits is: singles + doubles + triples + home runs

If you want to check your work, see compiled stats of the 2014 Boston Red Sox at: http://www.baseball-reference.com/teams/BOS

Explanation / Answer

#include <string.h>

The common functions are described below:

char *stpcpy (char *dest,const char *src) -- Copy one string into another.
int strcmp(char *string1,const char *string2) - Compare string1 and string2 to determine alphabetic order.
char *strcpy(char *string1,const char *string2) -- Copy string2 to stringl.
char *strerror(int errnum) -- Get error message corresponding to specified error number.
size_t strlen(const char *string) -- Determine the length of a string.
char *strncat(char *string1, char *string2, size_t n) -- Append n characters from string2 to stringl.
int strncmp(char *string1, char *string2, size_t n) -- Compare first n characters of two strings.
char *strncpy(char *string1,const char *string2, size_t n) -- Copy first n characters of string2 to stringl .
int strcasecmp(const char *s1, const char *s2) -- case insensitive version of strcmp().
int strncasecmp(const char *s1, const char *s2, int n) -- case insensitive version of strncmp().

The use of most of the functions is straightforward, for example:

Note that both strcat() and strcpy() both return a pointer to the first char in the destination array. Note the order of the arguments is destination array followed by source array which is sometimes easy to get the wrong around when programming.

The strcmp() function lexically compares the two input strings and returns:

Less than zero

-- if string1 is lexically less than string2

Zero

-- if string1 and string2 are lexically equal

Greater than zero

-- if string1 is lexically greater than string2

This can also confuse beginners and experience programmers forget this too.

The strncat(), strncmp,() and strncpy() copy functions are string restricted version of their more general counterparts. They perform a similar task but only up to the first n characters. Note the the NULL terminated requirement may get violated when using these functions, for example:

In general, with this form of strncpy(), str2 may NOT be NULL TERMINATED!! -- BEWARE. Also if the length of the str1 is longer that str2 what will happen?

String Searching

The library also provides several string searching functions:

char *strchr(const char *string, int c) -- Find first occurrence of character c in string.
char *strrchr(const char *string, int c) -- Find last occurrence of character c in string.
char *strstr(const char *s1, const char *s2) -- locates the first occurrence of the string s2 in string s1.
char *strpbrk(const char *s1, const char *s2) -- returns a pointer to the first occurrence in string s1 of any character from string s2, or a null pointer if no character from s2 exists in s1
size_t strspn(const char *s1, const char *s2) -- returns the number of characters at the begining of s1 that match s2.
size_t strcspn(const char *s1, const char *s2) -- returns the number of characters at the begining of s1 that do not match s2.
char *strtok(char *s1, const char *s2) -- break the string pointed to by s1 into a sequence of tokens, each of which is delimited by one or more characters from the string pointed to by s2.
char *strtok_r(char *s1, const char *s2, char **lasts) -- has the same functionality as strtok() except that a pointer to a string placeholder lasts must be supplied by the caller.

strchr() and strrchr() are the simplest to use, for example:

After this execution, ans points to the location str1 + 2

strpbrk() is a more general function that searches for the first occurrence of any of a group of characters, for example:

Here, ans points to the location str1 + 1, the location of the first e.

strstr() returns a pointer to the specified search string or a null pointer if the string is not found. If s2 points to a string with zero length (that is, the string ""), the function returns s1. For example,

will yield ans = str + 3.

strtok() is a little more complicated in operation. If the first argument is not NULL then the function finds the position of any of the second argument characters. However, the position is remembered and any subsequent calls to strtok() will start from this position if on these subsequent calls the first argument is NULL. For example, If we wish to break up the string str1 at each space and print each token on a new line we could do: