Tuesday, February 5, 2013

String Comparison in C

We can compare two strings to find whether two strings are equal or not. If they are not equal which comes first in the dictionary order. 

So, to compare two strings; First, we need to keep compare on corresponding characters of the both strings till the corresponding characters are same and none of the string are exhausted. If both the strings are exhausted simultaneously, then the strings are identical i.e. equal; else strings are different.

If the strings are different, then we need to find out which strings comes first in the dictionary order. In that case, if the first strings is exhausted, then it comes first; else second string comes first; 

If the strings are different and none of the strings are exhausted, then if the difference in the ASCII value of the corresponding characters is negative, i.e. character of the first string has lower ASCII value, then the first string comes earlier in the dictionary order. If the difference is positive, then the second string comes earlier in the dictionary order. 

Compile and execute the following code segment which implements string comparison function:






























      

You can use built-in string comparison function 'strcmp()' to do the same. Look at the following code:


















You can also sort set of strings in dictionary order using this function. Look at the following code:



Thursday, January 24, 2013

Array of Strings in C

A string in C is an array of characters treated as a single unit. For example:

           char str[6] =”hello”;

i.e. a string variable can store one string. If you need to handle array of strings, what is the solution?

You may use 2D array of characters to handle list of strings like 2D array. For example, you want to store month names of a year i.e. list of 12 months with max length 9 character(“September”). So the declaration will be:
                                                      
            char months [12] [10];

Where, 1st subscript defines how many strings and 2nd subscript defines how many characters in a string. 

You can initialize the 2D string following way:

char months[12][10]={ “January”, “February”, “March”, “April”, “May”, “June”, “July”,   
                                  “August”, “September”, “October”, “November”, “December”}

And this list of string can be represented in memory as follows:

J
a
n
u
a
r
y
‘\0’
F
e
b
r
u
a
r
y
‘\0’
M
a
r
c
h
‘\0’
A
p
r
i
l
‘\0’
M
a
y
‘\0’
J
u
n
e
‘\0’
J
u
l
y
‘\0’
A
u
g
u
s
t
‘\0’
S
e
p
t
e
m
b
e
r
‘\0’
O
c
t
o
b
e
r
‘\0’
N
o
v
e
m
b
e
r
‘\0’
D
e
c
e
m
b
e
r
‘\0’

If we look at the memory representation table, total 120 bytes used to store this list of strings. But if we count the total characters in a list, only 86 bytes requires to store the list. So 34 (120-86) bytes wastage.

That’s why most efficient way of storing list of strings is array of pointers to strings and declared as follows:

char *months [12] = { “January”, “February”, “March”, “April”, “May”, “June”, “July”,  
                                 “August”, “September”, “October”, “November”, “December”}

And the memory representation will be:

J
a
n
u
a
r
y
‘\0’
F
e
b
r
u
a
r
y
‘\0’
M
a
r
c
h
‘\0’

A
p
r
i
l
‘\0’

M
a
y
‘\0’

J
u
n
e
‘\0’

J
u
l
y
‘\0’

A
u
g
u
s
t
‘\0’

S
e
p
t
e
m
b
e
r
‘\0’
O
c
t
o
b
e
r
‘\0’
N
o
v
e
m
b
e
r
‘\0’
D
e
c
e
m
b
e
r
‘\0’



Now 86 bytes required to store the list, so no memory wastage. But there is one limitation in array of pointers to strings. We cannot receive strings from keyboard using scanf() function. So the following code segment will not work during execution:

int main()
{
    char *names[5];
    int count;
    for(count=0;count<=5;count++)
        scanf("%s",names[count]);            
    return 0;
}

We can solve this problem by allocating space using malloc() following way:

int main()
{    
      char *names[5], str[20];    
      int count;    
      for(count=0;count<=5;count++)    
      {       
            scanf("%s",str);
            names[count] = (char*)malloc(sizeof(str));
            strcpy(names[count],str);
       }      
       for(count=0;count<=5;count++)       
           printf("%s\n",names[count]);        
       return 0;
}