WAP in C to compare two strings using strcmpi() function?

#include <stdio.h>
#include <string.h>
int main( )
{
char str1[ ] = “c language” ;
char str2[ ] = “C lang” ;
int i, j, k ;
clrscr();
i = strcmpi ( str1, “C LANGUAGE” ) ;
j = strcmpi ( str1, str2 ) ;
k = strcmpi ( str1, “l” ) ;
printf ( “\n%d %d %d”, i, j, k ) ;
getch();
return (0);
}

Output :-

strcmpi.png

Advertisements

WAP in C to compare two strings using strcmp() function?

#include <stdio.h>
#include <string.h>
int main( )
{
char str1[ ] = “C Language” ;
char str2[ ] = “C Lang.” ;
int i, j, k ;
clrscr();
i = strcmp ( str1, “C Language” ) ;
j = strcmp ( str1, str2 ) ;
k = strcmp ( str1, “L” ) ;
printf ( “\n%d %d %d”, i, j, k ) ;
getch();
return(0);
}

Output :-

strcmp.png

Note:-

Return Value Remarks
0 if both strings are identical (equal)
negative if the ASCII value of first unmatched character is less than second.
positive integer if the ASCII value of first unmatched character is greater than second

WAP in C to Copy String Using strncpy() function?

#include <stdio.h>
#include<conio.h>
#include <string.h>
int main( )
{
char source[ ] = “C Language For Beginners” ;
char target[20]= “” ;
clrscr();
printf ( “\nsource string = %s”, source ) ;
printf ( “\ntarget string = %s”, target ) ;
strncpy ( target, source, 5 ) ;
printf ( “\ntarget string after strcpy( ) = %s”, target ) ;
getch();
return(0);
}

Output:-

strncpy

WAP in C to Copy String Using strcpy() function?

#include <stdio.h>
#include<conio.h>
#include <string.h>
int main( )
{
char source[ ] = “C Language For Beginners” ;
char target[20]= “” ;
clrscr();
printf ( “\nsource string = %s”, source ) ;
printf ( “\ntarget string = %s”, target ) ;
strcpy ( target, source ) ;
printf ( “\ntarget string after strcpy( ) = %s”, target ) ;
getch();
return(0);
}

Output :-

strcpy.png

WAP in C Concatenate Two Strings Using strncat() function?

#include <stdio.h>
#include<conio.h>
#include <string.h>
int main( )
{
char source[ ] = ” For Beginners” ;
char target[ ]= “C Language” ;
clrscr();
printf ( “\nSource string = %s”, source ) ;
printf ( “\nTarget string = %s”, target ) ;
strncat ( target, source, 4 ) ;
printf ( “\nTarget string after strncat( ) = %s”, target ) ;
getch();
return(0);
}

Output :-

strncat.png

 

WAP in C Concatenate Two Strings Using strcat() function?

#include <stdio.h>
#include<conio.h>
#include <string.h>
int main( )
{
char source[ ] = ” For Beginnners” ;
char target[ ]= ” C Language” ;
clrscr();
printf ( “\nSource string = %s”, source ) ;
printf ( “\nTarget string = %s”, target ) ;
strcat ( target, source ) ;
printf ( “\nTarget string after strcat( ) = %s”, target ) ;
getch();
}

Output :-

Strcat.png

 

STRING FUNCTIONS

  • String.h header file supports all the string functions in C language. All the string functions are given below.
String functions Description
strcat ( ) Concatenates str2 at the end of str1
strncat ( ) Appends a portion of string to another
strcpy ( ) Copies str2 into str1
strncpy ( ) Copies given number of characters of one string to another
strlen ( ) Gives the length of str1
strcmp ( ) Returns 0 if str1 is same as str2. Returns <0 if strl < str2. Returns >0 if str1 > str2
strcmpi ( ) Same as strcmp( ) function. But, this function negotiates case.  “A” and “a” are treated as same.
strchr ( ) Returns pointer to first occurrence of char in str1
strrchr ( ) last occurrence of given character in a string is found
strstr ( ) Returns pointer to first occurrence of str2 in str1
strrstr ( ) Returns pointer to last occurrence of str2 in str1
strdup ( ) Duplicates the string
strlwr ( ) Converts string to lowercase
strupr ( ) Converts string to uppercase
strrev ( ) Reverses the given string
strset ( ) Sets all character in a string to given character
strnset ( ) It sets the portion of characters in a string to given character
strtok ( ) Tokenizing given string using delimiter

WAP in C to find the Largest Number in an Array of Numbers using Recursion?

#include<stdio.h>
#include<conio.h>
int large(int[], int, int);
int main()
{
int size, largest, list[20], i;
clrscr();
printf(“Enter size of the array:”);
scanf(“%d”, &size);
printf(“Enter the elements:\n”);
for (i = 0; i < size ; i++)
{
scanf(“%d”,&list[i]);
}
if (size == 0)
{
printf(“Empty list\n”);
}
else
{
largest = list[0];
largest = large(list, size – 1, largest);
printf(“\nThe largest number in the list is: %d\n”, largest);
}

getch();

return(0);
}
int large(int list[], int size, int largest)
{
if (size == 1)
return(largest);
if (size > -1)
{
if (list[size] > largest)
{
largest = list[size];
}
return(largest = large(list, size – 1, largest));
}
else
{
return(largest);
}
}

Output:-

larnumrec.png

WAP in C to find the nth number in Fibonacci series using recursion?

#include <stdio.h>
#include<conio.h>
int fibo(int);
int main()
{
int num, result;
printf(“Enter the nth number in fibonacci series: “);
scanf(“%d”, &num);
if (num < 0)
{
printf(“Fibonacci of negative number is not possible.\n”);
}
else
{
result = fibo(num);
printf(“The %d number in fibonacci series is %d\n”, num, result);
}
getch();
return(0);
}
int fibo(int num)
{
if (num == 0)
{
return(0);
}
else if (num == 1)
{
return(1);
}
else
{
return(fibo(num – 1) + fibo(num – 2));
}
}

Output:-

fabirecu.png