Our website is made possible by displaying online advertisements to our visitors.Please consider supporting us by disabling your ad blocker.
Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu
Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Translate it in your own Language

Print this Job Post

Print Friendly and PDF
Showing posts with label C Program. Show all posts
Showing posts with label C Program. Show all posts

Monday, November 28, 2016

       



Wednesday, August 3, 2016

Dear readers, these C Programming Interview Questions have been designed specially to get you acquainted with the nature of questions you may encounter during your interview for the subject of C Programming. As per my experience good interviewers hardly plan to ask any particular question during your interview, normally questions start with some basic concept of the subject and later they continue based on further discussion and what you answer −

Friday, May 30, 2014

C programming code

#include <stdio.h>
#include<conio.h> 
void main()
{
   char text[100], blank[100];
   int c = 0, d = 0;
   clrscr();
   printf("Enter some text\n");
   gets(text);
   while (text[c] != '\0')
   {
      if (!(text[c] == ' ' && text[c+1] == ' ')) 
      {
        blank[d] = text[c];
        d++;
      }
      c++;
   }
  blank[d] = '\0';
  printf("Text after removing blanks\n%s\n", blank);
  getch();
}

Output of program:


C programming code using pointers
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<conio.h>
#define SPACE ' '
void main()
{
   char string[100], *blank, *start;
   int length, c = 0, d = 0;
   clrscr();
   printf("Enter a string\n");
   gets(string);
   length = strlen(string);
   blank = string;
   start = (char*)malloc(length+1);
   if ( start == NULL )
   exit(EXIT_FAILURE);
   while(*(blank+c))
   {
      if ( *(blank+c) == SPACE && *(blank+c+1) == SPACE )
      {}
      else
      {
         *(start+d) = *(blank+c);
d++;
      }
      c++;
   }
   *(start+d) = '\0';
   printf("%s\n", start);
   free(start);     
   getch();
}

C programming code

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include<conio.h>
void main()
{
   char first[100], second[100], *temp;
   clrscr();
   printf("Enter the first string\n");
   gets(first);
   printf("Enter the second string\n");
   gets(second);
   printf("\nBefore Swapping\n");
   printf("First string: %s\n",first);
   printf("Second string: %s\n\n",second);
   temp = (char*)malloc(100);
   strcpy(temp,first);
   strcpy(first,second);
   strcpy(second,temp);
   printf("After Swapping\n");
   printf("First string: %s\n",first);
   printf("Second string: %s\n",second);
   getch();
}

Output of program:



Here we will change string case with and without strlwr, strupr functions.

strlwr in c

#include <stdio.h>
#include <string.h>
#include<conio.h> 
void main()
{
    char string[] = "Strlwr in C";
    clrscr();
    printf("%s\n",strlwr(string));
    getch();
}

strupr in c

#include <stdio.h>
#include <string.h>
#include<conio.h>
void main()
{
    char string[] = "strupr in c";
    clrscr();
    printf("%s\n",strupr(string));
    getch();
}

Change string to upper case without strupr

#include <stdio.h>
#include<conio.h> 
void upper_string(char*);
void main()
{
   char string[100];
   clrscr();
   printf("Enter a string to convert it into upper case\n");
   gets(string);
   upper_string(string);
   printf("Entered string in upper case is \"%s\"\n", string);
   getch();
}
void upper_string(char *string)
{
   while(*string)
   {
       if ( *string >= 'a' && *string <= 'z' )
       {
          *string = *string - 32;
       }
       string++;
   }
}

Change string to lower case without strlwr

#include <stdio.h>
#include<conio.h> 
void lower_string(char*);
void main()
{
   char string[100];
   clrscr();
   printf("Enter a string to convert it into lower case\n");
   gets(string);
   lower_string(string);
   printf("Entered string in lower case is \"%s\"\n", string);
   getch();
}
void lower_string(char *string)
{
   while(*string)
   {
      if ( *string >= 'A' && *string <= 'Z' ) 
      {
         *string = *string + 32;
      }
      string++;
   }
}
C program to convert string to integer: It is frequently required to convert a string to an integer in applications. String should consists of digits only and an optional '-' (minus) sign at beginning for integers. For string containing other characters we can stop conversion as soon as a non digit character is encountered but in our program we will handle ideal case when only valid characters are present in string. Library function atoi can be used to convert string to an integer but we will create our own function.

C programming code

