Tuesday 17 January 2017

Counting the total number of alphabets, digits and special characters in the given string in C programming(c programming examples)(c program examples)

Counting the total number of alphabets, digits and special characters in the given string in C programming

The program is an example in C programming language which is written on the basis of loops and 'else if' conditions. Out task is to find out the total number of letters, digits and special characters in a given string. It is very easy when we use ASCII values in the conditions. 


If you want to see this code with a perfect indentation, copy the code into "sublime text editor" and make sure that the type of code is set to 'c' at the bottom right of the window. After pasting the code press the command "ctrl+shift+P", you get a search box. Type "indentation" you get an option like this below the search box "Indentation: Reindent lines". Clink on that to get the code with indentation. 


The program is as follows:

#include <stdio.h>
main()
{
char a[1000];
int i,alpha=0,spchar=0,digits=0;
printf("enter any string\n");
gets(a);
for(i=0;a[i]!='\0';i++)
{
if(a[i]>=65 && a[i]<=90)
alpha++;
else if(a[i]>=97 && a[i]<=122)
alpha++;
else if(a[i]>=48 && a[i]<=57)
digits++;
else
spchar++;
}
printf("the total alphabetic letters in the given string are %d\n",alpha );
printf("the total number of digits in the given string are %d\n",digits );
printf("the total number of special characters in the given string are %d\n",spchar );
printf("the total number of characters in the given string are %d\n",i+1 );
return 0;
}


The output is as follows:








#include <stdio.h>


The '#include' is a preprocessor directive which includes the source files inside the code. The 'stdio.h' is a standard input and output header file with several functions written in it. The functions like 'printf()''gets()' and 'scanf()' are work when we include 'stdio.h' header file. 

char a[1000];

The array 'a[]' is used to carry the value of the string. Each character of the string is divided and placed in each compartment of the array 'a[]'. At the end one compartment is placed with a null character, the null character represents the end of the string. We use the data type 'char' in the declaration of the array.

int i,alpha=0,spchar=0,digits=0;

We declared four integer type variables 'i', 'alpha', 'spchar' and 'digits'. The variable 'i' is used in the loops and in the end of the program it is used to print a total number of characters in the entire string.  The variable 'alpha' is used to represent the number of alphabetic letters in the string. The variable 'spchar' is used to represent the number of special characters in the string. The variable 'digits' is used to represent the total number of digits in the string. The last three variables are assigned with '0' to remove the garbage value in it.


printf("enter any string\n");
gets(a);

The two predefined functions 'printf()' and 'gets()' are included in the 'stdio.h' header file. These lines are executed with the help of 'stdio.h' header file. The function 'printf()' prints the message "enter any string". The function 'gets()' assigns the string into the array 'a[]'.

for(i=0;a[i]!='\0';i++)
{
if(a[i]>=65 && a[i]<=90)
alpha++;
else if(a[i]>=97 && a[i]<=122)
alpha++;
else if(a[i]>=48 && a[i]<=57)
digits++;
else
spchar++;
}

This for loop helps to find whether the character is an alphabetic letter or special character or a digit. The loop send only an individual character into the body of the loop to check what type it is? The ASCII values of characters are used to divide the counts of characters in the string. The ASCII values in between 65 and 90 represent that the character is an alphabetic letter. The ASCII values in between 97 and 122 represent that the character is a special character. Simultaneously the ASCII values in between 48 and 57 represent the digits. Each character in the string satisfies only one condition among all three conditions. At the end, the values of the three variables represent the total number of their particular assumptions.

printf("the total alphabetic letters in the given string are %d\n",alpha );
printf("the total number of digits in the given string are %d\n",digits );
printf("the total number of special characters in the given string are %d\n",spchar );
printf("the total number of characters in the given string are %d\n",i+1 );

After counting all the character the 'printf()' function is used to print all the integer values in each variable. At the end, the value of 'i' is incremented by '1' due to the concept of arrays. In the array, the count of compartments starts with '0'. Hence the problem is solved and another program gets into the bucket.



C is a general-purpose programming language. It has been closely associated with the UNIX system where is was developed, since both the system and most of the programs that run on it are written in C. The language, however, is not tied to any one operating system or machine; and although it has been called a “system programming language” because it is useful for writing compilers and operating systems, it has been used equally well to write major programs in many different domains.(c programming examples)(c program examples)
Many of the important ideas of C stem from the language BCPL, developed by Martin Richards. The influence of BCPL on C proceeded indirectly through the language B, which was written by Ken Thompson in 1970 for the first UNIX system on the DEC PDP-7.(c programming examples)
BCPL and B are “typeless” languages. By contrast, C provides a variety of data types. The fundamental types are characters, and integers and floating point numbers of several sizes. In addition, there is a hierarchy of derived data types created with pointers, arrays, structures, and unions. Expressions are formed from operators and operands; any expression, including an assignment or a function call, can be a statement. Pointers provide for machine-independent address arithmetic.(c programming examples)(c program examples)
C provides the fundamental control-flow constructions required for well-structured programs: statement grouping, decision making (if-else), selecting one of a set of possible cases (switch), looping with the termination test at the top (while, for) or at the bottom (do), and early loop exit (break).(c programming examples)(c program examples)
Functions may return values of basic types, structures, unions, or pointers. Any function may be called recursively. Local variables are typically “automatic,” or created anew with each invocation. Function definitions may not be nested but variables may be declared in a block-structured fashion. The functions of a C program may exist I separate source files that are compiled separately. Variables may be internal to a function, external but know only within a single source file, or visible to the entire program.(c programming examples)(c program examples)
A preprocessing step performs macro substitution on program text, inclusion of other source files, conditional compilation.(c programming examples)(c program examples)
C  is a relatively “low level” language. This characterization is not pejorative; it simply means that C deals with the same sort of object that most computers do, namely characters, numbers, and addresses. These may be combined and moved about with the arithmetic and logical operators implemented by real machines.(c programming examples)(c program examples)
C provides no operations to deal directly with composite objects such as character strings, sets, lists, or arrays. There are no operations that manipulate an entire array or string, although structures may be copied as a unit. The language does not define any storage allocation facility other than static definition and the stack discipline provided by the local variables of functions; there is n heap or garbage collection. Finally, C itself provides no input/output facilities; there are no READ or WRITE statements, and no built-in file access methods. All of these higher-level mechanisms must be provided by explicitly called functions. Most C implementations have included a reasonably standard collection of such functions.(c programming examples)(c program examples)

No comments :

Post a Comment