Module 4
Strings
1. With suitable examples describe the different functions used to Read
and Write Strings
Reading Strings
using scanf() function
scanf(“%s”, str);
using gets() function
gets(str);
using getchar(), getch() or getche() function repeatedly
i=0;
ch = getchar(); //Get a character
while(ch!=’\n’)
{
str[i] = ch; //Store the read character in str
i++;
ch = getchar(); //Get another character
}
str[i] = ‘\0’; //terminate str with null character
Writing Strings
using printf() function
printf(“%s”, str);
using puts() function
puts(str);
using putchar()function repeatedly
i=0;
while(str[i] != '\0’)
{
putchar(str[i]); //print the character on the screen
i++;
}
2. What is a String? Explain different ways of initializing the strings.
Ans: A string in C is a sequence of characters terminated by a null character
('\0'). It is typically represented as an array of characters.
i. char str[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; // Character Array with Individual
Characters
ii. char str[6] = "Hello"; //With Exact Size (including '\0'):
iii. char str[20] = "Hello"; // Extra space for more characters if needed
iv. char str[] = "Hello"; // Compiler sets size = 6
3. Write a program to check if two strings are equal or not without
using string built-in functions.
#include <stdio.h>
#include <string.h>
main()
{
char str1[50], str2[50];
int i=0, len1 = 0, len2 = 0;
printf("\n Enter the first string : ");
gets(str1);
printf("\n Enter the second string : ");
gets(str2);
len1 = strlen(str1);
len2 = strlen(str2);
if(len1 == len2)
{
while(i<len1)
{
if(str1[i] == str2[i])
i++;
else break;
}
if(i==len1)
{
printf("\n The two strings are equal");
}
}
if(len1!=len2)
printf("\n The two strings are not equal");
return 0;
}
4. Write a program to convert characters of a string to uppercase.
#include <stdio.h>
int main()
{
char str[100], upper_str [100];
int i=0, j=0;
printf("\n Enter the string:");
gets(str);
while(str[i] != '\0')
{
if(str[i]>='a' && str[i]<='z')
upper_str[j] = str[i] -32;
else
upper_str[j] = str[i];
i++;
j++;
}
upper_str[j] = '\0';
printf("\n The string converted into upper case is : ");
puts(upper_str);
return 0;
}
5. Write a program to convert characters of a string to lowercase
#include <stdio.h>
int main()
{
char str[100], lower_str [100];
int i = 0, j=0;
printf("\n Enter the string :");
gets(str);
while(str[i] != '\0')
{
if(str[i]>='A'&& str[i]<='Z')
lower_str[j]= str[i] + 32;
else
lower_str[j] = str[i];
i++;
j++;
}
lower_str[j] = '\0’;
printf("\n The string converted into lower case is : ");
puts(lower_str);
return 0;
}
6. Write a C Program to compare two strings using strcmp().
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100];
int result;
printf("Enter first string: ");
gets(str1); // Unsafe, but used here as requested
printf("Enter second string: ");
gets(str2); // Unsafe
result = strcmp(str1, str2);
if (result == 0)
printf("Strings are equal.\n");
else if (result > 0)
printf("First string is greater than second.\n");
else
printf("First string is less than second.\n");
return 0;
}
Output:
Enter first string: Mango
Enter second string: Mango
Strings are equal.
7. Write a program to convert an upper-case letter into lower-case and
vice-versa in a given string without using built-in function.
#include <stdio.h>
int main() {
char str[100], swapped[100];
int i = 0;
printf("Enter a string: ");
gets(str);
while (str[i] != '\0') {
if (str[i] >= 'a' && str[i] <= 'z') {
swapped[i] = str[i] - 32; // to uppercase
} else if (str[i] >= 'A' && str[i] <= 'Z') {
swapped[i] = str[i] + 32; // to lowercase
} else {
swapped[i] = str[i]; // other characters unchanged
}
i++;
}
swapped[i] = '\0';
printf("Case-swapped string: %s", swapped);
return 0;
}
Output:
Enter a string: Hello World 2025!
Case-swapped string: hELLO wORLD 2025!
Structures
1.Describe structures in C with examples.
• Structure is a user-defined data type that can store related information.
• Major difference between a structure and an array is that an array stores
the information of the same data type whereas, a structure stores
information of different data types.
• A structure is, therefore, a collection of variables under a single name.
Syntax of structure declaration:
struct struct_name
{
datatype var_name;
datatype var_name;
................
};
Example of a structure declaration:
struct student
{
int r_no;
char name[20];
char course[20];
float fees;
};
2. Write a C program to define Employee as a structure with members as
name, emp_id, salary. Read and display the the same.
#include <stdio.h>
// Define the structure
struct Employee {
char name[50];
int emp_id;
float salary;
};
int main() {
struct Employee emp;
// Read employee details
printf("Enter employee name: ");
scanf("%s", emp.name); // Assumes name without spaces
printf("Enter employee ID: ");
scanf("%d", &emp.emp_id);
printf("Enter employee salary: ");
scanf("%f", &emp.salary);
// Display employee details
printf("\n--- Employee Details ---\n");
printf("Name: %s\n", emp.name);
printf("Employee ID: %d\n", emp.emp_id);
printf("Salary: %.2f\n", emp.salary);
return 0;
}
3. Illustrate the use of nested structures with examples.
#include <stdio.h>
// Nested structure
struct Address {
char city[20];
int pincode;
};
struct Employee {
char name[20];
struct Address addr; // Nested structure
};
int main() {
struct Employee emp;
// Input
printf("Enter name: ");
scanf("%s", emp.name);
printf("Enter city: ");
scanf("%s", emp.addr.city);
printf("Enter pincode: ");
scanf("%d", &emp.addr.pincode);
// Output
printf("\nName: %s\nCity: %s\nPincode: %d\n",
emp.name, emp.addr.city, emp.addr.pincode);
return 0;
}
4. Define structure and different ways of initializing structure.
• Structure is a user-defined data type that can store related information.
Syntax of initializing a structure:
Struct struct_name
{
datatype member_name1;
datatype member_name2;
}struct_var={constant1,constant2,....};
OR
Struct struct_name
{
datatype member_name1;
datatype member_name2;
};
Struct struct_name struct_var = { constant1 ,Constant2,....};
5. Design a C program to read and display 10 students’ information
(Student ID, Name, and Date of birth) using nested structures.
#include <stdio.h>
// Nested structure for Date of Birth
struct DOB {
int day;
int month;
int year;
};
// Structure for Student
struct Student {
int id;
char name[50];
struct DOB dob; // Nested structure
};
int main() {
struct Student s[10];
int i;
// Input
printf("Enter details for 10 students:\n");
for(i = 0; i < 10; i++) {
printf("\nStudent %d:\n", i+1);
printf("Enter ID: ");
scanf("%d", &s[i].id);
printf("Enter Name: ");
scanf("%s", s[i].name); // Assumes name without spaces
printf("Enter DOB (dd mm yyyy): ");
scanf("%d %d %d", &s[i].dob.day, &s[i].dob.month, &s[i].dob.year);
}
// Output
printf("\n--- Student Information ---\n");
for(i = 0; i < 10; i++) {
printf("\nStudent %d:\n", i+1);
printf("ID: %d\n", s[i].id);
printf("Name: %s\n", s[i].name);
printf("DOB: %02d-%02d-%04d\n", s[i].dob.day, s[i].dob.month,
s[i].dob.year);
}
return 0;
}
6. Write the output of the following code
#include <stdio.h>
struct Point {
int x, y;
};
int main() {
struct Point p1 = {0, 1};
p1.x = 20;
printf("x = %d, y = %d", p1.x, p1.y);
return 0;
}
Output
x = 20, y = 1