#include <stdio.h>
#include<conio.h> 
int toString(char []);
void main()
{
  char a[100];
  int n;
  clrscr();
  printf("Input a valid string to convert to integer\n");
  scanf("%s", a);
  n = toString(a);
  printf("String  = %s\nInteger = %d\n", a, n);
  getch();
}
int toString(char a[]) 
{
  int c, sign, offset, n;
  if (a[0] == '-') 
  {  
  sign = -1;
  }
  if (sign == -1) 
  { 
    offset = 1;
  }
  else 
  {
    offset = 0;
  }
  n = 0;
 for (c = offset; a[c] != '\0'; c++) 
 {
  n = n * 10 + a[c] - '0';
 }
 if (sign == -1) 
{
  n = -n;
 }
 return n;
}

C program to sort a string in alphabetic order: For example if user will enter a string "programming" then output will be "aggimmnoprr" or output string will contain characters in alphabetical order.

C programming code

#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
#include <string.h>
void sort_string(char*);
void main()
{
   char string[100];
   clrscr();
   printf("Enter some text\n");
   gets(string);
   sort_string(string);
   printf("%s\n", string);
   getch();
}
void sort_string(char *s)
{
   int c, d = 0, length;
   char *pointer, *result, ch;
   length = strlen(s);
   result = (char*)malloc(length+1);
   pointer = s;
   for ( ch = 'a' ; ch <= 'z' ; ch++ )
   {
      for ( c = 0 ; c < length ; c++ )
      {
         if ( *pointer == ch )
         {
            *(result+d) = *pointer;
            d++;
         }
         pointer++;
      }
      pointer = s;
   }
   *(result+d) = '\0';
   strcpy(s, result);
   free(result);
}

Output of program:



C substring code

#include <stdio.h>
#include<conio.h>
#include <malloc.h>
char* substring(char*, int, int);
void main() 
{
   char string[100], *pointer;
   int position, length;
   clrscr();
   printf("Enter a string\n");
   gets(string);
   printf("Enter the position and length of substring\n");
   scanf("%d%d",&position, &length);
   pointer = substring( string, position, length);
   printf("Required substring is \"%s\"\n", pointer);
   free(pointer);
   getch();
}
/*C substring function: It returns a pointer to the substring */
char *substring(char *string, int position, int length) 
{
   char *pointer;
   int c;
   pointer = malloc(length+1);
   if (pointer == NULL)
   {
      printf("Unable to allocate memory.\n");
      exit(EXIT_FAILURE);
   }
   for (c = 0 ; c < position -1 ; c++) 
   string++; 
   for (c = 0 ; c < length ; c++)
   {
      *(pointer+c) = *string;      
      string++;   
   }
  *(pointer+c) = '\0';
  return pointer;
}

Output of program:


C code for all substrings of a string

#include <stdio.h>
#include <string.h>
#include<conio.h>
#include <malloc.h>
char* substring(char*, int, int);
void main() 
{
   char string[100], *pointer;
   int position = 1, length = 1, temp, string_length;
   clrscr();
   printf("Enter a string\n");
   gets(string);
   temp = string_length = strlen(string);
   printf("Substring of \"%s\" are\n", string);
   while (position <= string_length)
   {
      while (length <= temp)
      {
         pointer = substring(string, position, length);
         printf("%s\n", pointer);
         free(pointer);
         length++;
      }
      temp--;
      position++;
      length = 1;
   }
  getch();
}

Output of program:




c program to remove or delete vowels from a string, if the input string is "c programming" then output will be "c programming". In the program we create a new string and process entered string character by character, and if a vowel is found it is not added to new string otherwise the character is added to new string, after the string ends we copy the new string into original string. Finally we obtain a string without any vowels.

C programming code

#include <stdio.h>
#include <string.h>
#include<conio.h> 
int check_vowel(char);
int main()
{
  char s[100], t[100];
  int i, j = 0;
  clrscr();
  printf("Enter a string to delete vowels\n");
  gets(s);
  for(i = 0; s[i] != '\0'; i++) 
  {
    if(check_vowel(s[i]) == 0) 
    {      
      t[j] = s[i];
      j++;
    }
  }
  t[j] = '\0';
  strcpy(s, t);    
  printf("String after deleting vowels: %s\n", s);
  getch();
}
 int check_vowel(char c)
{
  switch(c) 
   {
    case 'a':
    case 'A':
    case 'e':
    case 'E':
    case 'i':
    case 'I':
    case 'o':
    case 'O':
    case 'u':
    case 'U':
      return 1;
    default:
      return 0;
  }
}

