Friday, November 25, 2011

Exiting from Nested Loops in Java

Let us consider a program that takes as input 5 numbers and outputs their sum.The program should do this thing 20 times. He should also only close immediately after the user entered 100 numbers (5 numbers x 20 times) or if the user introduces 0 as one of the numbers.

To implement such a program, nested loops are needed: one loop to ensure that the program keeps running 20 times (called a outer loop) and another loop (called an inner loop) where the user is asked 5 times for his input. The problem arises when the user inputs 0. Using the "normal" break statement, one could prematurely close the inner loop but it would exit to the outer loop and would not end the program (but rather restart the input process)

There are two ways to solve this problem:

1.Introducing a boolean flag in the loop's invariant

By introducing a boolean variable (a flag) to the loop's invariant we make sure that for each iteration of the outer loop it is checked if the program encountered 0 as user input. If the program encounters 0 it will change the state of the boolean variable and it will cause the outer loop to end (the inner loop will be exited through a break statement).

Also, before outputting the sum, for each iteration one needs to check also if the boolean variable was set or not (the program encountered 0 or not). Otherwise, the program will output the sum even if 0 was encountered.
  boolean exitFlag = false;
  int counter = 0;
  int i=0, temp=0;
  int sum = 0;
  Scanner keyboard = new Scanner(System.in);
  while(exitFlag==false && counter!=20)
  {
   System.out.println("Input five numbers: ");
   for(i=0;i<5;i++)
   {
    temp = keyboard.nextInt();
    if(temp!=0)
     sum += temp;
    else
    {
     exitFlag = true; //ExitFlag
     break;
    }
   }
   if(exitFlag==false)
        System.out.println("The sum of the last 5 numbers is:"+sum);
   sum=0;
   counter++;
  }

2.Using an identifier for the break statement

The main advantage of this method is that is less complex that the previous one and it consumes less execution time. Of course, considering the example program, execution time is not an important problem but in some other cases it may prove crucial (especially when it comes to real-time systems or multithreading applications)

By using an identifier/label (outerloop), one can use a labelled break statement in order to directly exit a loop specified by its label. This way, the outer loop's invariant is simplified (there is no need to check for a boolean flag) and the sum outputting process can also go unchecked. Some may argue that this method is reminiscent of C's goto and leads to a bad programming style, but sometimes it may prove quite handy.
  int i=0, temp=0;
  int counter = 0;
  int sum = 0;
  Scanner keyboard = new Scanner(System.in);
  outerloop:    //Label
   while(counter!=20)
   {
    System.out.println("Input five numbers: ");
    for(i=0;i<5;i++)
    {
     temp = keyboard.nextInt();
     if(temp!=0)
      sum += temp;
     else
      break outerloop; //Labeled break
    }
    System.out.println("The sum of the last 5 numbers is:"+sum);
    sum=0;
    counter++;
   }

Thursday, November 24, 2011

Precedence of Operators in Java

Not every boolean or arithmetic operation needs to be parenthesised in order to make sure that the expression is evaluated correctly. Java has an in-built set of rules concerning the precedence of operators in accordance to calculus (and logic) rules.

In the list below the operators are grouped according to their priority (precedence) in the evaluation process of an expression: Operators which are in the same group have the same priority.The groups are numbered from 1 to 15, where 1 represents the highest precedence and 15 the lowest precedence.
  1. Dot operator, array brackets, method invocation: . [] ()
  2. Postfix operators ++  -- (as in a++, a--)
  3. Prefix operators ++  -- (as in ++a, --a), logical and bitwise not ! ~ and the unary operators + - (as in x=-3 or y=+5)
  4. Type casts () and object creation operator  new
  5. Arithmetic operators % * /
  6. Arithmetic operators: + - and the string concatenation operator +
  7. Bit shift operators : >>  <<  >>> (the unsigned right shift operator)
  8. Relational type comparison: <   >  <=   >= and instanceof
  9. Condition checking operators == !=
  10. Bitwise and: &
  11. Bitwise or |
  12. Conditional and:  &&
  13. Conditional or: ||
  14. Conditional operator ?:
  15. Assignment operators :  =  \=  *=  +=  -=  %=  &=  |=  <<=  >>=  >>>=
