Cpro Assingment-String functions-20EUEC

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

PROBLEM SOLVING USING C

PROGRAMMING
ASSIGNMENT ON STRING FUNCTIONS
————————————————————————————————
NAME : RACHANA S. ROLL NO. : 20EUEC115

CLASS : ECE ‘B’ BATCH : 2

————————————————————————————————

1. Write a program in C to find the length of a given string without using


strlen function.

PROGRAM:

#include <stdio.h>
int main()
{
char str[100],i;
printf("Enter a string: \n");
scanf("%s",str);
for(i=0; str[i]!='\0'; ++i);
printf("\nLength of input string: %d",i);
return 0;
}

OUTPUT:

Enter a string:
Rachana
Length of input string: 7

————————————————————————————————


2. Write a program in C to perform concatenation of 2 strings without
using strcat function.

PROGRAM:

#include <stdio.h>
int main()
{
char str1[50], str2[50], i, j;
printf("\nEnter first string: ");
scanf("%s",str1);
printf("\nEnter second string: ");
scanf("%s",str2);
for(i=0; str1[i]!='\0'; ++i);
for(j=0; str2[j]!='\0'; ++j, ++i)
{
str1[i]=str2[j];
}
str1[i]='\0';
printf("\nOutput: %s",str1);
return 0;
}

OUTPUT:

Enter first string: Ariana


Enter second string: Grande

Output: ArianaGrande

————————————————————————————————


3. Write a program in C to compare two string without using strcmp
function.

PROGRAM:

#include <stdio.h>
#include <string.h>
int main()
{
char Str1[100], Str2[100];
int result, i;

printf("\n Enter the First String : ");


gets(Str1);
printf("\nEnter the Second String : ");
gets(Str2);
for(i = 0; Str1[i] == Str2[i] && Str1[i] == '\0'; i++);
if(Str1[i] < Str2[i])
{
printf("\nstring 1 is Less than string 2");
}
else if(Str1[i] > Str2[i])
{
printf("\nstring 2 is Less than string 1");
}
else
{
printf("\nstring 1 is Equal to string 2");
}
return 0;
}

OUTPUT:

Enter the First String : Hello


Enter the Second String : world
string 1 is Equal than string 2
————————————————————————————————






4. Write a program in C to copy a string without using strcpy function.

PROGRAM:

#include <stdio.h>
int main()
{
char s1[100], s2[100], i;
printf("Enter string s1: ");
fgets(s1, sizeof(s1), stdin);
for (i = 0; s1[i] != '\0'; ++i)
{
s2[i] = s1[i];
}
s2[i] = '\0';
printf("String s2: %s", s2);
return 0;
}

OUTPUT:

Enter string s1: Cprogramming


String s2: Cprogramming

————————————————————————————————


5. Write a program in C to reverse a given string without using strrev
function.

PROGRAM:

#include <stdio.h>
int main()
{
char s[1000], r[1000];
int begin, end, count = 0;
printf("Input a string\n");
gets(s);
while (s[count] != '\0')
count++;
end = count - 1;
for (begin = 0; begin < count; begin++)
{
r[begin] = s[end];
end--;
}
r[begin] = '\0';
printf("%s\n", r);
return 0;
}

OUTPUT:

Input a string:
Snow White
etihW wonS

————————————————————————————————

You might also like