C programming code using pointers

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#define TRUE 1
#define FALSE 0
int check_vowel(char);
void main()
{
   char string[100], *temp, *pointer, ch, *start;
   printf("Enter a string\n");
   gets(string);
   temp = string;
   pointer = (char*)malloc(100);
   if( pointer == NULL )
   {
      printf("Unable to allocate memory.\n");
      exit(EXIT_FAILURE);
   }
   start = pointer;
   while(*temp)
   {
      ch = *temp;
      if ( !check_vowel(ch) )
      {
         *pointer = ch;
         pointer++;
      }
      temp++;
   }
   *pointer = '\0';
   pointer = start;
   strcpy(string, pointer); /* If you wish to convert original string */
   free(pointer);
   printf("String after removing vowel is \"%s\"\n", string);
   getch();
}
int check_vowel(char a)
{
   if ( a >= 'A' && a <= 'Z' )
   a = a + 'a' - 'A';
   if ( a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
      return TRUE;
   return FALSE;
}

C program for palindrome

#include <stdio.h>
#include <string.h>
#include<conio.h> 
void main()
{
   char a[100], b[100];
   clrscr();
   printf("Enter the string to check if it is a palindrome\n");
   gets(a);
   strcpy(b,a);
   strrev(b);
   if( strcmp(a,b) == 0 )
      printf("Entered string is a palindrome.\n");
   else
      printf("Entered string is not a palindrome.\n");
   getch();
}

Output of program:


Palindrome number in c
#include <stdio.h>
#include<conio.h> 
void main()
{
   int n, reverse = 0, temp;
   clrscr();
   printf("Enter a number to check if it is a palindrome or not\n");
   scanf("%d",&n);
   temp = n;
   while( temp != 0 )
   {
      reverse = reverse * 10;
      reverse = reverse + temp%10;
      temp = temp/10;
   }
   if ( n == reverse )
      printf("%d is a palindrome number.\n", n);
   else
      printf("%d is not a palindrome number.\n", n);
   getch();
}

C program for palindrome without using string functions

#include <stdio.h>
#include <string.h>
#include<conio.h> 
void main()
{
   char text[100];
   int begin, middle, end, length = 0;
   clrscr();
   gets(text);
   while ( text[length] != '\0' )
   length++;
   end = length - 1;
   middle = length/2;
   for( begin = 0 ; begin < middle ; begin++ )
   {
      if ( text[begin] != text[end] )
      {
         printf("Not a palindrome.\n");
         break;
      }
      end--;
   }
   if( begin == middle )
   printf("Palindrome.\n");
   getch();
}

C program check palindrome

#include <stdio.h>
#include<conio.h> 
int is_palindrome(char*);
void copy_string(char*, char*);
void reverse_string(char*);
int string_length(char*);
int compare_string(char*, char*);
void main()
{
   char string[100];
   int result;
   clrscr();
   printf("Enter a string\n");
   gets(string);
   result = is_palindrome(string);
   if ( result == 1 )
   printf("\"%s\" is a palindrome string.\n", string);
   else
   printf("\"%s\" is not a palindrome string.\n", string); 
   getch();
}
int is_palindrome(char *string)
{
   int check, length;
   char *reverse;
   length = string_length(string);    
   reverse = (char*)malloc(length+1);    
   copy_string(reverse, string);
   reverse_string(reverse);
   check = compare_string(string, reverse);
   free(reverse);
   if ( check == 0 )
   return 1;
   else
   return 0;
}
int string_length(char *string)
{
   int length = 0;  
   while(*string)
   {
      length++;
      string++;
   }
   return length;
}
void copy_string(char *target, char *source)
{
   while(*source)
   {
      *target = *source;
      source++;
      target++;
   }
   *target = '\0';
}
void reverse_string(char *string) 
{
   int length, c;
   char *begin, *end, temp;
   length = string_length(string);
   begin = string;
   end = string;
   for ( c = 0 ; c < ( length - 1 ) ; c++ )
   end++;
    for ( c = 0 ; c < length/2 ; c++ ) 
   {        
      temp = *end;
      *end = *begin;
      *begin = temp;
      begin++;
      end--;
   }
}
int compare_string(char *first, char *second)
{
   while(*first==*second)
   {
      if ( *first == '\0' || *second == '\0' )
         break;
      first++;
      second++;
   }
   if( *first == '\0' && *second == '\0' )
      return 0;
   else
      return -1;
}

C programming code

#include <stdio.h>
#include <string.h>
#include<conio.h> 
void main()
{
   char arr[100];
   clrscr();
   printf("Enter a string to reverse\n");
   gets(arr);
   strrev(arr);
   printf("Reverse of entered string is \n%s\n",arr);
   getch();
}

C program to reverse a string using pointers

 Now we will invert string using pointers or without using library function strrev.
#include<stdio.h>
#include<conio.h> 
int string_length(char*);
void reverse(char*);
void main() 
{
   char string[100];
   clrscr(); 
   printf("Enter a string\n");
   gets(string);
   reverse(string);
   printf("Reverse of entered string is \"%s\".\n", string);
   getch();
}
void reverse(char *string) 
{
   int length, c;
   char *begin, *end, temp;
   length = string_length(string);
   begin = string;
   end = string;
    for ( c = 0 ; c < ( length - 1 ) ; c++ )
      end++;
    for ( c = 0 ; c < length/2 ; c++ ) 
   {        
      temp = *end;
      *end = *begin;
      *begin = temp;
      begin++;
      end--;
   }
}
int string_length(char *pointer)
{
   int c = 0;
   while( *(pointer+c) != '\0' )
    c++;
    return c;
}

C program to reverse a string using recursion

#include <stdio.h>
#include <string.h>
#include<conio.h> 
void reverse(char*, int, int);
void main()
{
   char a[100];
   gets(a);
   reverse(a, 0, strlen(a)-1);
   printf("%s\n",a);
   getch();
}
void reverse(char *x, int begin, int end)
{
   char c;
   if (begin >= end)
   return;
   c = *(x+begin);
   *(x+begin) = *(x+end);
   *(x+end) = c;
   reverse(x, ++begin, --end);
}
This program concatenates strings, for example if the first string is "c " and second string is "program" then on concatenating these two strings we get the string "c program". To concatenate two strings we use strcat function of string.h, to concatenate without using library function see another code below which uses pointers.

C programming code

#include <stdio.h>
#include <string.h>
#include<conio.h> 
void main()
{
   char a[100], b[100];
   clrscr();
   printf("Enter the first string\n");
   gets(a);
   printf("Enter the second string\n");
   gets(b);
   strcat(a,b);
   printf("String obtained on concatenation is %s\n",a);
   getch();
}

String concatenation without strcat

#include <stdio.h>
#include<conio.h> 
void concatenate_string(char*, char*);
void main()
{
    char original[100], add[100];
    clrscr();
    printf("Enter source string\n");
    gets(original);
    printf("Enter string to concatenate\n");
    gets(add);
    concatenate_string(original, add);
    printf("String after concatenation is \"%s\"\n", original);
    getch();
}
void concatenate_string(char *original, char *add)
{
   while(*original)
      original++;
   while(*add)
   {
      *original = *add;
      add++;
      original++;
   }
   *original = '\0';
}


C program to copy a string

#include<stdio.h>
#include<string.h>
#include<conio.h> 
void main()
{
   char source[] = "C program";
   char destination[50];
   clrscr();
   strcpy(destination, source);
   printf("Source string: %s\n", source);
   printf("Destination string: %s\n", destination);
   getch();
}

c program to copy a string using pointers

#include<stdio.h>
#include<conio.h>
#include<string.h> 
void copy_string(char*, char*);
void main()
{
    char source[100], target[100];
    clrscr();
    printf("Enter source string\n");
    gets(source);
    copy_string(target, source);
    printf("Target string is \"%s\"\n", target);
    getch();
}
void copy_string(char *target, char *source)
{
   while(*source)
   {
      *target = *source;
      source++;
      target++;
   }
   *target = '\0';
}

Thursday, May 29, 2014

C program to compare two strings using strcmp

#include <stdio.h>
#include <string.h>
void main()
{
   char a[100], b[100];
   clrscr();
   printf("Enter the first string\n");
   gets(a);
   printf("Enter the second string\n");
   gets(b);
   if( strcmp(a,b) == 0 )
      printf("Entered strings are equal.\n");
   else
      printf("Entered strings are not equal.\n");
    getch();
}

Output of program:

C program to compare two strings without using strcmp

Here we create our own function to compare strings.
int compare(char a[], char b[])
{
   int c = 0;
   while( a[c] == b[c] )
   {
      if( a[c] == '\0' || b[c] == '\0' )
      break;
      c++;
   }
   if( a[c] == '\0' && b[c] == '\0' )
      return 0;
   else
      return -1;
}

C program to compare two strings using pointers

In this method we will make our own function to perform string comparison, we will use character pointers in our function to manipulate string.
#include<stdio.h>
#include<conio.h> 
int compare_string(char*, char*);
void main()
{
    char first[100], second[100], result;
    clrscr(); 
    printf("Enter first string\n");
    gets(first);
    printf("Enter second string\n");
    gets(second);
    result = compare_string(first, second);
    if ( result == 0 )
       printf("Both strings are same.\n");
    else
       printf("Entered strings are not equal.\n");
    getch();
}
int compare_string(char *first, char *second)
{
   while(*first==*second)
   {
      if ( *first == '\0' || *second == '\0' )
      break;
      first++;
      second++;
   }
   if( *first == '\0' && *second == '\0' )
      return 0;
   else
      return -1;
}


Wednesday, May 28, 2014

This program prints length of string, for example consider the string "c programming" it's length is 13. Null character is not counted when calculating string length. To find string length we use strlen function of string.h.

C program to find string length

#include <stdio.h>
#include <string.h>
void main()
{
   char a[100];
   int length;
   clrscr();
   printf("Enter a string to calculate it's length\n");
   gets(a);
   length = strlen(a);
   printf("Length of entered string is = %d\n",length);
   getch();
}

C program to find string length without strlen

#include <stdio.h>
#include<conio.h> 
void main()
{
   char array[100], *pointer;
   int length = 0;
   clrscr();
   printf("Enter a string\n");
   gets(array);
   pointer = array;
   while(*(pointer+length))
   length++;
   printf("Length of entered string = %d\n",length);
   getch();
}

Function to find string length

int string_length(char *s)
{
   int c = 0;
   while(*(s+c))
   c++;
   return c;
}

C programming code

#include <stdio.h>
#include<conio.h> 
void main()
{
    char array[20] = "Hello World";
    clrscr();
    printf("%s\n",array);
    getch();
}

To input a string we use scanf function.

C programming code

#include <stdio.h>
#include<conio.h> 
void main()
{
    char array[100];
    clrscr(); 
    printf("Enter a string\n");
    scanf("%s", array);
    printf("You entered the string %s\n",array);
    getch();
}

Input string containing spaces

#include <stdio.h>
#include<conio.h> 
void main()
{
   char a[80];
   clrscr(); 
   gets(a);
   printf("%s\n", a);
   getch();
}
**Note that scanf can only input single word strings, to receive strings containing spaces use gets function.
c program to multiply matrices (two dimensional array), this program multiplies two matrices which will be entered by the user. Firstly user will enter the order of a matrix. If the entered orders of two matrix is such that they can't be multiplied then an error message is displayed on the screen. You have already studied the logic to multiply them in Mathematics.

Matrix multiplication in c language

#include <stdio.h>
#include<conio.h> 
void main()
{
  int m, n, p, q, c, d, k, sum = 0;
  int first[10][10], second[10][10], multiply[10][10];
  clrscr();
  printf("Enter the number of rows and columns of first matrix\n");
  scanf("%d%d", &m, &n);
  printf("Enter the elements of first matrix\n");
  for (  c = 0 ; c < m ; c++ )
  for ( d = 0 ; d < n ; d++ )
  scanf("%d", &first[c][d]);
  printf("Enter the number of rows and columns of second matrix\n");
  scanf("%d%d", &p, &q);
  if ( n != p )
    printf("Matrices with entered orders can't be multiplied with each other.\n");
  else
  {
    printf("Enter the elements of second matrix\n");
   for ( c = 0 ; c < p ; c++ )
   for ( d = 0 ; d < q ; d++ )
   scanf("%d", &second[c][d]);
   for ( c = 0 ; c < m ; c++ )
    {
      for ( d = 0 ; d < q ; d++ )
      {
        for ( k = 0 ; k < p ; k++ )
        {
          sum = sum + first[c][k]*second[k][d];
        }
       multiply[c][d] = sum;
       sum = 0;
      }
    }
    printf("Product of entered matrices:-\n");
    for ( c = 0 ; c < m ; c++ )
    {
      for ( d = 0 ; d < q ; d++ )
        printf("%d\t", multiply[c][d]);
       printf("\n");
    }
  }
getch();
}

Output of the program:


Copyright @ CrackMNC 2014-2024
Divas Nikhra Theme by Crack MNC