Sunday, 17 August 2014

REVERSING A STRING

REVERSING A STRING

#include<stdio.h>
#include<string.h>
void Reverse(char str[]);
int main(){
    char str[100];
    printf("Enter a string to reverse: ");
    gets(str);
    Reverse(str);
    printf("Reversed string: ");
    puts(str);
 return 0;
}
void Reverse(char str[]){
    int i,j;
    char temp[100];
    for(i=strlen(str)-1,j=0; i+1!=0; --i,++j)
    {
        temp[j]=str[i];
    }
    temp[j]='\0';
    strcpy(str,temp);
}

Thursday, 17 July 2014

DIFFERENT FUNCTIONS IN STRINGS EXAMPLE

PROGRAM TO ILLUSTRATE DIFFERENT FUNCTIONS IN STRINGS


#include <stdio.h>
#include <string.h>
#include <conio.h>

void main ( )
{
   char str1[12] = "Hello";
   char str2[12] = "World";
   char str3[12];
   int  len ;

   /* copy str1 into str3 */
   strcpy(str3, str1);
   printf("strcpy( str3, str1) :  %s\n", str3 );

   /* concatenates str1 and str2 */
   strcat( str1, str2);
   printf("strcat( str1, str2):   %s\n", str1 );

   /* total lenghth of str1 after concatenation */
   len = strlen(str1);
   printf("strlen(str1) :  %d\n", len );

getch ( );
}

DIFFERENT FUNCTIONS IN STRINGS

  •  These functions help to reduce the length of our program i.e. save space and time .
  • C supports a wide range of functions that manipulate null-terminated strings.
S.N.Function & Purpose
1strcpy(s1, s2);
Copies string s2 into string s1.
2strcat(s1, s2);
Concatenates string s2 onto the end of string s1.
3strlen(s1);
Returns the length of string s1.
4strcmp(s1, s2);
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
5strchr(s1, ch);
Returns a pointer to the first occurrence of character ch in string s1.
6strstr(s1, s2);
Returns a pointer to the first occurrence of string s2 in string s1.

STRING PROGRAM ( easy )

A VERY SIMPLE STRING PROGRAM


#include<stdio.h>
#include<conio.h>

void main( )
{

char greeting [6] = { 'h', 'e', 'l', 'l', 'o', ' \n ' };
printf("%s", greeting );
getch( );

}

OUTPUT - hello

STRINGS

STRINGS

  • The string in C programming language is actually a one-dimensional array of characters which is terminated by a null character '\0'. Thus a null-terminated string contains the characters that comprise the string followed by a null.
  • The following declaration and initialization create a string consisting of the word "Hello". To hold the null character at the end of the array, the size of the character array containing the string is one more than the number of characters in the word "Hello."
    char greeting[6] = {'H', 'e', 'l', 'l', 'o',                     '\0'};
  • NOTE :-
  • Above statement can also be written as :
  • char greeting[] = "Hello";
  • 
    

Monday, 14 July 2014

PROGRAM FOR 2D- ARRAY


PROGRAM TO READ AND PRINT A 2D ARRAY


#include<stdio.h>
#include<conio.h>

void main()
{
int n,m,i,j,a[20][20];
clrscr ( );
printf("enter number of rows\n");
scanf("%d",&n);
printf("enter number of columns\n");
scanf("%d",&m);
printf("enter elements in the array\n");

for(i=0;i<n;i++)
{
  for(j=0;j<m;j++)
{
scanf("%d",&a[i][j]);
}
 }
printf("the elements are\n");

for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d",a[i][j]);
}
}
getch( );
}

Sunday, 13 July 2014

MULTI- DIMENSIONAL ARRAYS

MULTI- DIMENSIONAL ARRAYS

  • Multi dimensional arrays are same as one- dimensional array. The only difference is that we have more than one dimension to our stack of data.
  • Observe one thing that if you are using a 1D array, you would need a single for loop and for a nD array, you would need n for loops. 

SYNTAX :-

storage_class data_type array_name [dimension][dimension ]....[dimension ];

TWO- DIMENSIONAL ARRAY

SYNTAX :-

storage_class data_type array_name [ ROW ] [COLUMN ];

Eg-
      int student [10] [5];     

NOTE----   5*10= 50 characters in array student

Tuesday, 8 July 2014

A PROGRAM TO READ AND PRINT ELEMENTS IN A SINGLE DIMENSIONAL ARRAY

A PROGRAM TO READ AND PRINT ELEMENTS IN A SINGLE DIMENSIONAL ARRAY

