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