Decision making and looping in C programming involves executing a set of
statements repeatedly until a condition is met to terminate the loop. A
program loop has two parts: the body of the loop and the control
statement. The control statement tests conditions to determine if the loop
should continue.
Here are some things to know about decision making and looping in C:
• Loop types
There are two types of loops in C: entry-controlled and exit-controlled.
• Entry-controlled loops: The control statement is placed before the body of
the loop. Examples of entry-controlled loops in C include the while and for
loops.
• Exit-controlled loops: The control statement is placed after the body of the
loop. An example of an exit-controlled loop in C is the do-while loop.
• The while statement
This is a pre-test loop structure that is used when it is uncertain if the loop
will be executed. The while statement evaluates the expression before
executing the statement.
• The switch case statement
This is an alternative to the if-else-if ladder. The switch block consists of
cases that are executed based on the value of the switch variable.
String Handling Functions in C
C programming language provides a set of pre-defined functions called string handling
functions to work with string values. The string handling functions are defined in a header file
called string.h. Whenever we want to use any string handling function we must include the header
file called string.h.
The following table provides most commonly used string handling function and their use...
Function Syntax (or) Example Description
strcpy() strcpy(string1, string2) Copies string2 value into string1
strncpy() strncpy(string1, string2, Copies first 5 characters string2 into string1
5)
Function Syntax (or) Example Description
strlen() strlen(string1) returns total number of characters in string1
strcat() strcat(string1,string2) Appends string2 to string1
strncat() strncpy(string1, string2, Appends first 4 characters of string2 to string1
4)
strcmp() strcmp(string1, string2) Returns 0 if string1
less than 0 if string1<string2; greater than 0 if string1>string2
strncmp() strncmp(string1, string2, Compares first 4 characters of both string1 and string2
4)
strcmpi() strcmpi(string1,string2) Compares two strings, string1 and string2 by ignoring case (upper or lower)
stricmp() stricmp(string1, string2) Compares two strings, string1 and string2 by ignoring case (similar to strcmpi())
strlwr() strlwr(string1) Converts all the characters of string1 to lower case.
strupr() strupr(string1) Converts all the characters of string1 to upper case.
strdup() string1 = strdup(string2) Duplicated value of string2 is assigned to string1
strchr() strchr(string1, 'b') Returns a pointer to the first occurrence of character 'b' in string1
strrchr() 'strrchr(string1, 'b') Returns a pointer to the last occurrence of character 'b' in string1
strstr() strstr(string1, string2) Returns a pointer to the first occurrence of string2 in string1
strset() strset(string1, 'B') Sets all the characters of string1 to given character 'B'.
strnset() strnset(string1, 'B', 5) Sets first 5 characters of string1 to given character 'B'.
strrev() strrev(string1) It reverses the value of string