0% found this document useful (0 votes)
18 views6 pages

practical7_2024_PF

Uploaded by

Jahnavi Shah
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)
18 views6 pages

practical7_2024_PF

Uploaded by

Jahnavi Shah
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/ 6

FR.

CONCEICAO RODRIGUES COLLEGE OF ENGINEERING


Academic Term: September -December 2024
Subject Name: Programming Fundamentals

Course Code: ESC11CEL03


Class: F.E. (Sem –I)

Name of Student Tanmay Mahajan


Lab Experiment No. 7 Roll No. 10671
Date of Performance: Date of Submission:
Expt. Title a) To implement user defined string handling functions
b) Write a program to validate whether the accepted string is
palindrome or not

CO Mapping: CO4

Problem Definition: Implement string handling user defined functions - xstrlen(), xstrcpy() and
xstrcmp()

Objective of the Experiment: Understanding passing strings to functions and pointers.

Theory:
Strings in C are represented by arrays of characters. The end of the string is marked with a special
character, the null character. The null or string terminating character is represented by another
character sequence, \0 .

Declaring Strings To find length of string


char arrayname[size]; len=0;i=0;
Initializing Strings examples while(arrayname[i]!=’\0’)
char arrayname[size]=” I like C” ; len=len+1;
or printf(“length = %d”,len);
char arrayname[size]={'I',' ','l','i','k','e',' ','C'};
or
scanf(“%s”, arrayname);

Suggested Algorithm Suggested Algorithm Suggested Algorithm

Xstrlen() Xstrcpy() Xstrcmp()


1. Input a string from user. 1. Input two strings from
Store it in some variable say 1. Input string from user and store
user. Store it in some
it to some variable say text1.
text. variable say str1 and
2. Declare another variable to
store copy of first string in str2.
2. Initialize a counter variable to
text2.
zero, say count = 0. Count
variable is used to store total 3. Run a loop from 0 to end of 2. Compare two strings
number of characters in the string. The loop structure character by character till
string, which is our effective should be like for(i=0; text1[i] an unmatched character is
length of string. != '\0'; i++). found or end of any string
3. To iterate through input string, 4. Inside the loop for each is reached.
run a loop from 0 to the last character in text1 copy to text2. 3. If an unmatched character
character of string i.e. NULL Say text2[i] = text1[i]. is found then strings are
character. The loop structure 5. Finally after loop make sure the not equal.
should look like for(i=0; copied string ends with NULL 4. Else if both strings
text[i]!='\0'; i++). character i.e. text2[i] = '\0';. reached their end then
4. Inside the loop increment the
counter variable with 1 i.e. both strings are equal.
count++.

Source code for the implementation.

#include <stdio.h>

int xstrlen(const char *str) {


int length = 0;
while (*str++) {
length++;
}
return length;
}

void xstrcpy(char *dest, const char *src) {


while ((*dest++ = *src++));
}

int xstrcmp(const char *str1, const char *str2) {


while (*str1 && (*str1 == *str2)) {
str1++;
str2++;
}
return *(unsigned char *)str1 - *(unsigned char *)str2;
}

int main() {
char str1[100], str2[100];
printf("Enter first string: ");
scanf("%s", str1);
printf("Enter second string: ");
scanf("%s", str2);
printf("Length of first string: %d\n", xstrlen(str1));
printf("Length of second string: %d\n", xstrlen(str2));

xstrcpy(str2, str1);
printf("After copying, second string: %s\n", str2);

int cmp = xstrcmp(str1, str2);


if (cmp == 0) {
printf("Strings are equal\n");
} else if (cmp < 0) {
printf("First string is less than second string\n");
} else {
printf("First string is greater than second string\n");
}

return 0;
}
Sample input and output
Test case 1:
Enter the string: Programming
Length of string is 11

Test Case 2:
Enter the string1 : Monday
Enter the string2: Monday
Strings are equal.

b) Definition: Write a program to validate whether the accepted string is palindrome or


not.
Source code for the implementation.

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

int main() {
char str[100], rev[100];
int len, i, j;

printf("Enter a string: ");


gets(str);

len = strlen(str);
for (i = 0, j = len - 1; i < len; i++, j--) {
rev[i] = str[j];
}
rev[i] = '\0';

if (strcmp(str, rev) == 0) {
printf("The string is a palindrome.\n");
} else {
printf("The string is not a palindrome.\n");
}

return 0;
}
Sample input and output
Test case 1:
Enter String: MADAM
String is Palindrome

Test case 2:
Enter String: TEST
String is Not a Palindrome

The program was tested for different sets of inputs.


Program is working is SATISFACTORY NOT SATISFACTORY ( Tick appropriate outcome)

Evaluation:

On time Completion and Knowledge of the topic Implementation and Total (10)
Submission (2) (4) Output (4)

Date & Signature of teacher:

You might also like