C/Data Type/Char

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Assing int value to a char type variable

<source lang="cpp">

  1. include <stdio.h>

int main(void) {

 char ch;
 int i;
 i = 1000;
 ch = i;
 printf("%d", ch);
 return 0;

}


      </source>


Characters and numbers: output

<source lang="cpp"> /*Characters and numbers */

  1. include <stdio.h>

void main() {

 char first = "Y";
 char second = 40;
 printf("\n first as a letter looks like this - %c", first);
 printf("\n first as a number looks like this - %d", first);
 printf("\n second as a letter looks like this - %c", second);
 printf("\n second as a number looks like this - %d\n", second);

}


      </source>


Char: Converting uppercase to lowercase

<source lang="cpp"> /* Char: Converting uppercase to lowercase */

  1. include <stdio.h>

void main() {

  char letter = 0;                         /* Stores a character */
  printf("Enter an uppercase letter:");  
  scanf("%c", &letter);                    /* Read a character   */
  /* Check whether the input is uppercase*/
  if(letter >= "A")                    /* Is is A or greater?   */
    if (letter <= "Z")                 /* and is it Z or lower? */
    {                                  /* It is upper case      */
      letter = letter - "A"+ "a";      /* Convert from upper to lower case */
      printf("An uppercase %c\n", letter);
    }
    else                               /* It is not an upper case letter */
      printf("A capital letter please.\n");

}


      </source>


Char to lower case

<source lang="cpp">

  1. include <stdio.h>
  2. include <ctype.h> /* For tolower() function */

void main() {

  char answer = "N";  /* Records yes or no to continue the loop */
    /* check for more input */
    printf("Do you want to enter another value? (Y or N): ");
    scanf(" %c", &answer );   
    if( tolower(answer) == "n" )    
        printf("You typed n. ");

}


      </source>


Char: to upper case

<source lang="cpp">

  1. include <stdio.h> /* For input and output */
  2. include <ctype.h> /* For toupper() function */
  3. include <stdlib.h> /* For rand() and srand() functions */
  4. include <time.h> /* For time() and clock() functions */

void main() {

  char another_game = "Y";
  printf("\nDo you want to play again (y/n)? ");
  scanf("%c", &another_game);
  printf("\n %c ",toupper(another_game));

}


      </source>


Double check before erasing

<source lang="cpp">

  1. include <stdio.h>
  2. include <stdlib.h>
  3. include <ctype.h>

int main(int argc, char *argv[]) {

 char str[80];
 if( argc != 2) {
   printf("usage: xerase <filename>\n");
   exit(1);
 }
 printf("Erase %s? (Y/N): ", argv[1]);
 gets(str);
 if(toupper( *str ) == "Y")
   if(remove( argv[ 1 ])) {
     printf("Cannot erase file.\n");
     exit(1);
   }
 return 0; 

}

      </source>


Encrypting a password

<source lang="cpp">

  1. include <stdio.h>

int main() {

 char unencrypted[] = "abcdefghijklmnopqrst";
 char encrypted[21];
 char password[80];
 int i = 0;
 long code = 0L;
 printf("\nEnter your password of up to 20 characters(no spaces):\n");
 scanf("%s", password);
  for(i = 0; i < 20 && password[i] != "\0"; i++)
     unencrypted[i] = password[i];
 for (i = 0 ; i<20 ; code += unencrypted[i++]);
 code %=  11;
 code += (code<26) ? "A" : ("a"-26);
 for (i = 0; i < 20; i++)
 {
    code *= unencrypted[i];
    code %= 11;
    code += (code<26) ? "A": ("a"-26);
    encrypted[i] = (char)code;
}
encrypted[i] = "\0";
printf("\nEncrypted password is: %s\n", encrypted);

}


      </source>


For loop with char as the loop condition

<source lang="cpp">

  1. include <stdio.h>

int main() {

 char ch;
 for ( ch = "a"; ch <= "z" ; ch++ ) {
      printf ( "%c", ch );
 }
 printf ( "\n" );

}


      </source>


Get char and change it to float

<source lang="cpp">

  1. include <stdlib.h>
  2. include <stdio.h>

int main(void) {

 char num1[80], num2[80];
 printf("Enter first: ");
 gets(num1);
 
 printf("Enter second: ");
 gets(num2);
 printf("The sum is: %lf.", atof(num1) + atof(num2));
 return 0;

}


      </source>


maximum and minimum value of char

<source lang="cpp">

  1. include <stdio.h>

main() {

   char i,j ;
   i = 1;
   
   while (i > 0) {
       j = i; 
       i++;  
   }
   printf ("Maximum value of char is %d\n",j);
   printf ("The value of char after overflow is %d\n",i);

}

      </source>


Output char to the console

<source lang="cpp">

  1. include <stdio.h>

int main(void) {

 putchar("A");
 putchar("\n");
 putchar("B");
 return 0;

}


      </source>


Use data type: char

<source lang="cpp"> /* Use data type: char */

  1. include <stdio.h>

void main() {

 char first = "A";
 char second = "B";
 char last = "Z";
 char number = 40;
 char ex1 = first + 2;             /* Add 2 to char       */
 char ex2 = second - 1;            /* Subtract 1 from char*/
 char ex3 = last + 2;              /* Add 2 to char       */
 printf(" Character values      %-5c%-5c%-5c", ex1, ex2, ex3);
 printf("\n Numerical equivalents value for above chars %-5d%-5d%-5d", ex1, ex2, ex3);
 printf("\nThe number %d is the code for the character %c\n", number, number);

}


      </source>