Tuesday 17 January 2017

Comparing two strings in C programming(c programming examples)(c program examples)

Comparing two strings in C programming

The program is an example introducing the string functions and how they work. There are two programs above with the same output. The first program is written on loops and conditions. But, the second program is using the function 'strcmp()', it returns only one value among '1''0' or '-1'. These three values are three conditions. The value '0' represents that both the strings are equal. The values '1' and '-1' represent that the two strings are not equal.

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.  

There are two methods for comparing two strings

     1. Without using string function.
     2. With using string function.


The program without string function:

#include <stdio.h>

main()
{
    char string1[1000],string2[1000];
    int res;
    printf("enter the first string\n");
    gets(string1);
    printf("enter the second string\n");
    gets(string2);
    res=compare(string1,string2);
    if(res==0)
        printf("the two strings are equal:\n");
    else if(res==1)
        printf("the first string is lexicographically greater than the second string\n");
    else
        printf("the first string is lexicographically smaller than the first one\n");
    return 0;
}
compare(char * string1,char * string2)
{
    int i=0;
    while(string1[i]==string2[i])
    {
        if(string1[i]!='\0' || string2[i]!='\0')
            break;
        i++;
    }
    if(string1[i-1]=='\0' && string2[i-1]=='\0')
        return 0;
    else if(string1[i]>string2[i])
        return 1;
    else if(string1[i]<string2[i])
        return -1;

}




The program using string function:

#include <stdio.h>

#include <string.h>
main()
{
char string1[1000],string2[1000];
int res;
printf("enter the first string\n");
gets(string1);
printf("enter the second string\n");
gets(string2);
res=strcmp(string1,string2);
if(res==0)
printf("the two strings are equal\n");
else if(res==1)
printf("the first string is lexicographically greater than the second string\n");
else
printf("the first string is lexicographically smaller than the first string\n");
return 0;
}

The output is as follows:






char string1[1000],string2[1000];

We declared two arrays 'string1[]' and 'string2[]'. The two arrays are filled with the two inputs given by the user. And then we compare the two arrays. The data type 'char' is used to store the character data in the two arrays.

 res=compare(string1,string2);

The variable 'res' sends the two string values 'string1' and 'string2' into the function named as 'compare'. After execution of the function the function 'compare' it returns a value which decides the comparison.

compare(char * string1,char * string2)
{
    int i=0;
    while(string1[i]==string2[i])
    {
        if(string1[i]!='\0' || string2[i]!='\0')
            break;
        i++;
    }
    if(string1[i-1]=='\0' && string2[i-1]=='\0')
        return 0;
    else if(string1[i]>string2[i])
        return 1;
    else if(string1[i]<string2[i])
        return -1;

}


This function checks each element of two arrays. If all the elements in the array are equal then it sends the value '0' into the main function. If there are variations into two strings. If the first string is bigger than the second string it sends the value '1' or the second string is greater than the first string then the function returns the value '-1'

    if(res==0)
        printf("the two strings are equal:\n");
    else if(res==1)
        printf("the first string is lexographically greater than the second string\n");
    else
        printf("the first string is lexicographically smaller than the first one\n");

If 'res' value is equal to '0' it prints the message "the two strings are equal:". If 'res' value is equal to '1' it prints the message "the first string is lexicographically greater than the second.". If the value is equal to '-1' then it prints the message "the first string is lexicographically smalle than the first one."

Second program:

res=strcmp(string1,string2);

The function 'strcmp()' sends the two strings into the function which is predefined in the header file 'string.h'. As the above program, it sends among the three values '0', '1' and '-1'

Hence the program 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)

1 comment :