Precedence influences the evaluation order in the following way:
  • Operators with higher precedence are evaluated before operators with lower precedence.
  • When operators of equal precedence appear in the same expression binary operators (all groups described above except 2,3,4 and 14,15) are evaluated from left to right. Assignment operators (the groups 2,3,5 and 14,15) are evaluated from right to left.
Example:
      int a = 4, b = 2;
      int c = 0;
      c = ++a % b-- * 2 + 5;
      //
      //(1)++a is evaluated first  => a becomes equal to 5
      //    c = 5 % b-- * 2 +5;
      //(2)b-- is evaluated second => b will become equal to 1 after 
      //    a value will be assigned to c
      //    c = 5 % 2 * 2 + 5
      //(3)% - is evaluated third   => 5 % 2 = 1
      //   c = 1 * 2 + 5
      //(4)* - is evaluated forth   => 1 * 2 = 2
      //   c = 2 + 5
      //(5)+ - is evaluated fifth   => 2 + 5 = 7
      //  c = 7
      //(6)= - is evaluated last => c becomes equal to 7
      //After the expression is evaluated b becomes equal to 1
      System.out.println("a="+a+"\n"+
                         "b="+b+"\n"+
                         "c="+c);
      //Output
      //a=5
      //b=1
      //c=7
Note that the example code above is pretty bad in terms of programming style and it should be regarded only as a demonstration on how the compiler evaluates an expression.

Example:
      String testString1 = "";
      testString1 = 1 + 2 + 3 + "Look at me!";
      //The string concatenation operator +  and the arithmetic +
      //have the same precedence, so the evaluation will start from 
      //left to right:
      //(1)The first + is considered arithmetic =>  1 + 2 = 3
      //testString1 = 3 + 3 + "Look at me!";
      //(2)The second + is also considered arithmetic => 3 + 3 = 6
      //testString1 = 6 + "Look at me!";
      //(3)The third + is considered the concatenation operator,
      //so 6 will be converted to the temporary string which will
      //hold the value "6" => "6" + "Look at me!" = "6Look at me!"
      //testString1 = "6" + "Look at me!" = "6Look at me!";
      //(4)The assignment operator will be evaluated last =>
      //testString1 will contain the value "6Look at me!"
      //String testString2 = "";
      testString2 = "Look at me!" + 1 + 2 + 3;
      //The string concatenation operator +  and the arithmetic +
      //have the same precedence, so the evaluation will start
      //from left to right:
      //(1)The first + is considered as being a concatenation
      //operator and the second operand 1 will be converted to the
      //string "1" => "Look at me!"+"1" = "Look at me1"
      //testString2 = "Look at me!" + "1" + 2 + 3 =
      //            = "Look at me!1" + 2 + 3
      //(2)The second + is also considered as being a concatenation
      //operator the the second operand 2 will be converted to the
      //string "2" => "2" + "Look at me1" = "Look at me12"
      //testString2 = "Look at me!1" + 2 + 3 =
      //            = "Look at me!12" + 3
      //(3)The third + is also considered as being a concatenation
      //operator the the second operand 3 will be converted to the
      //string "3" => "3" + "Look at me12" = "Look at me123"
      //testString2 = "Look at me!12" + "3" =
      //     = "Look at me!123"
      //(4)The assignment operator will be evaluated last =>
      //testString1 will contain the value "Look at me!123" */
      String testString3 = "";
      testString3 = "Look at me!" + (1 + 2 + 3);
      //First will be evaluated what is in the round parenthesis
      //from left to right:
      //(1)The first + is considered as being arithmetic => 
      //1 + 2 = 3
      //testString3 = "Look at me!" + (3 + 3);
      //(2)The second + is considered as being arithmetic also =>
      //3 + 3 = 6
      //testString3 = "Look at me!" + 6;
      //(3)After the parenthesis was evaluated, the remaining + will
      //be considered as a concatenation operator:
      //testString3 = "Look at me!" + "6" = "Look at me!6"
      //(4)The assignment operator will be evaluated last =>
      //testString1 will contain the value "6Look at me!"*/
      System.out.println(testString1 + "\n" +
                         testString2 + "\n" +
                         testString3 + "\n");
      //Output
      //6Look at me!
      //Look at me!123
      //Look at me!6
