NF06A - 1, 2 - Basic Concepts, Arrays, Strings
NF06A - 1, 2 - Basic Concepts, Arrays, Strings
Sandy Mahfouz
Office H103
sandy.mahfouz@utt.fr
1
Course Overview:
Foundations of Programming in C and Python
Part 1: Programming in C
This part of the course focuses on the fundamentals of programming in C language, covering essential
concepts such as types, operators, control structures, arrays and strings.
Advanced topics include:
• Pointers and dynamic memory allocation
• Functions and recursion
• Structures
• Linked lists
• File handling
2
Course Overview:
Foundations of Programming in C and Python
Part 2: Programming in Python
• Python Basics: Understanding fundamental concepts such as variables, data types, operators, and
control structures
• Functions and modules
• Data manipulation and file handling
• Essential Python libraries such as NumPy, pandas, and Matplotlib
• Principles of object-oriented programming in Python
3
Course Overview:
Foundations of Programming in C and Python
Part 3: Project
Course Evaluation:
• Midterm 35%
• Final 35%
• Project 30%
4
PRINTEMPS 2025
Février Mars Avril Mai Juin
1 S 1 S 1 M 1 J Fête du travail 1 D
2 D 2 D 2 Me 2 V 2 L Lab 3 - Python
INTERSEMESTRE
3 L 3 L Lab 1 - C 3 J Lecture 6 - C 3 S 3 M
4 M 4 M 4 V Lab 5 - C 4 D 4 Me
5 Me 5 Me 5 S 5 L 5 J Lecture 4 - Python
6 J 6 J Lecture 2 - C 6 D 6 M 6 V Lecture 5 - Python P Jeudi
7 V 7 V Lab 1 - C 7 L 7 Me Lecture 2 - Python P Jeudi 7 S
8 S 8 S 8 M Vacances 8 J Victoire 1945 8 D
9 D 9 D 9 Me de 9 V Lab 1 - Python 9 L Lundi de Pentecôte
10 L 10 L Lab 2 - C 10 J Printemps 10 S 10 M
11 M 11 M 11 V 11 D 11 Me
12 Me Vacances d'hiver 12 Me 12 S 12 L Lab 1 - Python 12 J Lecture 6 - Python
13 J 13 J Lecture 3 - C 13 D 13 M 13 V Lab 3 - Python
14 V 14 V Lab 2 - C 14 L Lab 6 - C 14 Me 14 S
15 S 15 S 15 M 15 J Lecture 3 - Python 15 D
16 D 16 D 16 Me 16 V Lab 2 - Python 16 L Lab 4 - Python
17 L 17 L Lab 3 - C 17 J Lecture 1 - Python 17 S 17 M
18 M 18 M 18 V Lab 6 - C 18 D 18 Me
19 Me 19 Me 19 S 19 L Lab 2 - Python 19 J Lecture 7 - Python
20 J 20 J Lecture 4 - C 20 D 20 M 20 V Lab 4 - Python
21 V 21 V Lab 3 - C 21 L Lundi de Pâques 21 Me 21 S Finals
22 S 22 S 22 M 22 J 22 D
23 D 23 D 23 Me 23 V Projects 23 L
24 L 24 L Lab 4 - C 24 J Midterm 24 S 24 M
25 M 25 M 25 V Lab 7 - C 25 D 25 Me
Finals
26 Me 26 Me 26 S 26 L Projects 26 J
27 J Lecture 1 - C 27 J Lecture 5 - C 27 D 27 M 27 V
28 V 28 V Lab 4 - C 28 L Lab 7 - C 28 Me 28 S
29 S 29 M 29 J Jeudi de l'Ascension 29 D
30 D 30 Me 30 V Pont 30 L
31 L Lab 5 - C 31 S
5
The calendar is provided as a first draft but is subject to change...
Topics covered in this set of slides (two lectures)
• Programming languages
• Our first C program (with printf)
• Variables and data types
• Arithmetic operators
• scanf
• Equality and relational operators
• Logical operators
• Control structures
• Decision making (conditions): if, if-else, switch
• Iteration statements: for loop, while loop, do-while loop
• Arrays
• Strings (gets, puts, functions from string.h)
6
High-level languages
• A high-level programming language provides powerful, descriptive, and human-readable commands, often
resembling English or mathematical notation (Python, Java, …).
• To execute a program written in a high-level language, it must be translated into machine language (0s and 1s).
• This translation is done by either a compiler or an interpreter:
• A compiler translates the entire program into machine code before execution, creating an independent
executable file. C is a compiled language.
• An interpreter translates and executes the program line by line at runtime, without generating a separate
executable. Python is an interpreted language.
• A high-level code is independent from the machine’s architecture, so it is portable, meaning it can run on different
systems with no modification.
7
The C language
• C is widely regarded as a middle-level programming language due to its unique blend of features.
• It includes features such as functions, control structures (loops, conditionals), and data types (arrays, structures) that
make it easier to write and understand code.
• C allows direct manipulation of hardware resources through pointers and memory addresses. This direct access to
memory and hardware registers is a characteristic of low-level languages, providing programmers with control over
system-level operations.
• C is portable across different hardware platforms – a characteristic of high-level languages.
8
Our first C program
• // indicate that the line is a comment. Comments document programs and improve program readability. They are
ignored by the C compiler, and do not generate any action.
• You can also use /*…*/ multi-line comments in which everything from /* on the first line to */ at the end of the line is a
comment.
• #include <stdio.h> This line includes the standard input-output library (stdio.h), which contains functions like
printf and scanf for input and output operations.
• int main(): This is the main function of our program. In C, every program must have a main function where
execution begins. The keyword int to the left of main indicates that main “returns” an integer (whole number) value.
Main does not accept any arguments in this case.
9
Our first C program
• return 0; This statement returns an integer value of 0 from the main function, indicating that the program executed
successfully (successful termination), while a non-zero value indicates an error or abnormal termination (covered later
in the course).
• Omitting the return 0; statement at the end of main is acceptable, and the compiler will automatically insert it for
you. The C standard specifies that the compiler should assume return 0; is present.
• Some compilers might issue a warning if they detect a missing return 0; at the end of main, but they will still
compile the code successfully and insert the return statement automatically.
• This entire line with the semicolon (;) is called a statement.
• The semicolon is known as the statement terminator.
10
Our first C program
• A left brace, {, begins the body of every function, and a corresponding right brace ends each function.
• This pair of braces and the portion of the program between the braces is called a block.
• printf("Hello world!\n"); instructs the computer to print on the screen the string of characters marked by the
quotation marks. The f in printf stands for formatted.
Escape sequence Description
Escape Sequences \n New line
• The backslash (\) is called an escape character. \t Horizontal tab
• Operators specify how an object can be manipulated (Example: addition (+) and multiplication (*) for numeric data).
• A variable is a container for storing data that can be manipulated or used by the program.
• Variables have a name (identifier), a data type, and a value.
char Represents a single character, such as 'a', 'B', or '@', typically occupies 1 byte (1 byte = 8 bits).
int Represents integer numbers (whole numbers) typically with a size of 4 bytes on most systems.
Integer short Represents short integer numbers, usually smaller than int.
types long Represents long integer numbers, often larger than int.
long long Represents very long integer numbers, providing a larger range than long.
float Represents single-precision floating-point numbers, typically occupies 4 bytes.
Floating- double Represents double-precision floating-point numbers, offering higher precision than float,
point usually occupies 8 bytes.
types long double Usually occupies 10 to 12 bytes, providing even higher precision than double.
unsigned … used with (int, short, long, long long, char). It means that the variable can only hold positive
values or zero. It cannot represent negative numbers.
void Represents an absence of type or a generic pointer type.
13
Data types
• The sizes in bytes may vary from platform to platform, but we always have the following:
sizeof(char) < sizeof(short) ≤ sizeof(int) ≤ sizeof(long) ≤ sizeof(long long)
sizeof(char) < sizeof(short) ≤ sizeof(float) ≤ sizeof(double) ≤ sizeof(long double)
• The size of a type in bytes can be found using the sizeof operator.
• When used in printf or scanf, the types will be represented as follows:
14
Data types
• The ranges of data types can vary depending on the specific system and compiler being used.
• Example of ranges:
• int: Typically ranges from -2,147,483,648 to 2,147,483,647 on most systems using 4 bytes for storage (32 bits).
• short: Typically ranges from -32,768 to 32,767 (16 bits).
• long: Typically ranges from -2,147,483,648 to 2,147,483,647 (32 bits).
• long long: Typically ranges from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (64 bits).
• unsigned int: Typically ranges from 0 to 4,294,967,295 (32 bits).
• You can use the <limits.h> header to obtain specific information about the ranges and sizes of integer types, and
<float.h> for floating-point types.
• For instance, you can use the FLT_MIN, FLT_MAX, DBL_MIN, DBL_MAX, and similar constants defined in <float.h> to
obtain the minimum and maximum representable values for floating-point types.
15
Data types
How are characters stored?
• Characters in programming languages like C are stored using character encoding schemes such as ASCII (American
Standard Code for Information Interchange) or Unicode. Each character is represented by a numeric code, which is
stored as an integer value in memory.
• For example, the character 'A' would be stored as the ASCII code 65 in memory.
https://www.ibm.com/docs/en/sdse/6.4.0?topic=configuration-ascii-characters-from-33-126
16
Identifiers
• A variable name or a function name in C is any valid identifier.
• An identifier is a series of characters consisting of letters (letters A-Z, a-z), digits (0-9) and underscores (_) that does
not begin with a digit. Spaces are not allowed.
• C is case sensitive—uppercase and lowercase letters are different in C, so a1 and A1 are different identifiers.
• A keyword cannot be an identifier.
17
Arithmetic operators
Example:
int y = 5+2*3;
Or
int y = 5+(2*3);
5 6 11
n1 n2 sum
Output:
printf("The sum is %d.", sum);
- calls function printf to print on the standard output (monitor).
- This printf has two arguments: "The sum is %d." and sum.
- The first argument is the format control string. It contains text to be displayed, and it contains the conversion
specifier %d indicating that an integer will be printed (the letter d stands for “decimal integer”).
- The second argument specifies the value to be printed.
19
Addition example 2 – introducing scanf
21 35
n1 n2
Output:
scanf("%d", &n1);
- calls scanf to read a value from the user from the standard input (the keyboard).
- This scanf has two arguments, "%d" and &n1.
- The first, the format control string, indicates the type of data that should be input by the user. The %d conversion
specifier indicates that the data should be an integer.
- The second argument of scanf begins with an ampersand & called the address operator in C—followed by the
variable name.
- The &, when combined with the variable name, tells scanf the location (or address) in memory at which the
variable n1 is stored. The computer then stores the value that the user enters for n1 at that location. 20
Typecasting
• Typecasting in C is the process of converting a value from one data type to another.
• Implicit Typecasting: In implicit typecasting, the compiler automatically converts one of the operands to a higher data
type to perform the operation. For example, when you add an integer and a floating-point number, the integer is
automatically promoted to a floating-point number before the addition.
• Example:
int x = 5; float y = 3.14;
float sum = x + y; // Implicit typecasting: x is promoted to a float before addition
• Explicit Typecasting: Explicit typecasting is performed manually by the programmer using the typecast operator (type).
It allows you to explicitly convert a value from one data type to another. This can be useful when you want to force a
conversion that might otherwise result in loss of precision or data.
• Example: two integers, but the result should be a float
int x = 5;
float y = (float)x/2;
This explicit typecasting ensures that the division is performed using floating-point arithmetic instead of integer
arithmetic, which preserves the decimal part of the result.
Without the explicit typecasting (float)x, the division would be performed using integer arithmetic, resulting in 2
instead of 2.5. 21
Decision Making: Equality and Relational operators
• Equality and relational operators compare two operands to produce a boolean result. In C, any non-zero value (1 by
convention) is true and 0 is false.
22
Decision Making: Logical operators
• Logical operators allow you to combine multiple conditions or expressions and evaluate them as a single Boolean result.
Operator Syntax Meaning/Description Examples Evaluates to
&& expr1 && expr2 Logical AND ('A'=='a') && (3==3) 0
Returns true if both expr1 and expr2 ((9/3)==3) && (2∗3==6) 1
are true; otherwise, it returns false.
|| expr1 || expr2 Logical OR 2==3 || 'A'=='A' 1
Returns true if either expr1 or expr2 2.99>=3 || ('A'=='a') 0
(or both) are true; returns false only
if both expr1 and expr2 are false.
! !expr Logical NOT !(3==3) 0
Returns true if expr is false, and !(2>3) 1
returns false if expr is true.
• Short circuit evaluation: The evaluation of an expression is discontinued if the value of a conditional expression can be
determined early.
23
Decision Making and Control Structures
• An if statement allows a program to make a decision based on the truth or falsity of a condition.
• If the condition is true (i.e., the condition is met) the statement in the body of the if statement is executed.
• If the condition is false (i.e., the condition isn’t met) the body statement is not executed.
• Whether the body statement is executed or not, after the if statement completes, execution proceeds with the next
statement after the if statement.
• Conditions in if statements are formed by using the equality operators and relational operators.
24
Decision Making and Control Structures
• Let us write a small program that asks the user to enter his grade (float). If the grade is greater than or equal to 50, the
program should print the message “Congratulations! You passed.”
Output sample 1
Output sample 2
• Adjust the previous program to do the following: If the grade is greater than or equal to 50, the program should print
the message “Congratulations! You passed.”. Otherwise, the program should print “Sorry; you failed.”.
Output sample 1
Output sample 2
26
Decision Making and Control Structures
• C provides the conditional operator (?:) which is closely related to the if…else statement.
• The conditional operator is C’s only ternary operator—it takes three operands.
• These together with the conditional operator form a conditional expression.
• The first operand is a condition. The second operand is the value for the entire conditional expression if the condition is
true and the third operand is the value for the entire conditional expression if the condition is false.
condition ? expression_if_true : expression_if_false;
27
Decision Making and Control Structures
• Nested if…else statements test for multiple cases by placing if…else statements inside if…else statements.
• For example, the following statements will print A for exam grades greater than or equal to 90, B for grades greater
than or equal to 80 (but less than 90), C for grades greater than or equal to 70 (but less than 80), D for grades greater
than or equal to 60 (but less than 70) and F for all other grades.
if ( grade >= 90 ) if ( grade >= 90 )
printf( "A" ); printf( "A" );
else
if ( grade >= 80 ) else if ( grade >= 80 )
printf("B"); printf( "B" );
else or else if ( grade >= 70 )
if ( grade >= 70 ) printf( "C" );
printf("C");
else else if ( grade >= 60 )
if ( grade >= 60 ) printf( "D" );
printf( "D" ); else
else printf( "F" );
printf( "F" );
• Both forms are equivalent. The right form is popular because it avoids the deep indentation of the code to the right.
• If you have only one statement in the if’s body, you do not need to use braces.
• To include several statements in the body of an if, you must enclose the set of statements in braces ({ and }).
• Remember that a set of statements contained within a pair of braces is called a block or a compound statement. 28
Decision Making and Control Structures
• C provides the switch multiple-selection statement to handle situations where a single variable or expression needs
to be tested against multiple constant integral values, and different actions are taken based on the value of the
variable or expression.
• The switch statement consists of a series of case labels, an optional default case and statements to execute for
each case as follows:
switch (expression) {
case constant1:
// Statements to execute if expression equals constant1
break;
case constant2:
// Statements to execute if expression equals constant2
break;
// More case labels as needed
default:
// Statements to execute if expression does not match any case
}
• If the value of the expression matches a case label, the statements following that case label are executed until a break
statement is encountered or until the end of the switch block is reached. If a break statement is omitted, control will
flow into the statements of the next case label (a behavior known as "fall-through").
• The break statement is used to exit the switch block after executing the statements associated with the matching29case.
Decision Making and Control Structures
• Write a C Program that does the following:
• Ask the user to enter an operator (+, -) and two integer operands,
• Using a switch, add or subtract the numbers and print the result.
Output sample
30
Control Structures – Iteration statements
• In C programming, iteration statements are used to execute a block of code repeatedly as long as a specified condition
is true. There are three main types of iteration statements in C:
• for loop: The for loop allows you to execute a block of code repeatedly for a fixed number of times. It consists of three
parts: initialization, condition, and increment/decrement:
for (initialization; condition; increment/decrement) {
// Code to be executed repeatedly
}
• while loop: The while loop repeats a block of code as long as the specified condition evaluates to true. The condition is
evaluated before each iteration:
while (condition) {
// Code to be executed repeatedly
}
• do-while loop: The do-while loop is similar to the while loop, but the condition is evaluated after the block of code is
executed, ensuring that the code is executed at least once:
do {
// Code to be executed repeatedly
} while (condition);
31
Control Structures – Iteration statements – for loop
• for loop: The for loop allows you to execute a block of code repeatedly for a fixed number of times. It consists of three
parts: initialization, condition, and increment/decrement:
for (initialization; condition; increment/decrement) {
// Code to be executed repeatedly
}
• Initialization: This part is executed only once at the beginning of the loop. It initializes the loop control variable or
variables. The loop control variable can also be declared here (see next slide), but then this variable will only be
accessible within the loop.
• Condition: This is the condition that is evaluated before each iteration of the loop. If the condition evaluates to true,
the loop continues; if it evaluates to false, the loop terminates.
• Increment/Decrement: This part is executed at the end of each iteration of the loop. It updates the control variable.
• The block of code inside the curly braces {} is the body of the loop. It contains the statements that are executed
repeatedly.
32
Control Structures – Iteration statements – for loop
• for loop: The for loop allows you to execute a block of code repeatedly for a fixed number of times. It consists of three
parts: initialization, condition, and increment/decrement:
for (initialization; condition; increment/decrement) {
// Code to be executed repeatedly
}
• Example:
#include <stdio.h> Output:
int main() {
for (int i=0; i<=3; i++) {
printf("%d ", i);
} // end for
return 0;
}
33
Control Structures – Iteration statements – for loop
• The following examples show methods of varying the control variable in a for statement.
34
Control Structures – Iteration statements – while loop
• while loop: The while loop repeats a block of code as long as the specified condition evaluates to true. The condition is
evaluated before each iteration:
while (condition) {
// Code to be executed repeatedly
}
35
Control Structures – Iteration statements – while loop
• A while loop is often used when you do not know in advance how many times you want to execute the loop, but you
know the condition under which you want to continue looping.
Sample output 1:
Sample output 2:
36
Control Structures – Iteration statements – do-while loop
• do-while loop: The do-while loop is like the while loop, but the condition is evaluated after the block of code is
executed, ensuring that the code is executed at least once:
do {
// Code to be executed repeatedly
} while (condition);
• Example:
#include <stdio.h>
Output:
int main() {
int i = 0;
do {
printf("%d ", i);
i++;
} while(i<=3);
return 0;
}
37
Control Structures – Iteration statements – do-while loop
• Another simple example of a do-while loop that prompts the user to enter a positive number:
Sample output 1:
Sample output 2:
• In this example:
• The do-while loop ensures that the prompt for user input is displayed at least once.
• The loop continues prompting the user to enter a positive number until a positive number is entered.
• If the user enters a non-positive number, an error message is displayed, and the loop continues.
38
break and continue statements
• break and continue are control flow statements used in loops (such as for, while, and do-while) to alter the flow of
execution.
Output:
39
break and continue statements
• The continue statement is used to skip the rest of the loop's body for the current iteration and proceed with the next
iteration of the loop.
• When encountered inside a loop, the continue statement immediately jumps to the next iteration of the loop.
Output:
40
goto statement
• The goto statement in C is a control flow statement that allows you to transfer the program's control to a labeled
statement within the same function, bypassing normal execution flow.
• While the goto statement can be useful in certain situations, it is generally discouraged due to its potential to create
complex and unreadable code (unstructured).
• Here's the syntax of the goto statement: • Example:
goto label;
…
label:
// Statement or block of code
Output:
41
Formatting the output in printf
42
Precedence and order of evaluation
• Precedence defines the order in which different types of operators are evaluated in an expression. Operators with
higher precedence are evaluated first.
• The table shows common operators and their general precedence from higher to lower:
Operators Associativity
() parentheses, ++(postfix), --(postfix) Left to right
high
+ (unary), - (unary), ++(prefix), --(prefix), ! (logical NOT), sizeof Right to left
*, /, % (multiplication, division, remainder) Left to right
+, - (addition, subtraction) Left to right
<, <=, >, >= (relational operators) Left to right
==, != (equality operators) Left to right
&& (logical AND) Left to right
|| (logical OR) Left to right
?: (conditional operator) Right to left
low =, +=, -=, *=, /=, %= (assignment and compound assignments) Right to left
• Use () generously to avoid ambiguities or side effects associated with precedence of operators.
43
1-dimensional arrays
• An array is a data structure that allows you to store a collection of elements of the same data type.
• The elements have contiguous memory locations.
• To declare an array, you specify the data type of the elements and the number of elements in square brackets [ ].
• Suppose we want to declare and initialize an array of daily average temperatures for the first week of March:
int array[7] = {12, 7, 8, 10, 11, 14, 12};
array 12 7 8 10 11 14 12
array indices 0 1 2 3 4 5 6
• Accessing elements: Elements of an array are accessed using zero-based indexing. You specify the index of the element
inside square brackets [ ].
For example: int x = array[2];
x 8
44
1-dimensional arrays
• Iterating over arrays: Loops can be used to iterate over the elements of an array and perform operations on them.
int array[7] = {12, 7, 8, 10, 11, 14, 12}; array
int sum=0; float average; 12 7 8 10 11 14 12
for (int i = 0; i < 7; i++) { array indices 0 1 2 3 4 5 6
printf("%d ", array[i]);
sum += array[i];
}
average = (float)sum/7;
printf("\nAverage is %.2f.",average);
• Notes:
• If the size of an array with an initializer list is not specified, the array will automatically be allocated memory to
match the number of elements in the list. Example: int array[] = {12, 7, 8, 10, 11, 14, 12}; the size of
the array will be 7.
• If the number of values in the initializer list is less than the size of the array, the remaining elements of the array
are initialized to zero.
• Examples:
int n[10] = {0}; // initializes entire array to zeros
int n[10] = {2,3}; // int n[10]={2,3,0,0,0,0,0,0,0,0};
45
1-dimensional arrays
type arrayName[size1][size2]...[sizeN];
• If there are not enough initializers for a given row, the remaining elements of that row are initialized to 0.
int b[2][2] = {{1}, {3, 4}}; // would initialize b[0][0] to 1, b[0][1] to 0, b[1][0] to 3 and b[1][1] to 4
48
Arrays of characters and strings
• A string is a sequence of characters terminated by a null character ('\0’).
• Strings are typically represented using arrays of characters. The null character at the end indicates the end of the string.
• Example of a string declaration and initialization:
char arr[] = {'H', 'e', 'l', 'l', 'o', '\0'};
char str[] = "Hello";
• Not all arrays of characters are necessarily strings. Strings must follow the specific convention of being null-terminated.
• Example of an array of characters that is not a string:
char arr[] = {'H', 'e', 'l', 'l', 'o'};
name J o h n \0
0 1 2 3 4 … 28 29
• Notice that & is not needed when scanning a string (see chapter on pointers).
• When scanf() is used with %s, it reads a sequence of characters until it encounters whitespace (space, tab, newline,
etc.).
• When "John Smith" is entered, scanf() stops reading input at the space between "John" and "Smith". As a result, only
"John" gets stored in the array name.
50
strings and gets
• For reading a string value with spaces, we can use either gets() or fgets(). gets() is used like scanf when the
input is taken from the keyboard, while fgets() works like fscanf when the input is taken from the keyboard or a file
(covered later in the course). These functions read a line of text until a newline character ('\n') is encountered.
• puts() can be used to display a string, but it does not support format specifiers or additional arguments. It simply
prints the string followed by a newline character.
51
gets and the buffer
• Consider the below code and output:
• After using scanf to read an integer, a newline character ('\n') is left in the input buffer.
• When gets is called to read the name, it encounters this newline character left in the buffer from the previous scanf
call. gets reads this newline character as an empty line, and it does not wait for the user input.
• To fix this issue, you can add a getchar() after scanf to consume the newline character left in the input buffer.
52
string.h
• string.h is a standard library of the C language that provides functions for manipulating strings of characters.
• strcpy(string1, string2) copies string2 into string1.
• strcat(string1, string2) concatenates (appends) the contents of string2 to the end of string1.
• Both strcpy and strcat do not alter string2; the changes occur only to string1.
• strcmp(string1, string2) compares string1 and string2 character by character. It returns:
• 0 if both strings are identical (equal)
• a negative integer if the ASCII value of the first unmatched character in string1 is less than the ASCII value of the corresponding
character in string2,
• a positive integer if the ASCII value of the first unmatched character in string1 is greater than the ASCII value of the
corresponding character in string2.
• strncmp(string1, string2, n) compares at most the first n bytes of string1 and string2.
n
53
string.h
• strlwr(string) converts all uppercase characters in a string to lowercase. It takes a string as input and modifies it in-
place.
• strupr(string) converts all lowercase characters in a string to uppercase. Like strlwr, it takes a string as input and
modifies it in-place.
• strlen(string) returns the length of string; i.e., it returns an integer representing the number of characters in the string
excluding the null character.
54
string.h
• Example using some functions from string.h:
str1 J o h n \0 str2 S m i t h \0
0 1 2 3 4 0 1 2 3 4 5
name1 J o h n S m i t h \0
0 1 2 3 4 5 6 7 8 9 … 19
name2 J o h n S m i t h \0
0 1 2 3 4 5 6 7 8 9 10
55