0% found this document useful (0 votes)
12 views7 pages

PIC Exp5

The document outlines an experiment for a C programming course focused on strings and string handling functions. It includes objectives, theory, problem statements, example code, and post-lab questions regarding string manipulation and memory management. The experiment aims to enhance understanding of string functions and their applications in programming.

Uploaded by

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

PIC Exp5

The document outlines an experiment for a C programming course focused on strings and string handling functions. It includes objectives, theory, problem statements, example code, and post-lab questions regarding string manipulation and memory management. The experiment aims to enhance understanding of string functions and their applications in programming.

Uploaded by

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

K. J.

Somaiya School of Engineering, Mumbai-77


(Somaiya Vidyavihar University)
Department of Science and Humanities

Course Name: Programming in C Semester: II


DIV/ Batch
Date of Performance: 12/ 03 /2025 P7-1
No:
Student Name: Rohaan Rajendra Bhagat Roll No: 16014024005

Experiment No: 5
Title: Strings and String Handling Functions

Aim and Objective of the Experiment:


Write a program in C to demonstrate use of strings and string handling functions.

COs to be achieved:
CO3: Apply the concepts of arrays and strings.

Theory:
In C programming, a string is an array of characters terminated by a null character ('\0'). Strings are
represented using character arrays. To handle strings effectively, C provides a set of built-in
functions in the <string.h> library.

Key functions for string:

● strlen(): Returns the length of a string (excluding the null-terminator).


● strcpy(): Copies a string from source to destination.
● strncpy(): Copies up to n characters from source to destination.
● strcat(): Appends one string to the end of another.
● strncat(): Appends up to n characters from source to destination.
● strcmp(): Compares two strings lexicographically.
● strncmp(): Compares the first n characters of two strings.
● strchr(): Searches for the first occurrence of a character in a string.
● strrchr(): Searches for the last occurrence of a character in a string.
● strstr(): Searches for the first occurrence of a substring in a string.
● strtok(): Tokenizes a string into substrings based on delimiters.
● sprintf(): Formats and stores a string into a character array.
● sscanf(): Reads formatted input from a string and stores it in variables.
● strdup(): Duplicates a string by allocating memory and copying it.
● strspn(): Returns the length of the initial segment of a string containing only characters from
a set.
● strcspn(): Returns the length of the initial segment of a string excluding characters from a set.
● strpbrk(): Searches for the first occurrence of any character from a set in a string.
● strtok_r(): A reentrant version of strtok() for thread-safe tokenization.
● memcpy(): Copies memory from source to destination.

Programming in C Semester: II Academic Year: 2024-25


K. J. Somaiya School of Engineering, Mumbai-77
(Somaiya Vidyavihar University)
Department of Science and Humanities

● memset(): Sets a block of memory to a specified value.

Problem Statements:

1. Write a program that takes a string as input and counts the number of vowels and consonants
in the string without using the built-in library function. Ignore spaces and punctuation.
2. Write a program to manage student records. The program will handle the following
operations using the string functions provided:
● Input the student's name and grade (two strings).
● Display the length of both the student's name and grade.
● Copy the student's name into a new string and display it.
● Concatenate a fixed string (e.g., " - Excellent Student") to the student's name and
display the result.
● Compare two students' names lexicographically and display which student has the
lexicographically greater name.
● Search for a substring in the student's name (e.g., "John" in "Johnny") and display the
position of the first occurrence.
● Search for a character in the grade string (e.g., 'A') and display the position of the first
occurrence.
● Tokenize the student's grade if it contains multiple components (e.g., "A B C") and
display each component.

Code :
1.

#include <stdio.h>

int main() {
char str[100];
int vowels = 0, consonants = 0;

printf("Enter a String : ");


gets(str);

for(int i = 0; str[i] != '\0'; i++)


{
char ch = str[i];

Programming in C Semester: II Academic Year: 2024-25


K. J. Somaiya School of Engineering, Mumbai-77
(Somaiya Vidyavihar University)
Department of Science and Humanities

if (ch >= 'A' && ch <= 'Z')


{
ch = ch + ('a' - 'A');
}

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')


{
vowels++;
}

else if (ch >= 'a' && ch <= 'z')


{
consonants++;
}
}

printf("The Vowels are : %d\n", vowels);


printf("The Consonants are : %d\n", consonants);

return 0;
}

