20_10

Write a C code to compare two strings.

 Source code:

/*Write a C code to compare two strings e.g. str1 and str2 and check whether strings are equal and str1 is greater than str2 or less, without using standard string library. */

#include <stdio.h>

#include <string.h>

int main(){

    char str1[20],str2[20];

    int c=0,d=0;

printf("Enter 1st string:");

    //Read String 1

    scanf("%s", &str1);

printf("Enter 2nd string:");

    //Read String 2

    scanf("%s", &str2);

int i=0;

while (str1[i] != '\0')

{

c++;

i++;

}

i=0;

while (str2[i] != '\0')

{

d++;

i++;

}

if (c==d)

{

        printf ("Strings are equal");

}

else if (c>d)

{

        printf ("String 1 is greater than String 2");

}

else

{

        printf ("String 1 is less than String 2");

}

return 0;

}

Output:
C code to compare two strings e.g. str1 and str2 and check whether strings are equal and str1 is greater than str2 or less, without using standard string library


Write a C code to concatenate two strings.

Source code:

 //Write a C code to concatenate two strings

#include <stdio.h>

#include <string.h>

int main(){

    char str1[20],str2[2];

    printf("Enter 1st string:");

    //Read String 1

    scanf("%s", str1);

printf("Enter 2nd string:");

    //Read String 2

    scanf("%s", str2);

int i,j,temp;

i=j=0;

while (str1[i] != '\0')

        i++;

    for (j = 0; str2[j] != '\0'; i++, j++)

      str1[i] = str2[j];

    str1[i] = '\0';

printf("The concatenated output:%s",str1);

return 0;

}

Output:

C code to concatenate two strings


Write a C code to reverse a string without using any string library functions.

 Source code:

/*Write a C code to reverse a string without using any string library functions , e.g. '12345' should be '54321'. */

#include <stdio.h>

#include <string.h>

int main(){

    char str[20];

printf("Enter a string:");

    //Read String

    scanf("%s", str);

int temp;

int i=0,j=0,length;


while (str[j] != '\0')

        j++;

        j--;

    while (i<j) {

        temp=str[i];

        str[i]=str[j];

        str[j]=temp;

        i++;

        j--;

}

printf("The reverse string:%s",str);

return 0;

}

Output:

C code to reverse a string without using any string library functions


Write a C code to check whether a given string is palindrome or not.

Source code:

 /*A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward, e.g. madam 123321 etc. Write a C code to check whether a given string is palindrome or not. */

#include <stdio.h>

#include <string.h>

int main(){

    char string[20];

    int flag = 0;

    printf("Enter a string:");

    //Read a String

    scanf("%s", string);

int i, length; 

length = strlen(string);

  for(i=0; i < length ; i++){

        if(string[i] != string[length-i-1]){

            flag = 1;

            break;

    }

}

  if (flag) {

        printf("The given string %s is not palindrome", string);

    }

    else {

        printf("The given string %s is palindrome", string);

    }

    return 0;

}

Output:

C code to check whether a given string is palindrome or not


Write a C code to find the difference between two matrices of any order and print the result in matrix format.

 Source code:

/*Write a C code to find the difference between two matrices of any order and  print the result in matrix format.*/

#include <stdio.h>

int main()

{

        int row, col, i, j, first[10][10], second[10][10], difference[10][10]; 

printf("Enter the number of row & column:");

        scanf("%d %d", &row, &col);  

printf("Enter the 1st matrix:\n");

    for (i = 0; i < row; i++)

    for (j = 0 ; j < col; j++)

scanf("%d", &first[i][j]);

  printf("Enter the 2nd matrix:\n");

for (i = 0; i < row; i++)

for (j = 0; j < col; j++)

        scanf("%d", &second[i][j]);

/* Find the difference of entered matrices and store it in difference[10][10] */

 

    for (i = 0; i < row; i++) {

    for (j = 0; j < col; j++) {

        difference[i][j] = first[i][j] - second[i][j];

     

    }

/*printing the difference array */

printf("Subtraction of two matrix is:\n");

    for (i = 0; i < row; i++)

{

    for (j = 0 ; j < col; j++){

    printf("%d\t",difference[i][j]);    

}

printf("\n");

    }

return 0;

}

Output:

C code to find the difference between two matrices of any order and  print the result in matrix format


Write a C program to count total number of vowel or consonant in a string.

Source code:

//Write a C program to count total number of vowel or consonant in a string 

#include <stdio.h>

#include <string.h>

int main()

{

    char s[1000],ch;

    int i=0, vowel, consonant;

    printf("Enter your sentence:");

    while(ch != '\n')    /* terminates if user hit enter */

    {

        ch = getchar();

        s[i] = ch;

        i++;

    }

    s[i-1] = '\0';       /* inserting null character at end */

int len;

vowel = 0;

    consonant = 0;

    len = strlen(s);


    for(i=0; i<len; i++)

    {

        if((s[i]>='a' && s[i]<='z') || (s[i]>='A' && s[i]<='Z'))

        {


            if(s[i] =='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u' ||

               s[i] =='A' || s[i]=='E' || s[i]=='I' || s[i]=='O' || s[i]=='U'  )

                vowel++;

            else

                consonant++;

        }

    }

printf("Total number of vowel = %d\n", vowel);

    printf("Total number of consonant = %d\n", consonant);

return 0;

}

Output:

C program to count total number of vowel or consonant in a string


Write a program in C to convert a decimal number to binary using recursion.

 Source code: //Write a program in C to convert a decimal number to binary using recursion. #include<stdio.h> long convertB_to_D(int d...