You are on page 1of 6

Assignment 5

1) String operations without using library functions.

1)Concatenation Function:

#include<stdio.h>

#include<string.h>

void concat(char[], char[]);

int main() {

char s1[50], s2[30];

printf("\nEnter String 1 :");

gets(s1);

printf("\nEnter String 2 :");

gets(s2);

concat(s1, s2);

printf("\nConcated string is :%s", s1);

return (0);

void concat(char s1[], char s2[]) {

int i, j;

i = strlen(s1);
for (j = 0; s2[j] != '\0'; i++, j++)

s1[i] = s2[j];

s1[i] = '\0';

Output:

Enter String 1 :hello

Enter String 2 : everyone

Concated string is :hello everyone


2)String length Function:

#include<stdio.h>

void main()

char str[100];

int length=0;

printf("Enter string :");

scanf("%s",&str);

printf("\n The string is : %s",str);

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

length++;

printf("\n The length of the string is %d.",length);

Output:

Enter string :helloeveryone

The string is : helloeveryone

The length of the string is 13.


3)String copy function:

#include<stdio.h>

void main()

char s1[50];

char s2[50];

int i=0;

int length=0;

int c=0;

printf("Enter elements of the first string:");

scanf("%s",&s1);

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

length++;

printf("The length of string is : %d",length);

for(c=0 ; s1[c] != length ;c++)

s2[c]=s1[c];

s2[c]='\0';
printf("\nThe copied items in string s2 are: %s ",s2);

Output:

Enter elements of the first string:hello

The length of string is : 5

The copied items in string s2 are: hello

4)Reverse the string contents without strrev()

#include<stdio.h>

int main()

char s1[50];

int I;

int length=0 ;

printf("Enter the string:");

scanf("%s",&s1);

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

length++;

}
printf("\n The length of string is : %d",length);

printf("\n The reversed string is : ");

while(length>=0)

printf("%c",s1[length]);

length--;

return 0;

Output:

Enter the string:hello

The length of string is : 5

The reversed string is : olleh

You might also like