Program 7:
Title: Design and Develop a c program using functions to implement string operations such
as compare, concatenate, string, length. Do not use string handling functions.
Problem Description: The program should prompt the user to input two strings and perform
the specified operations accordingly.
Method: Use parameter passing techniques, and strings.
Algorithm
Step 1: [Initialize]
Start
Step 2: [Input two source strings]
read source1,source2
Step 3: [Caculate length1 of source1 by calling the user defined
function, strlength(); Repeat the same for length2 of source2 ]
length1=strlength(source1)
length2=strlength(source2)
Step 4: [Ouput length1,length2]
Print length1,length2
Step 5: [Compare the two strings by calling the user defined function,
strcompare()]
k=strcompare(source1,source2)
Step 6: [check k, to find the whether the strings are same or not]
if(k = = 0)
print Both strings are same
else
print strings are different
end if
Step 7: [Concatenate two strings by calling the user defined function,
strconcat() and the concatenated string is stored in source1]
strconcat(source1,source2)
print source1
Step 8: [Finished]
Stop
User Defined Function - strlength()
Step 1: [Initialize]
Start
Step 2: [set i=0]
i=0
Step 3: [receive the source string as str, read character by character, count one
by one until we reach NULL character]
while(str[i]!=’\0')
i++
end while
Step 4: [return i to the calling function]
return i
User Defined Function - strcompare( )
Step 1: [Initialize]
Start
Step 2: [set i=0]
i=0
Step 3: [Receive both the source strings as str1 and str2, read character by
character until they match, if the matched character is a NULL then go
out of while loop, when any unmatched character then go out of loop]
while(str1[i] = = str2[i])
if(str1[i] = = ’\0')
break
end if
i++
end while
Step 4: [calculate k]
k=str1[i]-str2[j]
Step 5: [return i to the calling function]
return k
User Defined Function - strconcat( )
Step 1: [Initialize]
Start
Step 2: [set i=0]
i=0
Step 3: [Receive both the source strings as str1 and str2, calculate length of
str1 using strlength() as l]
l=strlength(str1)
Step 4: [read str2 character by character and store it from the end of str1]
while(str2[i]!=’\0')
str1[l+i]=str2[i]
i++
end while
Step 5: [return to the calling function]
return
Program:
#include<stdio.h>
int strlength(char s[50]);
void strconcat(char s1[50],char s2[50]);
void strcomp(char s1[10], char s2[10]);
int main()
{
char s1[50],s2[50];
int length1,length2,k;
printf("\n Enter the source string 1:");
gets(s1);
printf("\n Enter the source string 2:");
gets(s2);
length1=strlength(s1);
length2=strlength(s2);
printf("string length of string 1 is %d\n",length1);
printf(" string length of string 2 is %d\n",length2);
strconcat(s1,s2);
strcomp(s1,s2);
}
int strlength(char s1[50])
{
int i=0;
while(s1[i]!='\0')
i++;
return i;
}
void strconcat(char s1[50],char s2[50])
{
char s3[50];
int i=0,j=0;
while(s1[i]!='\0')
{
s3[i]=s1[i];
i++;
}
while(s2[j]!='\0')
{
s3[i+j]=s2[j];
j++;
}
s3[i+j]='\0';
printf("string concatenation : %s\n",s3);
}
void strcomp(char s1[10], char s2[10])
{
int i=0;
while(s1[i]==s2[i] && s1[i]!='\0' && s2[i]!='\0')
{
i++;
}
if(s1[i]=='\0'&& s2[i]=='\0')
printf("strings are same");
else
printf("strings are different");
}
Test Cases:
Test No Input Parameters Expected Output Obtained Output
String length of string1 is: 4 String length of string1 is: 4
Enter the source string1:
good String length of string2 is: 4 String length of string2 is: 4
1
Enter the source string2: Both strings are same
Both strings are same
good concatenated string is: concatenated string is:
goodgood good good
String length of string1is: 4 String length of string1 is: 4
Enter the source string1:
good String length of string2 is: 5 String length of string2 is: 5
2
Enter the source string2: strings are different strings are different
girls concatenated string is: concatenated string is:
goodgirls goodgirls
Program 8:
Title: Design and Develop a C program using pointers to compute the sum, mean and
standard deviation of all elements stored in an array of n real numbers.
Problem Description: The program should prompt the user to input the number of
elements n and the array elements. Using pointer arithmetic and mathematical
formulas, the program should calculate the sum, mean, and standard deviation of the
array elements.
Method: Exploration of pointers and pointer arithmetic in C.
Algorithm:
Step 1: [Initialize]
Start
Step 2:[Read the no of elements and array elements]
Read n
Read a[ ]
Step 3: [Set starting address of array to a pointer variable]
ptr=a
Step 4:[Iterate using a for loop to find sum using pointers]
for(i=0;i<n;i++)
sum=sum+*ptr
ptr++
end for
Step 5:[Calculate mean]
mean=sum/n
Step 6: [Set starting address of array to a pointer variable]
ptr=a
Step 7:[Iterate using a for loop to find sumstd using pointers]
for(i=0;i<n;i++)
sumstd=sumstd+pow((*ptr-mean),2)
ptr++
end for
Step 8:[Calculate standard deviation]
std=sqrt(sumstd/n)
Step 9:[Display the result]
Print sum,mean,std
Step 10:[Finished]
Stop
Program:
#include<stdio.h>
#include<math.h>
int main( )
{
float a[10],*ptr,mean,std,sum=0,sumstd=0;
intn,i;
printf(“\n Enter the number of elements”);
scanf(“%d”,&n);
printf(‘\n Enter the array elements”);
for(i=0;i<n;i++)
{
scanf(‘%f”,&a[i]);
}
ptr=a;
for(i=0;i<n;i++)
{
sum=sum+*ptr;
ptr++;
}
mean=sum/n;
ptr=a;
for(i=0;i<n;i++)
{
sumstd=sumstd+pow((*ptr-mean),2);
ptr++;
}
std=sqrt(sumstd/n);
printf(“Sum=%f\n”,sum);
printf(“Mean=%f\n”,mean);
printf(“Standard Deviation=%f\n”,std);
return 0;
}
Test Cases:
Test Input Parameters Expected Output Obtained Output
No
Enter the number of elements Sum = 28 Sum = 28
5
1 Enter the array elements Mean = 5.6 Mean = 5.6
15967 Standard Deviation = 2.09 Standard Deviation = 2.09
Enter the number of elements Sum = 10.68 Sum = 10.68
4
2 Enter the array elements Mean = 2.67 Mean = 2.67
2.3 1.1 4.5 2.78 Standard Deviation = 0.863 Standard Deviation = 0.863