0% found this document useful (0 votes)
27 views30 pages

Characters and Strings

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views30 pages

Characters and Strings

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

CHARACTERS

AND STRINGS
OBJECTIVES:
• Learn how characters are represented in C
• Explore string handling in C
• Master the input and output operations for characters and
strings in C
STRINGS
• A string in C is an array of characters.
• The length of a string is determined by a terminating null character: '\0' .
• So, a string with the contents, say, “hello" has six characters:
‘h' , ‘e' , ‘l' , ‘l' , ‘o' and the terminating null ( '\0‘ ) character.
• The terminating null character has the value zero.
• Series of characters treated as a single unit
- Can include letters, digits and special characters (*, /, $)

• String literal (string constant) - written in double quotes


- "Hello“

• String a pointer to first character

• Value of string is the address of first character


String Declaration:

Syntax:
char stringName [numOfCharacters];
Example:
char s1[10]; char lname[15],fname[15],mi[2];
String Initialization:
Syntax:
char stringName[ ]=“stringValue”;

Example:
char name[]={‘L’, ‘a’, ‘d’, ‘\0’};
is the same as
char name[]=“Lad”;

char name[]=“Lad”;
Or alternatively, we can write
char name[]={‘L’, ‘a’, ‘d’, ‘\0’};
String Input/Output:
• Input Function:
-We use the scanf() or the gets() functions. Example: scanf(“%s”,&s1); or gets(s1);

• Output Function:
-We use the printf() or the puts() functions. Example: printf(“\n s1=%s”,s1); or
puts(s1);

• Format Specifier:
%s
Fundamentals of Strings:
• String definitions
-Define as a character array or a variable of type char *
char color[] = "blue";
char *colorPtr = "blue";
-Remember that strings represented as character arrays end with '\0’
color has 5 elements
• Inputting strings
-Use scanf
scanf("%s", word);
• Copies input into word[]
• Do not need & (because a string is a pointer)
-Remember to leave room in the array for '\0'
Character Handling Library:
• Character handling library
-Includes functions to perform useful tests and manipulations
of character data

• The following slide contains a table of all the functions in


<ctype.h>
Prototype Description
int isdigit( int c );
Returns true if c is a digit and false otherwise.
int isalpha( int c );
Returns true if c is a letter and false otherwise.
int isalnum( int c );
Returns true if c is a digit or a letter and false otherwise.
int isxdigit( int c );
Returns true if c is a hexadecimal digit character and false otherwise.
int islower( int c );
Returns true if c is a lowercase letter and false otherwise.
int isupper( int c );
Returns true if c is an uppercase letter; false otherwise.
int tolower( int c );
If c is an uppercase letter, tolower returns c as a lowercase letter. Otherwise, tolower
returns the argument unchanged.
int toupper( int c );
If c is a lowercase letter, toupper returns c as an uppercase letter. Otherwise, toupper
returns the argument unchanged.
int isspace( int c );
Returns true if c is a white-space character—newline ('\n'), space (' '), form feed
('\f'), carriage return ('\r'), horizontal tab ('\t'), or vertical tab ('\v')—and false
otherwise
int iscntrl( int c );
Returns true if c is a control character and false otherwise.
int ispunct( int c );
Returns true if c is a printing character other than a space, a digit, or a letter and false
otherwise.
int isprint( int c );
Returns true value if c is a printing character including space (' ') and false otherwise.
int isgraph( int c );
Returns true if c is a printing character other than space (' ') and false otherwise.
Practical exercise - calculator
Objective:
Introduce participants to basic coding concepts by building a simple calculator

Instructions:
1. Open a Python development environment and write the following code:
# Simple Calculator
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
print("Sum:", num1 + num2)
print("Difference:", num1 - num2)
print("Product:", num1 * num2)
print("Quotient:", num1 / num2)
2. Run the program and experiment with different numbers
3. Observe the output

You might also like