Note:
Because the current syntax highliter I'm using seems to have a problem with /* */ comments (it doesn't recognize */,  so it will highlight everything), I had to use // for each line.

Short Circuit Evaluation

Short circuit evaluation (also called "lazy" evaluation or McCarthy Evaluation) is a commonly used method for avoiding the execution of a second expression contained in a conditional clause.

Speaking plainly, it means that when you use an if instruction that has 2 or more clauses, if the first condition is not met (when the conditions are connected by an AND operator) or if the first condition is met (when the conditions are connected by an OR operator), the program will not evaluate the second expression.

The short circuit evaluation is available for C, C++, Java, C# and VB (and most of the other programming languages. See this for the full list). 

It is commonly used for:
  • Checking before a division that the denominator is not equal to 0
  • Checking if a pointer is allocated before doing some operation with the data that he is pointing to.
Also, short circuit evaluation helps optimizing the code since an if statement that contains 5 clauses will end very quickly if one condition is not respected (without continuing to evaluate the other clauses), so your program will run more quickly.

Division check example for C\C++, Java and C#:
int a = 0, b = 32;
if ( (a != 0) && (b / a > 10) )
   //Will not go here because a=0
else
   //Will go here without triggering an divide by zero error/exception
Division check example for VB.NET
Dim x, y As Integer
x = 0
y = 32
If ( (x <> 0) AndAlso (x / y > 10) ) Then
   'Will not go here because x=0
Else
   'Will go here without triggering an division by zero exception
End If
Pointer example for C\C++
//We shall assume that you have a structure/class with a field
//(a public field in//case of C++) data.
MyStruct *myStructPointer = NULL;
if ( (myStructPointer!=null) && (myStructPointer.data == 5) )
   //Will not go here because myStructPointer = null
else
   //Will go here without triggering a null error/exception
Pointer (Reference) example for Java and C#
//We shall assume that you have a class containing
//a public field called data.
Xobject myObject = null;
if ( (myObject!=null) && (myObject.data == 5) )
   //Will not go here because myObject = null
else
   //Will go here without triggering a null error/exception