#include<stdio.h>
#include<conio.h>
void main( )
{
int n,i,a[20];
clrscr ( );
printf ("enter number of terms in the array\n");
scanf ("%d",&n);
printf ("enter elements in the array\n");
for( i=0 ; i < n ; i++ )
{
scanf ("%d", &a[i] );
}
printf ("the elements are\n");
for ( i = 0 ; i < n ; i++)
{
printf ("%d" , a[i] );
}
getch( );
}

SINGLE- DIMENSIONAL ARRAY

SINGLE- DIMENSIONAL ARRAY

  • IT IS SOMETIMES CALLED A LIST.
  • IT IS A COLLECTION OF SIMILAR DATA TYPES.
  • IT IS THE SIMPLEST FORM OF AN ARRAY.
  • DECLARATION PART :-
Specify its name , size and data types of the item.

SYNTAX :-
        storage_class data_type array_name[size];

EXAMPLE:-
        int arr[ 20 ];
        float array[40];
        int X[5] ={ 2,3,4,5,6 };

NOTE- 
Array elements are accessed by their index value. The index of an array starts from zero.
int marks[4] = { 70,80,90,85 };
The value 70 lies in the index position zero and 85 lies in index position three ( n - 1 ).

Monday, 7 July 2014

ARRAY

ARRAY



WHAT IS AN ARRAY?
  • An array is a derived data type from fundamental data type.
  • It is a collection of variables of the same type that are referenced by a common name which are stored in a contiguous memory location.

DECLARING AN ARRY

  • Arrays are declared by specifying its data type,name and the number of elements the array holds between square brackets immediately following the array name.
Eg- int name [20];
    -  int student_marks[5][20];

TYPES- 1 DIMENSIONAL
            - N DIMENSIONAL

C PROGRAM TO REVERSE A 3 DIGIT NUMBER

REVERSING A 3 DIGIT NUMBER



#include<conio.h>
#include <stdio.h>
 
int main()
{
   int n, reverse = 0;
 clrscr ();
   printf("Enter a number to reverse\n");
   scanf("%d",&n);
 
   while (n != 0)
   {
      reverse = reverse * 10;
      reverse = reverse + n%10;
      n = n/10;
   }
 
   printf("Reverse of entered number is = %d\n", reverse);
 
   return 0;
}

FIBONACCI SERIES( using for loop )

FIBONACCI SERIES( using for loop )


#include<stdio.h>
#include<conio.h>
void main();
{
int n,first=0,second=1,next,c;
clrscr( );
printf("enter no. of terms");
scanf("%d",&n);
for(c=0;c<n;c++)
{
if(c<=1)
{
next=c;
}
else
{
next=first+second;
first=second;
second=next;
}
}
printf("%d",n);
getch();
}

Monday, 30 June 2014

APPLICATION OF DO- WHILE LOOP

APPLICATION OF DO- WHILE LOOP




#include <stdio.h>

 void main()
{
    int i = 10;

    do{
       printf("Hello %d\n", i );
       i = i -1;
    }
while ( i > 0 );
}


OUTPUT-

Hello 10
Hello 9
Hello 8
Hello 7
Hello 6
Hello 5
Hello 4
Hello 3
Hello 2
Hello 1

A SIMPLE WHILE LOOP APPLICATION USING BREAKS

SIMPLE WHILE LOOP APPLICATION USING BREAKS








#include <stdio.h>

main()
{
    int i = 10;

    while ( i > 0 )
    {
       printf("Hello %d\n", i );
       i = i -1;
       if( i == 6 )
       {
          break;
       }
    }
}





OUTPUT-         Hello 10
Hello 9
Hello 8
Hello 7

FACTORIAL OF A NUMBER USING C PROGRAM

PROGRAM TO CALCULATE FACTORIAL



#include <stdio.h>

int main()
{ int number,factorial; printf("Enter a number.\n"); scanf("%d",&number); factorial=1; while (number>0){ /* while loop continues until test condition number>0 is true */ factorial=factorial*number; --number; } printf("Factorial=%d",factorial); return 0;
}

Saturday, 28 June 2014

GUESS THE OUTPUT

GUESS THE OUTPUT

int inum = 2;

switch( inum )

{

case 1.5:

printf("one and half");

break;

case 2:
printf("two");

break;

case A:
printf( "A is a character");

break;

default:
printf(" invalid ");

break;
}













ANSWER- WE WOULD GET ERROR BECAUSE VALUES IN THE CASES MUST BE INTEGERS