2.

#include <stdio.h>
#include <string.h>

int main()
{
char name[100], grade[100], copiedName[100], concatName[100], anotherName[100];
char searchName[100], searchChar;

printf("Enter the student's name : ");


gets(name);

printf("Enter the student's grade : ");


gets(grade);

printf("\nLength of the Name : %d", (int)strlen(name));


printf("\nLength of the Grade : %d", (int)strlen(grade));

Programming in C Semester: II Academic Year: 2024-25


K. J. Somaiya School of Engineering, Mumbai-77
(Somaiya Vidyavihar University)
Department of Science and Humanities

strcpy(copiedName, name);
printf("\nCopied Name : %s", copiedName);

strcpy(concatName, name);
strcat(concatName, " - Brilliant Student");
printf("\nConcatenated Name : %s", concatName);

printf("\n\nEnter another student's name : ");


gets(anotherName);

if (strcmp(name, anotherName) > 0)


{
printf("'%s' is greater than '%s'", name, anotherName);
}

else if (strcmp(name, anotherName) < 0)


{
printf("'%s' is smaller than '%s'", name, anotherName);
}
else
printf("Both names are equal");

printf("\n\nEnter a substring to search in name: ");


gets(searchName);

char *pos = strstr(name, searchName);


if (pos)
{
printf("Substring '%s' found at position: %ld", searchName, pos - name + 1);
}
else
printf("Substring '%s' not found in name", searchName);

printf("\n\nEnter a character to search in the grade : ");


scanf(" %c", &searchChar);

char *charPos = strchr(grade, searchChar);


if (charPos)
{
printf("Character '%c' found at position: %ld", searchChar, charPos - grade + 1);
}

Programming in C Semester: II Academic Year: 2024-25


K. J. Somaiya School of Engineering, Mumbai-77
(Somaiya Vidyavihar University)
Department of Science and Humanities

else
printf("Character '%c' not found in grade", searchChar);

printf("\n\nTokenized Grade Components is :\n");


char tempGrade[100];
strcpy(tempGrade, grade);
char *token = strtok(tempGrade, " ");
while (token) {
printf("%s\n", token);
token = strtok(NULL, " ");
}

return 0;
}

Programming in C Semester: II Academic Year: 2024-25


K. J. Somaiya School of Engineering, Mumbai-77
(Somaiya Vidyavihar University)
Department of Science and Humanities

Output:
1.

2.

Programming in C Semester: II Academic Year: 2024-25


K. J. Somaiya School of Engineering, Mumbai-77
(Somaiya Vidyavihar University)
Department of Science and Humanities

Post Lab Subjective/Objective type Questions:


1. In C, what will happen if you pass an uninitialized string or a string without a null
terminator to any of the string handling functions (e.g., strcpy(), strlen(), strcmp())?

If you use an uninitialized string or a string without a null terminator in C, things can go
wrong. The program may keep reading memory it shouldn’t, which can cause errors or even
make it crash. For example, strlen() might not know where to stop and give a wrong length.
strcpy() might copy extra, unwanted data, which can mess up your program. strcmp() might
compare more than it should, giving wrong results or crashing. To avoid these problems,
always make sure your strings end with a null character (\0).

2. In C, how does memory allocation for strings work? What are the potential risks
associated with string manipulation in C, and how can buffer overflow issues be
prevented?

In C, strings are stored in memory using character arrays or dynamic memory (malloc()). A
big risk is going past the memory limit, which can cause errors or crashes. Buffer overflow
happens when a string is too big and overwrites other data, which can break the program or
cause security issues. To prevent this, always make sure there is enough space, use safer
functions like strncpy() instead of strcpy(), and check string sizes before copying. Being
careful with memory helps keep programs working properly and safely.

Conclusion:
This experiment demonstrated the use of strings and string handling functions in C. By implementing
programs that manipulate strings, we understood how functions like strlen(), strcpy(), and strcmp()
work. Proper memory management and careful handling of string operations help prevent errors like
buffer overflow and ensure program stability.

Signature of faculty in-charge with Date:

Programming in C Semester: II Academic Year: 2024-25

You might also like