Pointer (Reference) example for VB. NET
If ( (str <> Nothing) AndAlso (str.Equals("Bird") ) Then
   'Will not go here because x=0
Else
   'Will go here without triggering a null exception
End If
In VB.NET the short circuit OR operator is OrAlso.

In some cases you may not want to use short circuit evaluation, but rather let the program evaluate all clauses. In this case you will need to use "eager" operators like (in contrast to the "lazy" operators used in short circuit evaluation):
  • C++\C#\Java : & instead of && and | instead of ||
  • Java has also : and instead of && and or instead of ||
  • VB.NET : and instead of andAlso and or instead of orAlso 
  • C doesn't have "eager" operators and all evaluation is done short-circuit only.
"Eager" operators are mostly used when your second condition consists of a function that modifies some of your other variables (but this corresponds in most cases to a bad programming style).

Sunday, November 20, 2011

The Arithmetic, Geometric and Harmonic Mean of an Array in ANSI C

Before defining the functions which shall compute the means, we shall create 2 functions which will help us compute the elemental sum and the elemental product of the array. These functions will be used by the mean functions.
/**
 * Description:
 *  Computes the sum of all the elements from the array.
 * Parameters:
 *  array - a pointer to the array
 *  size - the number of elements present in the array
 * Returns:
 *  The sum of all elements from the array.
 */
double ArraySum(double* array, int size)
{
   double sum = 0.0;
   int i;
   for(i=0; i<size; i++)
   {
      sum+=array[i];
   }
   return sum;
}
/**
 * Description:
 *  Computes the product of all the elements from the array.
 * Parameters:
 *  array - a pointer to the array
 *  size - the number of elements present in the array
 * Returns:
 *  The product of all elements from the array.
 */
double ArrayProduct(double *array, int size)
{
   double product = 1.0;
   int i;
   for(i=0; i<size; i++)
   {
      product*=array[i];
   }
   return product;
}
1.The Arithmetic Mean

The arithmetic mean is computed using the following formula:
where n represents the size of the array and xi represents the element at the i index. The implementation for the function is:
/**
 * Description:
 *  Computes the arithmetic mean of all the elements from the array.
 * Parameters:
 *  array - a pointer to the array
 *  size - the number of elements present in the array
 * Returns:
 *  The arithmetic mean of all elements from the array.
 */
double ArithmeticMean(double* array, int size)
{
   double result = ArraySum(array,size)/size;
   return result;
}
2.The Geometric Mean

The geometric mean is computed by using the following formula:
where n represents the size of the array and xi represents the element at the i index. The implementation for the function is:
/**
 * Description:
 *  Computes the geometric mean of all the elements from the array.
 * Parameters:
 *  array - a pointer to the array
 *  size - the number of elements present in the array
 * Returns:
 *  The harmonic mean of all elements from the array.
 */
double GeometricMean(double* array, int size)
{
   double result = pow(ArrayProduct(array,size),
                   (1.0/(double)(size)));
   return result;
}
3.The Harmonic Mean
The harmonic mean is computed by using the following formula:
where n represents the size of the array and xi represents the element at the i index. The implementation for the harmonic mean function would be:
/**
 * Description:
 *  Computes the harmonic mean of all the elements from the array.
 * Parameters:
 *  array - a pointer to the array
 *  size - the number of elements present in the array
 * Returns:
 *  The geometric mean of all elements from the array.
 */
double HarmonicMean(double* array, int size)
{
   double result = 0;
   int i;
   for(i=0; i<size; i++)
   {
      result+=(1.0)/array[i];
   }
   result = (double)(size)*pow(result,-1.0);
   return result;
}
4. Example
#include<stdio.h>
#include<math.h>

double ArraySum(double* array, int size);
double ArrayProduct(double *array, int size);
double ArithmeticMean(double* array, int size);
double GeometricMean(double* array, int size);
double HarmonicMean(double* array, int size);

int main(void)
{
   /*Declaring an array which will hold 9 elements*/
   double array[] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0,7.0, 8.0, 9.0};
   /*Printing the results of the functions*/
   printf("Arithmetic mean = %lf\n"
          "Harmonic mean   = %lf\n"
          "Geometric mean  = %lf\n",
          ArithmeticMean(array,9),
          HarmonicMean(array,9),
          GeometricMean(array,9));
   return 0;
}
/*Output
Arithmetic mean = 5.000000
Harmonic mean   = 3.181372
Geometric mean  = 4.147166
 */

Creating, Destroying and Copying A Matrix using NMat

Before reading about how create, destroy and copy a matrix using the NMat library, make sure you've read this article:
The article explains the principles and the structure of the matrices used by NMat.

The operations Create, Destroy and Copy are implemented by the following functions:
/*
 * Description:
 *  Alocates spaces for a NMatrix structure according to the parameters
 * @rows si @columns.
 * Parameters:
 *  rows - the number of rows
 *  columns - the number of columns
 * Returns:
 *  A pointer to the allocated matrix structure.
 */
NMatrix* NMatrix_Create(integer rows, integer columns);
/*
 * Description:
 *  Free the space hold by a NMatrix structure
 * Parameters:
 *  matrix - a pointer to the NMatrix structure
 * Returns:
 *  NULL
 * Preconditions:
 *  @matrix must not be NULL
 */
NMatrix* NMatrix_Destroy(NMatrix *matrix);
/*
 * Description:
 *  Creates a hard copy of the matrix @source
 * Parameters:
 *  source - the matrix who shall be copied
 *  destination - the matrix where the copy
 *       will be stored
 * Returns:
 *  A pointer to the clone matrix
 * Preconditions:
 *  @matrix must not be NULL
 */
NMatrix* NMatrix_Clone(const NMatrix *source);
If you want to see how the functions are implemented, click the link below:
Example:
#include<stdio.h>
#include"NMatrix.h"

void PrintMatrix(NMatrix *mat)
{
   integer i = 0, j = 0;
   for (i = 0; i < mat->rows; i++)
   {
      for (j = 0; j < mat->columns; j++)
      {
         printf("%+f ", mat->data[i][j]);
      }
      putchar('\n');
   }
   putchar('\n');
}

