C/Data Type/Char
Содержание
- 1 Assing int value to a char type variable
- 2 Characters and numbers: output
- 3 Char: Converting uppercase to lowercase
- 4 Char to lower case
- 5 Char: to upper case
- 6 Double check before erasing
- 7 Encrypting a password
- 8 For loop with char as the loop condition
- 9 Get char and change it to float
- 10 maximum and minimum value of char
- 11 Output char to the console
- 12 Use data type: char
Assing int value to a char type variable
#include <stdio.h>
int main(void)
{
char ch;
int i;
i = 1000;
ch = i;
printf("%d", ch);
return 0;
}
Characters and numbers: output
/*Characters and numbers */
#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);
}
Char: Converting uppercase to lowercase
/* Char: Converting uppercase to lowercase */
#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");
}
Char to lower case
#include <stdio.h>
#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. ");
}
Char: to upper case
#include <stdio.h> /* For input and output */
#include <ctype.h> /* For toupper() function */
#include <stdlib.h> /* For rand() and srand() functions */
#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));
}
Double check before erasing
#include <stdio.h>
#include <stdlib.h>
#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;
}
Encrypting a password
#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);
}
For loop with char as the loop condition
#include <stdio.h>
int main() {
char ch;
for ( ch = "a"; ch <= "z" ; ch++ ) {
printf ( "%c", ch );
}
printf ( "\n" );
}
Get char and change it to float
#include <stdlib.h>
#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;
}
maximum and minimum value of char
#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);
}
Output char to the console
#include <stdio.h>
int main(void)
{
putchar("A");
putchar("\n");
putchar("B");
return 0;
}
Use data type: char
/* Use data type: char */
#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);
}