Programming For Problem Solving Using C and C RBU
Programming For Problem Solving Using C and C RBU
3.5 Unions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
5 Strings 51
• Exercises on Strings
1 C Basics, Operators, Loops
C is a general-purpose, procedural programming language widely used for system and application software. It provides a
strong foundation for understanding more complex programming concepts and languages.
3 int main () {
4 // code
5 return 0;
6 }
• #include <stdio.h>: Preprocessor directive to include the Standard Input Output library.
• return 0;: Ends the program and returns control to the operating system.
Operators
Operators are symbols used to perform operations on variables and values. C supports several types of operators:
Arithmetic Operators
• + (Addition)
• - (Subtraction)
• * (Multiplication)
• / (Division)
• % (Modulus)
Syntax:
1 int a = 10;
2 int b = 5;
3 int sum = a + b ; // sum is 15
Relational Operators
• == (Equal to)
Syntax:
1 int a = 10;
2 int b = 5;
3 int result = ( a > b ) ; // result is 1 ( true )
Logical Operators
• (Logical AND)
• || (Logical OR)
• ! (Logical NOT)
Syntax:
1 int a = 10;
2 int b = 5;
3 int result = ( a > b ) && ( b > 0) ; // result is 1 ( true )
Assignment Operators
• = (Simple assignment)
Syntax:
1 int a = 10;
2 a += 5; // a is now 15
• ++ (Increment)
• – (Decrement)
Syntax:
1 int a = 10;
2 a ++; // a is now 11
Control statements
Control statements in C allow you to alter the flow of execution in your program. They enable conditional execution
Syntax:
1 if ( condition ) {
2 // code to execute if condition is true
3 }
Example:
1 int a = 10;
2 if ( a > 5) {
3 printf ( " a is greater than 5\ n " ) ;
4 }
If-Else Statement
Executes one block of code if the condition is true and another block if the condition is false.
Syntax:
1 if ( condition ) {
2 // code to execute if condition is true
3 } else {
4 // code to execute if condition is false
5 }
Example:
1 int a = 10;
2 if ( a > 5) {
3 printf ( " a is greater than 5\ n " ) ;
4 } else {
5 printf ( " a is not greater than 5\ n " ) ;
6 }
Syntax:
1 if ( condition1 ) {
2 // code to execute if condition1 is true
3 } else if ( condition2 ) {
4 // code to execute if condition2 is true
5 } else {
6 // code to execute if none of the conditions are true
7 }
Example:
1 int a = 10;
2 if ( a > 15) {
3 printf ( " a is greater than 15\ n " ) ;
4 } else if ( a > 5) {
5 printf ( " a is greater than 5 but less than or equal to 15\ n " ) ;
6 } else {
7 printf ( " a is 5 or less \ n " ) ;
8 }
Switch Statement
Allows multi-way branching based on the value of an expression. Each case represents a branch.
Syntax:
1 switch ( expression ) {
2 case value1 :
3 // code to execute if expression equals value1
4 break ;
5 case value2 :
6 // code to execute if expression equals value2
7 break ;
8 // more cases
9 default :
10 // code to execute if no case matches
11 }
Example:
1 int day = 3;
2 switch ( day ) {
3 case 1:
4 printf ( " Monday \ n " ) ;
5 break ;
6 case 2:
7 printf ( " Tuesday \ n " ) ;
8 break ;
9 case 3:
10 printf ( " Wednesday \ n " ) ;
11 break ;
12 default :
13 printf ( " Invalid day \ n " ) ;
14 }
Break Statement
Exits from the innermost loop or switch statement.
Syntax:
1 break ;
Example:
Continue Statement
Skips the rest of the code inside the loop for the current iteration and proceeds with the next iteration.
Syntax:
1 continue ;
Example:
Goto Statement
Transfers control to a labeled statement. Its use is generally discouraged because it can lead to code that is difficult to
Syntax:
1 goto label ;
2
3 label :
4 // code to execute
Example:
1 int i = 0;
2 start :
3 if ( i < 5) {
4 printf ( " % d \ n " , i ) ;
5 i ++;
6 goto start ; // Jump back to the label
7 }
Loops
Loops are used to execute a block of code multiple times.
For Loop
Syntax:
1 for ( initia lization ; condition ; increment / decrement ) {
2 // code
3 }
Example:
While Loop
Used when the number of iterations is not known, and execution continues as long as the condition is true.
Syntax:
1 while ( condition ) {
2 // code
3 }
Example:
1 int i = 0;
2 while ( i < 5) {
3 printf ("% d \ n " , i ) ;
4 i ++;
5 }
Do-While Loop
Similar to the while loop, but it guarantees at least one execution of the loop body.
Syntax:
1 do {
2 // code
3 } while ( condition ) ;
Example:
1 int i = 0;
2 do {
3 printf ("% d \ n " , i ) ;
4 i ++;
5 } while ( i < 5) ;
Instructions:
1 Enter an integer : 7
Sample Output:
Program:
3 int main () {
4 int num ;
5
17 return 0;
18 }
division based on user choice. Use a switch-case statement to handle the operations.
Instructions:
Sample Input:
Sample Output:
1 10 * 5 = 50
Program:
3 int main () {
4 float num1 , num2 ;
5 char operator ;
6
38 return 0;
39 }
ax 2 + bx + c = 0
using the quadratic formula. Handle cases for real and complex roots.
Instructions:
1 Enter coefficient a : 1
2 Enter coefficient b : -3
3 Enter coefficient c : 2
Sample Output:
Program:
4 int main () {
5 float a , b , c , discriminant , root1 , root2 , realPart , imaginaryPart ;
6
32 return 0;
33 }
Instructions:
1. Prompt the user to enter a number.
Sample Input:
1 Enter a number : 5
Sample Output:
Program:
21 int main () {
22 int num ;
23
34 return 0;
35 }
1.5 Fibonacci Series
Objective: Write a C program to generate the Fibonacci series up to n terms using a for loop.
Instructions:
Sample Input:
Sample Output:
1 Fibonacci Series : 0 1 1 2 3 5 8
Program:
3 int main () {
4 int terms , first = 0 , second = 1 , next ;
5
29 return 0;
30 }
2. Use a for loop to check if the number is divisible by any number other than 1 and itself.
Sample Input:
1 Enter a number : 29
Sample Output:
Program:
3 int main () {
4 int num , isPrime = 1;
5
29 return 0;
30 }
Instructions:
Sample Input:
Sample Output:
Program:
3 int main () {
4 int num , sum = 0;
5
19 return 0;
20 }
1 *
2 **
3 ***
4 ****
5 *****
Instructions:
Sample Output:
1 *
2 **
3 ***
4 ****
5 *****
Program:
3 int main () {
4 int i , j , rows = 5;
5
14 return 0;
15 }
Instructions:
2. Use a for loop to iterate through the string and count vowels and consonants.
Sample Input:
Sample Output:
1 Vowels : 3
2 Consonants : 7
Program:
4 int main () {
5 char str [100];
6 int i , vowels = 0 , consonants = 0;
7
28 return 0;
29 }
Instructions:
Sample Input:
Sample Output:
Program:
9 int main () {
10 int num1 , num2 ;
11
19 return 0;
20 }
Instructions:
2. Use a for loop to generate and display the multiplication table up to 10.
Sample Input:
1 Enter a number : 7
Sample Output:
Program:
3 int main () {
4 int num ;
5
16 return 0;
17 }
Instructions:
Sample Input:
Sample Output:
Program:
3 int main () {
4 int num , count = 0;
5
19 return 0;
20 }
Instructions:
2. Use a for loop to calculate the sum of the first N natural numbers.
Sample Input:
1 Enter the value of N : 5
Sample Output:
Program:
3 int main () {
4 int n , sum = 0;
5
18 return 0;
19 }
Syntax.
Example
Initializing Arrays
1 int value = numbers [2]; // Accesses the third element of the array ( index starts at 0)
Multidimensional Arrays
Arrays with more than one dimension, such as
Syntax.
Example
C Functions
Functions in C are blocks of code that perform a specific task and can be reused throughout the program. They help in
Syntax.
Example
3 // Function declaration
4 int add ( int a , int b ) ;
5
6 int main () {
7 int result = add (10 , 5) ; // Function call
8 printf ( " Result : % d \ n " , result ) ;
9 return 0;
10 }
11
12 // Function definition
13 int add ( int a , int b ) {
14 return a + b ;
15 }
Function Declaration
Declares the function before its use in main() or other functions.
Syntax.
Syntax.
Function Call
Invokes the function and optionally passes arguments.
Syntax.
1 functionName ( arguments ) ;
Example
1 int result = add (10 , 5) ; // Calls the function and stores the result
Default Arguments
C does not support default arguments. All arguments must be explicitly provided in function calls.
Instructions:
1. Prompt the user to enter the size of the array and the elements.
Sample Input:
Sample Output:
1 Reversed array : 5 4 3 2 1
Program:
3 int main () {
4 int arr [100] , size ;
5
30 return 0;
31 }
Instructions:
1. Prompt the user to enter the size of the array and the elements.
Sample Input:
Sample Output:
1 Largest element : 9
2 Smallest element : 1
Program:
3 int main () {
4 int arr [100] , size ;
5 int largest , smallest ;
6
34 return 0;
35 }
Instructions:
Sample Input:
Sample Output:
Program:
3 int main () {
4 int A [2][2] , B [2][2] , Sum [2][2];
5
38 return 0;
39 }
Instructions:
Sample Input:
Sample Output:
Program:
3 int main () {
4 int matrix [3][3] , transpose [3][3];
5
30 return 0;
31 }
2.5 Calculate the Length of a String
Objective: Write a C program to calculate the length of a string without using the strlen() function.
Instructions:
2. Calculate the length of the string by iterating through it until the null terminator is reached.
Sample Input:
Sample Output:
Program:
3 int main () {
4 char str [100];
5 int length = 0;
6
24 return 0;
25 }
Instructions:
2. Concatenate the second string to the end of the first string manually.
3. Display the concatenated string.
Sample Input:
Sample Output:
Program:
3 int main () {
4 char str1 [100] , str2 [100];
5 int i , j ;
6
37 return 0;
38 }
3 Structures and Unions
In C, structures are user-defined data types that group related variables of different types under a single name. Each
Syntax
1 struct StructureName {
2 type member1 ;
3 type member2 ;
4 // more members
5 };
Example
3 // Structure definition
4 struct Person {
5 char name [50];
6 int age ;
7 float height ;
8 };
9
10 int main () {
11 // Creating and initializing a structure variable
12 struct Person person1 = { " Alice " , 30 , 5.5};
13
19 return 0;
20 }
Accessing Structure Members Use the dot operator (.) to access members of a structure variable.
Example
Unions
Unions in C are similar to structures but allow only one member to be accessed at a time. They are useful when you
need to store different data types in the same memory location but not simultaneously.
Syntax
1 union UnionName {
2 type member1 ;
3 type member2 ;
4 // more members
5 };
Example
3 // Union definition
4 union Data {
5 int integer ;
6 float floating ;
7 char character ;
8 };
9
10 int main () {
11 // Creating and initializing a union variable
12 union Data data ;
13
24 return 0;
25 }
Accessing Union Members Use the dot operator (.) to access members of a union variable.
Example
Key Differences
• Structures: All members occupy different parts of memory. The size of a structure is the sum of the sizes of its
• Unions: All members share the same memory location. The size of a union is the size of its largest member.
of this structure, assign values to its members, and display these values.
Instructions:
Sample Input:
Sample Output:
1 Student Details :
2 Name : John Doe
3 Age : 20
4 Marks : 85.5
Program:
10 int main () {
11 // Create an instance of Student
12 struct Student student ;
13
28 return 0;
29 }
that includes street, city, and zip. Create an instance of Employee, assign values to all members, and display the
complete information.
Instructions:
Sample Input:
Sample Output:
1 Employee Details :
2 Name : Alice Smith
3 Age : 30
4 Address :
5 Street : 123 Elm Street
6 City : Springfield
7 Zip : 12345
Program:
17 int main () {
18 // Create an instance of Employee
19 struct Employee employee ;
20
44 return 0;
45 }
Book structures, initialize it with data, and display the details of all books.
Instructions:
Sample Output:
1 Book 1:
2 Title : The Great Gatsby
3 Author : F . Scott Fitzgerald
4 Price : 10.99
5
6 Book 2:
7 Title : 1984
8 Author : George Orwell
9 Price : 8.99
10
11 Book 3:
12 Title : To Kill a Mockingbird
13 Author : Harper Lee
14 Price : 12.49
Program:
10 int main () {
11 // Create an array of Book structures
12 struct Book books [3] = {
13 { " The Great Gatsby " , " F . Scott Fitzgerald " , 10.99} ,
14 { " 1984 " , " George Orwell " , 8.99} ,
15 { " To Kill a Mockingbird " , " Harper Lee " , 12.49}
16 };
17
26 return 0;
27 }
takes a Rectangle structure as an argument and returns its area. Use this function to calculate and display the area of a
rectangle.
Instructions:
3. Call the function from the main function and display the result.
Sample Input:
1 Length : 5
2 Width : 4
Sample Output:
Program:
1 # include < stdio .h >
2
14 int main () {
15 struct Rectangle rect ;
16
27 return 0;
28 }
3.5 Unions
Objective: Write a C program to define a union Data with members integer, float, and char. Create an instance of
this union, assign values to each member, and display the values to show how the union’s memory is shared.
Instructions:
4. Observe how changing one member affects the others due to shared memory.
Sample Input:
1 Integer value : 10
2 Float value : 3.14
3 Character value : A
Sample Output:
1 Integer value : 10
2 Float value : 3.14
3 Character value : A
Program:
1 # include < stdio .h >
2
10 int main () {
11 union Data data ;
12
25 return 0;
26 }
occupying 1 bit. Define an enumerated type Day for days of the week. Create an instance of Flags, assign values to the
bit-fields, and display them. Use the Day enumeration to display the name of a specific day of the week.
Instructions:
3. Create an instance of Flags, assign values to the bit-fields, and display them.
4. Create a variable of type Day and display the name of a specific day.
Sample Input:
1 Bit1 : 1
2 Bit2 : 0
3 Bit3 : 1
4 Day : Wednesday
Sample Output:
1 Flags :
2 Bit1 : 1
3 Bit2 : 0
4 Bit3 : 1
5
6 Day : Wednesday
Program:
13 int main () {
14 struct Flags flags ;
15 enum Day today ;
16
Syntax
1 type * pointerName ;
Example
3 int main () {
4 int value = 10;
5 int * ptr ; // Pointer declaration
6
13 return 0;
14 }
Key Operations
Example
4 int main () {
5 int * ptr ;
6 ptr = ( int *) malloc ( sizeof ( int ) ) ; // Allocate memory for one integer
7
8 if ( ptr == NULL ) {
9 printf ( " Memory allocation failed \ n " ) ;
10 return 1;
11 }
12
18 return 0;
19 }
File Handling
C provides functions to read from and write to files. Files are managed using file pointers.
Syntax
1 FILE * filePointer ;
2 filePointer = fopen ( " filename " , " mode " ) ;
Modes
• "r": Read
• "w": Write
• "a": Append
Example
3 int main () {
4 FILE * file ;
5
31 return 0;
32 }
Key Functions
Pointers and file handling are fundamental for managing memory and data storage in C, offering flexibility and control
variable, and displaying the value of the variable using the pointer.
Instructions:
Sample Input:
Sample Output:
3 int main () {
4 int num = 25; // Define an integer variable
5 int * ptr = & num ; // Create a pointer to the integer variable
6
11 return 0;
12 }
traverse the array, and displaying the elements of the array using pointer arithmetic.
Instructions:
Sample Input:
1 Array elements : 10 , 20 , 30 , 40 , 50
Sample Output:
Program:
3 int main () {
4 int arr [] = {10 , 20 , 30 , 40 , 50}; // Define an array of integers
5 int * ptr = arr ; // Create a pointer to the array
6
array, and display its elements. Free the allocated memory using free().
Instructions:
Sample Input:
1 Number of elements : 5
2 Array elements : 1 , 2 , 3 , 4 , 5
Sample Output:
Program:
4 int main () {
5 int * arr ;
6 int size = 5;
7
29 return 0;
30 }
this function and use the pointer to call the function and display the result.
Instructions:
3. Use the function pointer to call the function and display the result.
Sample Input:
1 Integers : 5 , 10
Sample Output:
1 Sum of 5 and 10 is : 15
Program:
8 int main () {
9 int (* funcPtr ) ( int , int ) = sum ; // Define a pointer to the function
10 int result ;
11
18 return 0;
19 }
4.5 Pointer to Pointer
Objective: Write a C program to demonstrate pointer to pointer by creating an integer variable, defining a pointer to
that variable, and defining another pointer that points to the first pointer. Display the value of the integer using the pointer
to pointer.
Instructions:
Sample Input:
1 Integer value : 42
Sample Output:
Program:
3 int main () {
4 int num = 42; // Define an integer variable
5 int * ptr1 = & num ; // Define a pointer to the integer variable
6 int ** ptr2 = & ptr1 ; // Define a pointer to the pointer
7
12 return 0;
13 }
that the file has been created and contains the correct content.
Instructions:
Sample Input:
Program:
3 int main () {
4 FILE * file = fopen ( " output . txt " , " w " ) ; // Open the file for writing
5
6 if ( file == NULL ) {
7 printf ( " Error opening file !\ n " ) ;
8 return 1;
9 }
10
11 fprintf ( file , " Hello , World !\ n " ) ; // Write a string to the file
12 fclose ( file ) ; // Close the file
13
14 printf ( " File ’ output . txt ’ created and string written successfully .\ n " ) ;
15
16 return 0;
17 }
Instructions:
Sample Input:
Sample Output:
Program:
3 int main () {
4 FILE * file = fopen ( " input . txt " , " r " ) ; // Open the file for reading
5 char buffer [256]; // Buffer to hold file contents
6
7 if ( file == NULL ) {
8 printf ( " Error opening file !\ n " ) ;
9 return 1;
10 }
11
19 return 0;
20 }
Instructions:
Sample Input:
Sample Output:
Program:
3 int main () {
4 FILE * file = fopen ( " log . txt " , " a " ) ; // Open the file in append mode
5
6 if ( file == NULL ) {
7 printf ( " Error opening file !\ n " ) ;
8 return 1;
9 }
10
11 fprintf ( file , " New log entry .\ n " ) ; // Append a new line of text
12 fclose ( file ) ; // Close the file
13
16 return 0;
17 }
4.9 Copying File Contents
Objective: Write a C program to copy the contents of a file named source.txt to a new file named destination.txt.
Instructions:
3. Copy the contents from the source file to the destination file.
Sample Input:
Sample Output:
Program:
3 int main () {
4 FILE * source = fopen ( " source . txt " , " r " ) ; // Open the source file for reading
5 FILE * destination = fopen ( " destination . txt " , " w " ) ; // Open the destination file for writing
6 char buffer [256]; // Buffer to hold file contents
7
21 printf ( " Contents copied from ’ source . txt ’ to ’ destination . txt ’ successfully .\ n " ) ;
22
23 return 0;
24 }
4.10 File Operations with Error Handling
Objective: Write a C program to open a file named data.bin in binary mode for writing. Write an array of integers
to the file, and then open the file again in binary mode for reading, read the integers back, and display them. Implement
Instructions:
Sample Input:
Sample Output:
Program:
4 int main () {
5 FILE * file ;
6 int numbers [] = {10 , 20 , 30 , 40 , 50};
7 int readNumbers [5];
8 size_t size = sizeof ( numbers ) / sizeof ( numbers [0]) ;
9
34 return 0;
35 }
5 Strings
Strings
In C, strings are arrays of characters terminated by a null character (‘\0‘). The null character signifies the end of the
string. Strings are manipulated using a variety of functions provided in the C standard library.
Syntax
Example
Syntax
Example
4 int main () {
5 char str [] = " Hello " ;
6 printf ( " Length : % zu \ n " , strlen ( str ) ) ; // Output : Length : 5
7 return 0;
8 }
strcpy()
Syntax
Example
4 int main () {
5 char src [] = " Hello " ;
6 char dest [20];
7 strcpy ( dest , src ) ;
8 printf ( " Destination : % s \ n " , dest ) ; // Output : Destination : Hello
9 return 0;
10 }
strcat()
Syntax
Example
4 int main () {
5 char dest [20] = " Hello " ;
6 char src [] = " World " ;
7 strcat ( dest , src ) ;
8 printf ( " Concatenated : % s \ n " , dest ) ; // Output : Concatenated : Hello World
9 return 0;
10 }
strcmp()
Syntax
Example
4 int main () {
5 char str1 [] = " Hello " ;
6 char str2 [] = " Hello " ;
7 int result = strcmp ( str1 , str2 ) ;
8 if ( result == 0) {
9 printf ( " Strings are equal \ n " ) ; // Output : Strings are equal
10 } else if ( result < 0) {
11 printf ( " str1 is less than str2 \ n " ) ;
12 } else {
13 printf ( " str1 is greater than str2 \ n " ) ;
14 }
15 return 0;
16 }
strchr()
Syntax
Example
4 int main () {
5 char str [] = " Hello " ;
6 char * ptr = strchr ( str , ’l ’) ;
7 if ( ptr != NULL ) {
8 printf ( " Character found at position : % ld \ n " , ptr - str ) ; // Output : Character found at
position : 2
9 }
10 return 0;
11 }
strstr()
Syntax
Example
4 int main () {
5 char str [] = " Hello , World ! " ;
6 char * ptr = strstr ( str , " World " ) ;
7 if ( ptr != NULL ) {
8 printf ( " Substring found at position : % ld \ n " , ptr - str ) ; // Output : Substring found at
position : 7
9 }
10 return 0;
11 }
strncat()
Concatenates a specified number of characters from the source string to the destination string.
Syntax
Example
4 int main () {
5 char dest [20] = " Hello " ;
6 char src [] = " World ! " ;
7 strncat ( dest , src , 6) ; // Append only " World "
8 printf ( " Concatenated : % s \ n " , dest ) ; // Output : Concatenated : Hello World
9 return 0;
10 }
strncpy()
Copies a specified number of characters from the source string to the destination string.
Syntax
Example
4 int main () {
5 char src [] = " Hello , World ! " ;
6 char dest [20];
7 strncpy ( dest , src , 5) ; // Copy first 5 characters
8 dest [5] = ’ \0 ’; // Null - terminate the string
9 printf ( " Destination : % s \ n " , dest ) ; // Output : Destination : Hello
10 return 0;
11 }
These functions provide various capabilities for string manipulation and are essential for working with text data in C
programs.
Sample Input:
Sample Output:
Program:
3 int main () {
4 char str [] = " Hello , C Programming ! " ; // Declare and initialize the string
5
8 return 0;
9 }
Instructions:
Sample Input:
Sample Output:
Program:
3 int main () {
4 char str [] = " String Length Example " ; // Declare and initialize the string
5 int length = 0;
6
12 printf ( " The length of the string is : % d \ n " , length ) ; // Display the length
13
14 return 0;
15 }
Instructions:
Sample Input:
Sample Output:
Program:
3 int main () {
4 char str1 [] = " Array Initia lization " ; // Implicit null - termination
5 char str2 [25] = { ’A ’ , ’r ’ , ’r ’ , ’a ’ , ’y ’ , ’ ’ , ’I ’ , ’n ’ , ’i ’ , ’t ’ , ’i ’ , ’a ’ , ’l ’ , ’i ’ , ’z ’ , ’a ’ , ’
t ’ , ’i ’ , ’o ’ , ’n ’ , ’ \0 ’ }; // Explicit null - termination
6
10 return 0;
11 }
Instructions:
Sample Input:
Sample Output:
Program:
3 int main () {
4 char str1 [] = " Array Initia lization " ; // Array initiali zation
5 char * str2 = " Pointer Initi alizatio n " ; // Pointer ini tializat ion
6
10 return 0;
11 }
the string. Display both the copied string and its length.
Instructions:
2. Use strcpy() to copy a string from the source array to the destination array.
Sample Input:
Sample Output:
Program:
4 int main () {
5 char source [] = " String Copy Example " ;
6 char destination [50]; // Ensure sufficient size
7
8 // Copy source to destination
9 strcpy ( destination , source ) ;
10
14 // Display results
15 printf ( " Copied string : % s \ n " , destination ) ;
16 printf ( " Length of the copied string : % d \ n " , length ) ;
17
18 return 0;
19 }
Instructions:
Sample Input:
Sample Output:
Program:
4 int main () {
5 char str1 [50] = " Hello " ;
6 char str2 [] = " World " ;
7 char result [100]; // Ensure sufficient size
8
9 // Compare strings
10 int cmp_result = strcmp ( str1 , str2 ) ;
11 printf ( " Comparison result : % d \ n " , cmp_result ) ;
12
13 // Concatenate strings
14 strcpy ( result , str1 ) ;
15 strcat ( result , str2 ) ;
16 printf ( " Concatenated string : % s \ n " , result ) ;
17
18 return 0;
19 }
Instructions:
2. Iterate through the string and print each character along with its ASCII value.
Sample Input:
Sample Output:
1 Character : N , ASCII : 78
2 Character : u , ASCII : 117
3 Character : l , ASCII : 108
4 Character : l , ASCII : 108
5 Character : , ASCII : 32
6 Character : T , ASCII : 84
7 Character : e , ASCII : 101
8 Character : r , ASCII : 114
9 Character : m , ASCII : 109
10 Character : i , ASCII : 105
11 Character : n , ASCII : 110
12 Character : a , ASCII : 97
13 Character : t , ASCII : 116
14 Character : i , ASCII : 105
15 Character : o , ASCII : 111
16 Character : n , ASCII : 110
Program:
3 int main () {
4 char str [] = " Null Termination " ;
5
11 return 0;
12 }
5.8 Null-Terminated String Length
Objective: Write a C program to compute the length of a string manually by iterating through the characters until the
Instructions:
Sample Input:
Sample Output:
Program:
3 int main () {
4 char str [] = " Manual Length Calculation " ;
5 int length = 0;
6
14 return 0;
15 }
Instructions:
Sample Input:
Program:
3 int main () {
4 char arr [] = " Hello , World ! " ;
5 char * ptr = arr ; // Pointer to the character array
6
14 return 0;
15 }
Instructions:
Sample Input:
Sample Output:
Program:
3 int main () {
4 char arr [] = " Pointer Arithmetic " ;
5 char * ptr = arr ; // Pointer to the character array
6
13 return 0;
14 }
whether the strings are equal, or if one is lexicographically greater than the other.
Instructions:
Sample Input:
Sample Output:
Program:
11 int main () {
12 char str1 [] = " Apple " ;
13 char str2 [] = " Banana " ;
14
17 if ( result == 0) {
18 printf ( " The strings are equal \ n " ) ;
19 } else if ( result < 0) {
20 printf ( " String 1 is less than String 2\ n " ) ;
21 } else {
22 printf ( " String 1 is greater than String 2\ n " ) ;
23 }
24
25 return 0;
26 }
Case-Insensitive Comparison Objective: Write a C program to compare two strings in a case-insensitive manner by
Instructions:
Sample Input:
Sample Output:
Program:
20 int main () {
21 char str1 [] = " HeLLo " ;
22 char str2 [] = " hello " ;
23
31 if ( result == 0) {
32 printf ( " The strings are equal \ n " ) ;
33 } else if ( result < 0) {
34 printf ( " String 1 is less than String 2\ n " ) ;
35 } else {
36 printf ( " String 1 is greater than String 2\ n " ) ;
37 }
38
39 return 0;
40 }
Instructions:
Sample Input:
Sample Output:
Program:
18 int main () {
19 char str [] = " Hello , World ! " ;
20 char sub [] = " World " ;
21
24 if ( pos ) {
25 printf ( " Position of the first occurrence : % ld \ n " , pos - str ) ;
26 } else {
27 printf ( " Substring not found \ n " ) ;
28 }
29
30 return 0;
31 }
Instructions:
Sample Input:
Sample Output:
1 Number of occurrences : 4
Program:
28 int main () {
29 char str [] = " abababab " ;
30 char sub [] = " ab " ;
31
36 return 0;
37 }
Instructions:
Sample Input:
Sample Output:
1 Token : Tokenize
2 Token : this
3 Token : string
Program:
4 int main () {
5 char str [] = " Tokenize this string " ;
6 char * token ;
7
17 return 0;
18 }
Instructions:
Sample Input:
Sample Output:
1 Token : Apple
2 Token : Orange
3 Token : Banana
4 Token : Grape
Program:
4 int main () {
5 char str [] = " Apple , Orange Banana , Grape " ;
6 char * token ;
7
17 return 0;
18 }
Instructions:
Sample Input:
1 String : "12345"
Sample Output:
1 Integer : 12345
Program:
12 int main () {
13 char str [] = " 12345 " ;
14
Instructions:
Sample Input:
Sample Output:
Program:
17 int main () {
18 char str [] = " 123 abc " ;
19
22 if ( num != -1) {
23 printf ( " Integer : % d \ n " , num ) ;
24 }
25
26 return 0;
27 }