int main(int argc, char *argv[])
{
   NMatrix* mat = NULL;
   NMatrix* matCopy = NULL;
   /*Creating a 2x2 NMatrix structure*/
   mat = NMatrix_Create(2, 2);
   /*Assigning values to the structure*/
   mat->data[0][0] = 3.3;
   mat->data[0][1] = 2.1;
   mat->data[1][0] = 5.2;
   mat->data[1][1] = -6.3;
   /*Printing the matrix*/
   puts("The original matrix: ");
   PrintMatrix(mat);
   /*Creating a copy of the first matrix*/
   matCopy = NMatrix_Clone(mat);
   /*Destroying the first matrix*/
   mat = NMatrix_Destroy(mat);
   /*Printing the second matrix*/
   puts("The copy of the matrix: ");
   PrintMatrix(matCopy);
   return 0;
}
/*Output:
 The original matrix:
 +3.300000 +2.100000
 +5.200000 -6.300000

 The copy of the matrix:
 +3.300000 +2.100000
 +5.200000 -6.300000
 */

Tuesday, November 15, 2011

Splitting a String into Substrings in ANSI C

The easiest way to split a string into substrings is by the string.h library function strtok.

Here's an example on how to use it:
#include<stdio.h>
#include<string.h>

int main(void)
{
   char str[45] = "Bird!Bird!Bird!The bird is the word!";
   char *substr = NULL;
   /*"Initializes" strtok and gets the first substring*/
   substr = strtok(str," !");
   /*Loops until there are no more substrings*/
   while(substr!=NULL)
   {
       /*Prints the token*/
       puts(substr);
       /*Gets the next substrings*/
       substr = strtok(NULL," !");
   }
   puts("\nWhat remained from the primary string: ");
   puts(str);
   return 0;
}
/*Output:
Bird
Bird
Bird
The
bird
is
the
word

What remained from the primary string:
Bird
 */
The function can be a little odd to use, since it returns a pointer to a substring and not an array of strings like you would probably expect. When using it to split string, the following steps must be completed:

1)You must first "initialize" strtok by calling it using the string you want to split as the string primaryString. This will return a pointer to the first substring (which will be between your first character and the first delimiter character found)

2)To get the next substring, strtok must be called using the NULL pointer instead of the string you want to split. The function will return a pointer to a substring between the first delimiter found and the next delimiter character found (Note: the delimiter characters will not be included).

3)Using a loop you can get all the substrings from the string you want to split (you will still need to call strtok using a NULL pointer, so it will know that you have not changed the string you want to split).
When strtok will finish the splitting process it will return a NULL pointer.

PITFALL:It's very important to know that strtok replaces all delimiters with NULL, so your primary string will be destroyed after it's tokenized.


Question

References:
http://www.cplusplus.com/reference/cstring/

Tuesday, November 1, 2011

Searching Characters and Substrings in a String in ANSI C

ANSI C provides the string.h library which is specialized in string operations. If you're not that familiar with how strings work in ANSI C, you can check out my previous post here. The available functions for string searching are:
-Searching for a character : strchr, strrchr, strcspn, strpbrk,
-Searching for a substring : strstr, strspn

Searching for a character in a string

Both strchr and strchr will return a pointer (the memory address) of the character you search.

The difference between them is that strchr will return a pointer to the location of the first occurrence of the character, while strrchr will return a pointer to the location of the last occurence of the character.

If the character does not exist in the string, NULL will be returned.

Here's an example on how to use them:
#include<stdio.h>
#include<string.h>

int main(void)
{
    char string[40] = "The bird is the word";
    char *firstE = NULL;
    char *lastE = NULL;
    int firstEIndex = 0;
    int lastEIndex = 0;

    firstE = strchr(string, 'e');
    lastE = strrchr(string, 'e');

    /*You can use these pointer to print the
     substring starting with the first/last occurence of
     your character*/
    puts(firstE);
    puts(lastE);

    /*You can also easily determine de index of the searched
    character by applying simple pointer arithmetic*/
    firstEIndex = firstE - string;
    lastEIndex = lastE - string;
    printf("%d %d\n",firstEIndex, lastEIndex);

    if(firstE!=NULL)
    {
        puts("We found the character!");
    }
    return 0;
}
/*Output
e bird is the word
e word
2 14
We found the character!
 */
