Tag Archives: array

C Programming Tutorial – 15 – Extract a character from an array + compare it with a character :

Published by:

Note:

  • //lastname[3] : Extract the 4th (not the 3rd) character of array lastname.
  • //lastname[3] > ‘M’   : compare the 4th character with character M.

Code:
—————————

#include <stdio.h>
#include <stdlib.h>

int main()
{

char lastname[20];
printf(“Enter your last name ? \n”);
scanf(” %s”, lastname);
if (lastname[3] > ‘M’) { // character extract and character comparison.
printf(“You are in team Blue \n”);
} else {
printf(“You are in team Red \n”);
}

return 0;

}

C Programming Tutorial – 8 – Get data from keyboard scanf():

Published by:

Note:

  • // scanf() :  get data from keyboard or user.
  • // Use & before all of the variables, except Array variables.

Code:
—————————

#include <stdio.h>
#include <stdlib.h>

int main()
{

char firstname[20];
char lovename[20];
int NumberOfBaby;
printf(“What is your name?\a\n”);
scanf(“%s”,firstname);                                                           // scanf get data from keyboard or user
printf(“What is your love name?\a\n”);
scanf(“%s”,lovename);
printf(“How many baby you will have?\a\n”);
scanf(“%d”, &NumberOfBaby);                     // Use & before all of the variables except Array variables.
printf(“%s and %s are in love. They will have %d babies\n”, firstname,lovename ,NumberOfBaby );

return 0;

}

C Programming Tutorial – 5 – Define an Array + replace a character in an array :

Published by:

Note:

  • //Define array:
    char name[15]= “Sajjad Baghaee”;
  • // replacing a character in an array : 
    name[2]= ‘C’;
  • Array start by 0 so S=0 a=1  j=2  j=3  a =4  d=5 =6  B=7  a=8  g=9  h=10  a=11  e=12  e=13

Code:
—————————

#include <stdio.h>
#include <stdlib.h>

int main()
{

char name[15]= “Sajjad Baghaee”;
printf(“This is %s \n”, name);
name[2]= ‘C’;                  // replacing the 1st character “j” in array “Sajjad Baghaee”  with “C”
name[3]=’c’;                   // replacing the 2nd character “j” in array “Sajjad Baghaee”  with “c”
printf(“This is %s\n”,name);

return 0;

}