C langugae : Strings

A string is a continuous sequence of characters terminated by '\0', the null character. The length of a string is considered to be the number of characters excluding the terminating null character. There is no string type in C, and consequently there are no operators that accept strings as operands.A string is nothing more than a character array.All strings end with the NULL character.
char str1[30] = "Let's go";     // String length: 8; array length: 30
char str1[30] = { 'L', 'e', 't', '\'', 's',' ', 'g', 'o', '\0' };
An array holding a string must always be at least one element longer than the string length to accommodate the terminating null character. Thus the array str1 can store strings up to a maximum length of 29. It would be a mistake to define the array with length 8 rather than 30, because then it wouldn't contain the terminating null character. The C library supplies several string-handling functions; ANSI C uses the string.h header file to provide the prototypes. Some string functions that test or manipulate strings follow: See Reference Function at Glance.
• strcat(s1, s2): Concatenates (merges) the s2 string to the end of the s1 character array. The s1 array must have enough reserved elements to hold both strings.
• strcmp(s1, s2): Compares the s1 string with the s2 string on an alphabetical, element-by-element basis. If s1 alphabetizes before s2, strcmp() returns a negative value. If s1 and s2 are the same strings, strcmp() returns 0. If s1 alphabetizes after s2, strcmp() returns a positive value.
• strlen(s1): Returns the length of s1 Remember, the length of a string is the number of characters up to—but not including—the null zero. The number of characters defined for the character array has nothing to do with the length of the string.
String I/O Functions
The string input and output functions are listed as follows:
• gets(s): Stores input from stdin (usually directed to the keyboard) into the string named s.
• puts(s): Outputs the s string to stdout (usually directed to the screen by the operating system).
• fgets(s, len, dev): Stores input from the standard device specified by dev (such as stdin or stdaux) in the s string. If more than len characters are input, fgets() discards them.
• fputs(s, dev): Outputs the s string to the standard device specified by dev.
One final function is worth noting, although it is not a string function. It is the fflush() function, which flushes (empties) whatever standard device is listed in its parentheses. To flush the keyboard of all its input, you would code as follows:
fflush(stdin);
When doing string input and output, sometimes an extra newline character gets into the keyboard buffer. As gets() or getc() might have an extra newline you forgot to discard. When a program seems to ignore gets(), you might have to insert fflush(stdin) before gets().
Flushing the standard input device causes no harm, and using it can clear the input stream so that your next gets() works properly. You can also flush standard output devices with fflush() to clear the output stream of any characters you may have sent into it.
The puts() function and the null character

/* Ex-- joins two strings, check size first */
#include < stdio.h >
#include < string.h >
#define SIZE 30
#define BUGSIZE 13
int main(void)
{
char flower[SIZE];
char addon[] = "s smell like old shoes.";
char bug[BUGSIZE];
int available;
puts("What is your favorite flower?");
gets(flower);
if ((strlen(addon) + strlen(flower) + 1) <= SIZE)
strcat(flower, addon);
puts(flower);
puts("What is your favorite bug?");
gets(bug);
available = BUGSIZE - strlen(bug) - 1;
strncat(bug, addon, available);
puts(bug);
return 0;
}
Converting Strings to Numbers
ANSI C provides functions to convert numbers stored in character strings to a numeric data type. These are as follows: -
• atoi(s): Converts s to an integer. The name stands for alphabetic to integer.
• atol(s): Converts s to a long integer. The name stands for alphabetic to long integer.
• atof(s): Converts s to a floating-point number. The name stands for alphabetic to floating-point.
Numeric Functions
This section presents many of the built-in C numeric functions. As with the string functions, these functions save you time by converting and calculating numbers instead of you having to write functions that do the same
• ceil(x): The ceil(), or ceiling, function rounds numbers up to the nearest integer.
• fabs(x): Returns the absolute value of x. The absolute value of a number is its positive equivalent.
• floor(x): The floor() function rounds numbers down to the nearest integer.
• fmod(x, y): Returns the floating-point remainder of (x/y), with the same sign as x; y cannot be zero. Because the modulus operator (%) works only with integers, this function is supplied to find the remainder of floating-point number divisions.
• pow(x, y): Returns x raised to the y power, often written as xy. If x is less than or equal to zero, y must be an integer. If x equals zero, y cannot be negative.
• sqrt(x): Returns the square root of x; x must be greater than or equal to zero.
Trigonometric Functions
The following functions are available for trigonometric applications:
• cos(x): Returns the cosine of the angle x, expressed in radians.
• sin(x): Returns the sine of the angle x, expressed in radians.
• tan(x): Returns the tangent of the angle x, expressed in radians.
Logarithmic Functions
Three highly mathematical functions are sometimes used in business and mathematics. They are listed as follows:
• exp(x): Returns the base of natural logarithm (e) raised to a power specified by x (ex); e is the mathematical expression for the approximate value of 2.718282.
• log(x): Returns the natural logarithm of the argument x, mathematically written as ln(x). x must be positive.
• log10(x): Returns the base-10 logarithm of argument x, mathematically written as log10(x). x must be positive.
Arrays of Strings
It is not uncommon in programming to use an array of strings. For example, the input processor to a database may verify user commands against an array of valid commands. To create an array of strings, use a two-dimensional character array. The size of the left dimension determines the number of strings, and the size of the right dimension specifies the maximum length of each string. The following declares an array of 30 strings, each with a maximum length of 79 characters:
/* A very simple text editor. */
#include < stdio.h >
#define MAX 100
#define LEN 80
char text[MAX][LEN];
int main(void)
{
register int t, i, j;
printf("Enter an empty line to quit.\n");
for(t=0; t printf(''%d: ", t);
gets(text[t]);
if(!*text[t]) break; /* quit on blank line */
}
for(i=0; i for(j=0; text[i][j]; j++) putchar(text[i][j]);
putchar('\n');
}
return 0;
}


Free Web Hosting