TIP: As you saw in the example above, you can easily determine the index of the character using pointer arithmetic.
TIPThe searched character is sent as an integer, but is internally converted to a char. If you want to read why, check out this link.
PITFALL: The result is unpredictable for string which are not null terminated.
The ANSI functions defined in the library string.h that are available for this purpose are:

An alternative to strchr is strcspn. This function no longer takes as parameter a single character, but another string (a key string).

strcspn will return the index of the first occurrence of one of the characters from the key string. If none of the characters from the key string are found, strcspn will return the length of the searched string.

Here's an example on how to use this function:
#include<stdio.h>
#include<string.h>

int main(void)
{
    char string[20]="The bird is the word";
    size_t size = strlen(string);

    int eFirstIndex = strcspn(string,"e");
    int eOrhFirstIndex = strcspn(string, "eh");
    int xyFoundIndex = strcspn(string, "xXyY");

    //Verifies if e was found
    if(eFirstIndex!=size)
    {
        printf("e char index is %d\n",eFirstIndex);
    }
    else
    {
        printf("e char not found\n");
    }
    //Verifies if e or h was found
    if(eOrhFirstIndex!=size)
    {
       printf("e or h char index is %d\n",eOrhFirstIndex);
    }
    else
    {
       printf("e or h chars not found in string\n");
    }

    //Verifies if x,X,y or Y was found
    if(xyFoundIndex!=size)
    {
       printf("x or y or X or Y found at char index is %d\n",xyFoundIndex);
    }
    else
    {
       printf("No occurences of x, y, X or Y\n");
    }

    return 0;
}
/*Output:
e char index is 2
e or h char index is 1
No occurences of x, y, X or Y
 */
Another alternative is strpbrk. The difference is that strpbrk will return a pointer (just like strstr) instead of the index.

Here's an example on how to use it:
#include<stdio.h>
#include<string.h>

int main(void)
{
    char string[20]="The bird is the word";

    char* eFirstOccurence = strpbrk(string,"e");
    char* eOrHFirstOccurence = strpbrk(string, "eh");
    char* xOrYFirstOccurence = strpbrk(string, "xXyY");

    //Verifies if e was found
    if(eFirstOccurence!=NULL)
    {
        printf("e char index is %d\n",(eFirstOccurence-string) );
    }
    else
    {
        printf("e char not found\n");
    }
    //Verifies if e or h was found
    if(eOrHFirstOccurence!=NULL)
    {
       printf("e or h char index is %d\n",(eOrHFirstOccurence-string));
    }
    else
    {
       printf("e or h chars not found in string\n");
    }

    //Verifies if x,X,y or Y was found
    if(xOrYFirstOccurence!=NULL)
    {
       printf("x or y or X or Y found at char index is %d\n",
              (xOrYFirstOccurence-string) );
    }
    else
    {
       printf("No occurences of x, y, X or Y\n");
    }

    return 0;
}
/*Output
e char index is 2
e or h char index is 1
No occurences of x, y, X or Y
 */
TIPFor strpbrk you can apply pointer arithmetic to determine the index, just like for strchr.
PITFALL: The result is unpredictable if any of the strings are not null terminated. This is true for both strpbrk and strcspn.

Searching for a substring in a string

The functions use for searching substrings are similar to the ones for searching character. strstr is the equivalent of strchr for strings.

strstr will return a pointer to the first occurence of the substring. If the substring is not found, NULL shall be returned.

Here's an example on how to use it:
#include<stdio.h>
#include<string.h>

int main(void)
{
    char string[20] = "The bird is the word";

    char* birdOccurrence = strstr(string, "bird");
    char* angryBirdOccurrence = strstr(string, "angry bird");

    if(birdOccurrence!=NULL)
    {
        printf("The 'bird' substring was found starting at index %d\n",
               (birdOccurrence-string));
    }
    else
    {
        puts("The 'bird' substring was not found");
    }
    if(angryBirdOccurrence!=NULL)
    {
        printf("The 'angry bird' substring was found starting at index %d\n",
               (angryBirdOccurrence-string));
    }
    else
    {
        puts("The 'angry bird' substring was not found");
    }
    return 0;
}
/*Output
The 'bird' substring was found starting at index 4
The 'angry bird' substring was not found
 */
Another interesting function made available by the string.h is strspn.

strspn is very particular because it returns the length of the initial portion consisting only of characters that are part of the key string. If not character from the key string is found, it shall return 0.

Here's an example:
#include<stdio.h>
#include<string.h>

int main(void)
{
    char string[20] = "the bird is the word";

    size_t initialPortionLength = strspn(string, "abcdefghijklmnopqrt ");

    if(initialPortionLength!=0)
    {
        printf("The first %d characters exist in the key string",
                initialPortionLength);
    }
    else
    {
        puts("The first character was not found in the key string");
    }
    return 0;
}
/*Output
The first 10 characters exist in the key string
 */

TIP: As with all char* returning functions from string.h, you can apply pointer arithmetic to strstr in order to determine the index.
PITFALL: The result is unpredictable if any of the strings are not null terminated. This is true for both strspn and strstr.

QuestionWhat tips & tricks you know related to string searching operations?

References:
http://www.cplusplus.com/reference/cstring/

Working with Strings in ANSI C

Compared to other languages, C doesn't have a built-in string structure. Instead, character arrays are used:
char str[20] = "The bird is the word";
The string above is represented into memory like in the table below:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
T
h
e

b
i
r
d

i
s

t
h
e

w
o
r
d
/0
where '/0' represents the string's terminator (this is how C knows where your string ends).

ANSI C also provides a library, string.h which can be used for all sort of string operations such as:
-Getting the length of string : strlen
-Concatenating two strings: strcat, strncat
-Copying data to a string: strcpy, strncpy
-Comparing two strings: strcmp, strncmp

Getting the length of a string
strlen can be used for computing the length of a string. The length of the string is dictated by the position of its null terminator. Here's an example:
#include<stdio.h>
#include<string.h>

int main(void)
{
   char str[20] = "The bird is the word";
   size_t length = strlen(str);
   printf("Length of the string: %u\n", length);
   return 0;
}
/*Output:
Length of the string: 20
*/
TIP: The length is returned as a size_t datatype. The size of size_t is platform dependent (it can be an int, unsigned int, long, etc), but in most cases you can consider it as unsigned int.
PITFALL: The length of a string is determined by the position of the '\0' character.  If you pass a string who doesn't have a null terminator, the result is unpredictable.

Concatenating two strings
strcat and strncat the functions that can be used for concatenating 2 strings. The main difference between them is that strncat allows you to specify how many characters from the second string will be appended to the first string.
Here's an example on how to use strcat:
#include<stdio.h>
#include<string.h>

int main(void)
{
   char dest[25]      = "The bird is";
   char source[10]    =" the word";
   strcat(dest,source);
   printf("Destination string: %s\n"
          "Source string     : %s",
           dest,source);
   return 0;
}
/*Output:
Destination string: The bird is the word
Source string     :  the word
 */
TIP: strcat also returns a pointer to the destination string. Here's an example:
#include<stdio.h>
#include<string.h>

int main(void)
{
   char dest[25]      = "The bird is";
   char source[10]    =" the word";
   char *dest2 = NULL;
   /*dest2 will point now to the dest string*/
   dest2 = strcat(dest,source);
   printf("Destination string  : %s\n"
          "Source string       : %s\n"
          "Destination pointer : %s\n",
          dest,source,dest2);
   return 0;
}
/*Output:
Destination string  : The bird is the word
Source string       :  the word
Destination pointer : The bird is the word
 */
strncat allows you to specify how many characters will be appended through its third parameter:
#include<stdio.h>
#include<string.h>

int main(void)
{
   char dest[25]      = "The bird is";
   char source[10]    =" the word";
   /*Only the first 4 characters from source will be appended to dest*/
   strncat(dest,source,4);
   printf("Destination string  : %s\n"
          "Source string       : %s\n",
          dest,source);
   return 0;
}
/*Output:
Destination string  : The bird is the
Source string       :  the word
 */
TIP:Both string must be null-terminated, otherwise the result is unpredictable and may even trigger memory errors.
TIP:Be sure that the character array that holds your destination string is large enough to support the operation. Otherwise, a buffer overflow error might occur.

Copying data to a string
strcpy and strncpy the functions that can be used for concatenating 2 strings.  The main difference between them is that strncpy allows you to specify how many characters from the second string will be copied to the first string (just like strcat and strncat).

Here's an example on how to use strcpy:
#include<stdio.h>
#include<string.h>

int main(void)
{
   char dest[25]      = "The bird is";
   char source[10]    =" the word";
   strcpy(dest,source);
   printf("Destination string  : %s\n"
          "Source string       : %s\n",
          dest,source);
   return 0;
}
/*Output:
Destination string  :  the word
Source string       :  the word
 */
And here's an example on how to use strncpy:
#include<stdio.h>
#include<string.h>

int main(void)
{
   char dest[25]      = "The bird is";
   char source[10]    =" the word";
   /*Only the first 9 characters from source will be overwritten in dest*/
   strncpy(dest,source,8);
   printf("Destination string  : %s\n"
          "Source string       : %s\n",
          dest,source);
   return 0;
}
/*Output:
Destination string  :  the wor is
Source string       :  the word
 */
TIP:The source string must be null-terminated, otherwise the result is unpredictable and may even trigger memory errors.
TIP:Be sure that the character array that holds your destination string is large enough to support the operation. Otherwise, a buffer overflow error might occur.
TIP: strcpy destroys the destination string, while strncpy overwrites only the first n characters with the first n characters from the source string.
PITFALL: If you use dynamically allocated character arrays, a common mistake is to use the assignment operator instead of strcpy/strncpy. Using = will create a shallow copy of your first string. This means that when your first string will be modified, your second string will be modified as well (since both pointers point to the same memory zone). Here's an example:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(void)
{
    const int LENGTH = 20;

    char* myString = NULL;
    char* shallowCopy = NULL;
    char* deepCopy = NULL;

    //Allocates memory for the strings
    myString = malloc(sizeof(char)*LENGTH);
    shallowCopy = malloc(sizeof(char)*LENGTH);
    deepCopy = malloc(sizeof(char)*LENGTH);
    //Copies into the first string its value
    strcpy(myString, "String text");
    //Creates a SHALLOW copy of the first string
    shallowCopy = myString;
    //Creates a DEEP copy of the first string
    strcpy(deepCopy, myString);

    //Making a modification in the first string that will
    //propagate through shallow copy.
    myString[0]='Z';

    puts(myString);
    puts(shallowCopy);
    puts(deepCopy);

    return 0;
}
/*Output
Ztring text
Ztring text
String text
 */
Comparing two strings
Strings are compared lexicographically, character by character using the functions strcmp and strncmp.

The difference between them is that strmcp compares the two strings character by character until the a string terminator is encountered in one of the strings, while strncmp compares only the first n characters.

The return value of the functions is calculated in the following way:
-if the strings are equal from a lexicographic point of view, it returns 0
-if the strings are not equal from a lexicographic point of view and the functions find a character in the first string who has has a bigger ASCII value than the character in the second string at the same position it will return a positive value
-if the are not equal from a lexicographic point of view and a the functions finds a character in second string who has has a bigger ASCII value than the character in first string at the same position it will return a negative value.

Here's an example on how to use these functions:
#include<stdio.h>
#include<string.h>

int main(void)
{
   char string1[10]    ="the ward";
   char string2[10]    ="the word";
   int cmp1 = strcmp(string1,string2);
   int cmp2 = strncmp(string1,string2,3);
   printf("Cmp1 = %d\nCmp2 = %d",cmp1,cmp2);
   /*
     cmp1 = -1 "the ward" < "the word" because 'a'<'o'
     cmp2 =  0  "the" = "the"
   */
   return 0;
}
/*Output:
Cmp1 = -1
Cmp2 = 0
 */
TIP:Both string must be null-terminated, otherwise the result is unpredictable and may even trigger memory errors.
TIP: If strncmp encounters a string terminator before the n-th character, strncmp will act like strcmp.
TIP: If you want to check if two strings are equal, use the clause (strcmp(str1,str2)==0)

Question: What tips & tricks do you know related to string operations?

References:
http://www.cplusplus.com/reference/cstring/
Related Posts Plugin for WordPress, Blogger...