PROGRAM FOR SEARCHING A CHARACTER IN A STRING

#include
#include

void main()
{
char str[20],ch;
int cnt=0,i;
clrscr();

printf("\n\n\n\t\t Program for searching a character in a string");

printf("\n\n\n\t\t\t Enter a string = ");
scanf("%s",&str);

printf("\n\t\t\t Enter the character to search = ");
flushall();
scanf("%c",&ch);

for(i=0;str[i]!='\0';i++)
{
if(str[i]==ch)
cnt++;

}

if(cnt==0)
printf("\n\n\t\t\t The character '%c' is not found in the string...",ch);
else
printf("\n\n\t\t\t The character '%c' is occuring %d times in the string.",ch,cnt);


getch();

}

Related Links :

Calculating power of a number

#include
#include


void main()
{
double power(double,int);
double n,ans;
int p;
clrscr();

printf("\n\n\n\t\t\t Program for calculating power of a number");


printf("\n\n\n\t\t\t Enter the base number = ");
scanf("%lf",&n);

printf("\n\t\t\t Enter the power = ");
scanf("%d",&p);

ans=power(n,p);

printf("\n\n\t\t\t Result of %.2lf to power %d is %.2lf",n,p,ans);

getch();

}

double power(double nn,int pp)
{
if(pp==0)
return (1);
else
return(nn*power(nn,pp-1));
}

Related Links :

Printing Fibonacci Series

#include
#include

void main()
{
int fibonacci();
int n,f;
clrscr();

printf("\n\n\n\t\t\t Program for printing Fibonacci Series");


printf("\n\n\n\t\t\t How many Fibonacci numbers you want to see ? = ");
scanf("%d",&n);


while(n)
{
f=fibonacci();
printf("\n\t\t\t %d",f);
n--;
}

getch();

}

int fibonacci()
{
static a=0,b=1,c=0;
c=a+b;
b=a;
a=c;
return(c);

}

Related Links :

calculating an Armstrong number

#include
#include

void main()
{
int n,r,res=0,t;
clrscr();
printf("\n\n\n\t\t\t Program for calculating an Armstrong number");


printf("\n\n\n\t\t\t Enter any number = ");
scanf("%d",&n);
t=n;

while(n>0)
{
r=n%10;
res=res+r*r*r;
n=n/10;
}

if(t==res)
printf("\n\n\n\t\t\t %d is an Armstrong number.",t);

else
printf("\n\n\n\t\t\t %d is Not an Armstrong number.",t);



getch();


}

Related Links :

Program for generating a sequence

#include
#include

void main()
{

int n,f=0;
clrscr();
printf("\n\n\n\t\t\t Program for generating a sequence");


printf("\n\n\n\t\t\t Enter any no. = ");
scanf("%d",&n);

if(n<1)
{
printf("\n\n\t\t\t Error !");
exit(0);
}
else
{
while(n!=1)
{
if(n%2==1)
{
n=n*3+1;
f++;
}
else
{
n=n/2;
f++;
}

printf("\n\n\t\t\t Next value %d",n);
}

printf("\n\n\t\t\t Final value %d, Operation performed %d",n,f);


}


getch();


}

Related Links :

Program for printing Prime numbers

#include
#include
#include

void main()
{
int i=1,j,f;
clrscr();

printf("\n\n\n\t\t\t Program for printing Prime numbers");

while(i<=1000)
{
j=1;
f=0;
while(j<=i)
{
if(i%j==0)
f++;
j++;
}

if(f<=2)
printf("\n\t\t\t %d",i);
i++;

delay(100);
}

getch();

}

Related Links :

Program for reversing the digit

#include
#include

void main()
{

int n,rev=0,r;
char ch;
clrscr();

printf("\n\n\n\t\t\t Program for reversing the digit\n ");


printf("\n\n\n\t\t\t >> Enter any number = ");
scanf("%d",&n);

while(n>0)
{
r=n%10;
rev=rev*10+r;
n=n/10;

}

printf("\n\n\t\t\t Reverse of digit is = %d",rev);

getch();

}

Related Links :

CALCULATER In C

#include
#include

void main()
{

int a,b;
char opr,ch;
start:
clrscr();

printf("\n\n\n\t\t\t NUMERIC CALCULATER");


printf("\t\t\t + Addtion\n\n");
printf("\t\t\t - Subtarction\n\n");
printf("\t\t\t * Multiplication\n\n");
printf("\t\t\t / Devide\n\n");
printf("\t\t\t % Remainder\n\n");


printf("\n\n\n\n\t\t\t Enter First number = ");
scanf("%d",&a);

printf("\n\t\t\t Enter Second number = ");
scanf("%d",&b);

printf("\n\n\t\t\t Choose any operator from above menu = ");
flushall();
scanf("%c",&opr);

switch(opr)
{
case '+': printf("\n\n\t\t\t Result = %d",a+b); break;

case '-': printf("\n\n\t\t\t Result = %d",a-b); break;

case '*': printf("\n\n\t\t\t Result = %d",a*b); break;

case '/': printf("\n\n\t\t\t Result = %.2f",(float)a/b); break;

case '%': printf("\n\n\t\t\t Result = %d",a%b); break;

default : printf("\n\n\t\t\t Invalid Operator !");
printf("\n\n\t\t\t Do you want to try agin ? (y/n) = ");
flushall();
scanf("%c",&ch);

if(ch=='y' || ch=='Y')
goto start;
};


getch();

}

Related Links :

Conversion of Centimeters in Feets & Inches

#include
#include

void main()
{

float h,inch;
int feet;
clrscr();

printf("\n\n\n\t\t Conversion of Centimeters in Feets & Inches\n");


printf("\t\t Enter your Hieght in Centimeters = ");
scanf("%f",&h);

inch=(float)h/2.54;
feet=inch/12;
inch=(inch/12-feet)*12;


printf("\n\n\n\t\t Your hieght is = %d feet %.1f inch",feet,inch);


getch();

}

Related Links :

Program for finding minimum number

/* Problem : sol2_1_2.c
Solution : */



#include
#include

void main()
{
int a,b,c,min;
clrscr();
printf("\n\n\t\t\t Program for finding minimum number \n\n");


printf("Enter first number:-");
scanf("%d",&a);

printf("\nEnter Second number:-");
scanf("%d",&b);

printf("\nEnter Third number:-");
scanf("%d",&c);

min=a < b?(a
printf("\n\n The smallest number is %d",min);

getch();


}

Related Links :

Pointer types and Arrays

Okay, let's move on. Let us consider why we need to identify the type of variable that a pointer points to, as in:

     int *ptr;

One reason for doing this is so that later, once ptr "points to" something, if we write:

    *ptr = 2;

the compiler will know how many bytes to copy into that memory location pointed to by ptr. If ptr was declared as pointing to an integer, 2 bytes would be copied, if a long, 4 bytes would be copied. Similarly for floats and doubles the appropriate number will be copied. But, defining the type that the pointer points to permits a number of other interesting ways a compiler can interpret code. For example, consider a block in memory consisting if ten integers in a row. That is, 20 bytes of memory are set aside to hold 10 integers.

Now, let's say we point our integer pointer ptr at the first of these integers. Furthermore lets say that integer is located at memory location 100 (decimal). What happens when we write:

    ptr + 1;

Because the compiler "knows" this is a pointer (i.e. its value is an address) and that it points to an integer (its current address, 100, is the address of an integer), it adds 2 to ptr instead of 1, so the pointer "points to" the next integer, at memory location 102. Similarly, were the ptr declared as a pointer to a long, it would add 4 to it instead of 1. The same goes for other data types such as floats, doubles, or even user defined data types such as structures. This is obviously not the same kind of "addition" that we normally think of. In C it is referred to as addition using "pointer arithmetic", a term which we will come back to later.

Similarly, since ++ptr and ptr++ are both equivalent to ptr + 1 (though the point in the program when ptr is incremented may be different), incrementing a pointer using the unary ++ operator, either pre- or post-, increments the address it stores by the amount sizeof(type) where "type" is the type of the object pointed to. (i.e. 2 for an integer, 4 for a long, etc.).

Since a block of 10 integers located contiguously in memory is, by definition, an array of integers, this brings up an interesting relationship between arrays and pointers.

Consider the following:

    int my_array[] = {1,23,17,4,-5,100};

Here we have an array containing 6 integers. We refer to each of these integers by means of a subscript to my_array, i.e. using my_array[0] through my_array[5]. But, we could alternatively access them via a pointer as follows:

    int *ptr;
ptr = &my_array[0]; /* point our pointer at the first
integer in our array */

And then we could print out our array either using the array notation or by dereferencing our pointer. The following code illustrates this:

#include 

int my_array[] = {1,23,17,4,-5,100};
int *ptr;

int main(void)
{
int i;
ptr = &my_array[0]; /* point our pointer to the first
element of the array */
printf("\n\n");
for (i = 0; i < 6; i++)
{
printf("my_array[%d] = %d ",i,my_array[i]); /*<-- A */
printf("ptr + %d = %d\n",i, *(ptr + i)); /*<-- B */
}
return 0;
}

Compile and run the above program and carefully note lines A and B and that the program prints out the same values in either case. Also observe how we dereferenced our pointer in line B, i.e. we first added i to it and then dereferenced the new pointer. Change line B to read:

    printf("ptr + %d = %d\n",i, *ptr++);

and run it again... then change it to:

    printf("ptr + %d = %d\n",i, *(++ptr));

and try once more. Each time try and predict the outcome and carefully look at the actual outcome.

In C, the standard states that wherever we might use &var_name[0] we can replace that with var_name, thus in our code where we wrote:

    ptr = &my_array[0];

we can write:

    ptr = my_array;

to achieve the same result.

This leads many texts to state that the name of an array is a pointer. I prefer to mentally think "the name of the array is the address of first element in the array". Many beginners (including myself when I was learning) have a tendency to become confused by thinking of it as a pointer. For example, while we can write

    ptr = my_array;

we cannot write

    my_array = ptr;

The reason is that while ptr is a variable, my_array is a constant. That is, the location at which the first element of my_array will be stored cannot be changed once my_array[] has been declared.

Earlier when discussing the term "lvalue" I cited K&R-2 where it stated:

"An object is a named region of storage; an lvalue is an expression referring to an object".

This raises an interesting problem. Since my_array is a named region of storage, why is my_array in the above assignment statement not an lvalue? To resolve this problem, some refer to my_array as an "unmodifiable lvalue".

Modify the example program above by changing

    ptr = &my_array[0];

to

    ptr = my_array;

and run it again to verify the results are identical.

Now, let's delve a little further into the difference between the names ptr and my_array as used above. Some writers will refer to an array's name as a constant pointer. What do we mean by that? Well, to understand the term "constant" in this sense, let's go back to our definition of the term "variable". When we declare a variable we set aside a spot in memory to hold the value of the appropriate type. Once that is done the name of the variable can be interpreted in one of two ways. When used on the left side of the assignment operator, the compiler interprets it as the memory location to which to move that value resulting from evaluation of the right side of the assignment operator. But, when used on the right side of the assignment operator, the name of a variable is interpreted to mean the contents stored at that memory address set aside to hold the value of that variable.

With that in mind, let's now consider the simplest of constants, as in:

    int i, k;
i = 2;

Here, while i is a variable and then occupies space in the data portion of memory, 2 is a constant and, as such, instead of setting aside memory in the data segment, it is imbedded directly in the code segment of memory. That is, while writing something like k = i; tells the compiler to create code which at run time will look at memory location &i to determine the value to be moved to k, code created by i = 2; simply puts the 2 in the code and there is no referencing of the data segment. That is, both k and i are objects, but 2 is not an object.

Similarly, in the above, since my_array is a constant, once the compiler establishes where the array itself is to be stored, it "knows" the address of my_array[0] and on seeing:

    ptr = my_array;

it simply uses this address as a constant in the code segment and there is no referencing of the data segment beyond that.

This might be a good place explain further the use of the (void *) expression used in Program 1.1 of Chapter 1. As we have seen we can have pointers of various types. So far we have discussed pointers to integers and pointers to characters. In coming chapters we will be learning about pointers to structures and even pointer to pointers.

Also we have learned that on different systems the size of a pointer can vary. As it turns out it is also possible that the size of a pointer can vary depending on the data type of the object to which it points. Thus, as with integers where you can run into trouble attempting to assign a long integer to a variable of type short integer, you can run into trouble attempting to assign the values of pointers of various types to pointer variables of other types.

To minimize this problem, C provides for a pointer of type void. We can declare such a pointer by writing:

void *vptr;

A void pointer is sort of a generic pointer. For example, while C will not permit the comparison of a pointer to type integer with a pointer to type character, for example, either of these can be compared to a void pointer. Of course, as with other variables, casts can be used to convert from one type of pointer to another under the proper circumstances. In Program 1.1. of Chapter 1 I cast the pointers to integers into void pointers to make them compatible with the %p conversion specification. In later chapters other casts will be made for reasons defined therein.

Well, that's a lot of technical stuff to digest and I don't expect a beginner to understand all of it on first reading. With time and experimentation you will want to come back and re-read the first 2 chapters. But for now, let's move on to the relationship between pointers, character arrays, and strings.


Related Links :

What is a pointer?

One of those things beginners in C find difficult is the concept of pointers. The purpose of this tutorial is to provide an introduction to pointers and their use to these beginners.

I have found that often the main reason beginners have a problem with pointers is that they have a weak or minimal feeling for variables, (as they are used in C). Thus we start with a discussion of C variables in general.

A variable in a program is something with a name, the value of which can vary. The way the compiler and linker handles this is that it assigns a specific block of memory within the computer to hold the value of that variable. The size of that block depends on the range over which the variable is allowed to vary. For example, on PC's the size of an integer variable is 2 bytes, and that of a long integer is 4 bytes. In C the size of a variable type such as an integer need not be the same on all types of machines.

When we declare a variable we inform the compiler of two things, the name of the variable and the type of the variable. For example, we declare a variable of type integer with the name k by writing:

    int k;

On seeing the "int" part of this statement the compiler sets aside 2 bytes of memory (on a PC) to hold the value of the integer. It also sets up a symbol table. In that table it adds the symbol k and the relative address in memory where those 2 bytes were set aside.

Thus, later if we write:

    k = 2;

we expect that, at run time when this statement is executed, the value 2 will be placed in that memory location reserved for the storage of the value of k. In C we refer to a variable such as the integer k as an "object".

In a sense there are two "values" associated with the object k. One is the value of the integer stored there (2 in the above example) and the other the "value" of the memory location, i.e., the address of k. Some texts refer to these two values with the nomenclature rvalue (right value, pronounced "are value") and lvalue (left value, pronounced "el value") respectively.

In some languages, the lvalue is the value permitted on the left side of the assignment operator '=' (i.e. the address where the result of evaluation of the right side ends up). The rvalue is that which is on the right side of the assignment statement, the 2 above. Rvalues cannot be used on the left side of the assignment statement. Thus: 2 = k; is illegal.

Actually, the above definition of "lvalue" is somewhat modified for C. According to K&R II (page 197): [1]

"An object is a named region of storage; an lvalue is an expression referring to an object."

However, at this point, the definition originally cited above is sufficient. As we become more familiar with pointers we will go into more detail on this.

Okay, now consider:

   int j, k;

k = 2;
j = 7; <-- line 1
k = j; <-- line 2

In the above, the compiler interprets the j in line 1 as the address of the variable j (its lvalue) and creates code to copy the value 7 to that address. In line 2, however, the j is interpreted as its rvalue (since it is on the right hand side of the assignment operator '='). That is, here the j refers to the value stored at the memory location set aside for j, in this case 7. So, the 7 is copied to the address designated by the lvalue of k.

In all of these examples, we are using 2 byte integers so all copying of rvalues from one storage location to the other is done by copying 2 bytes. Had we been using long integers, we would be copying 4 bytes.

Now, let's say that we have a reason for wanting a variable designed to hold an lvalue (an address). The size required to hold such a value depends on the system. On older desk top computers with 64K of memory total, the address of any point in memory can be contained in 2 bytes. Computers with more memory would require more bytes to hold an address. Some computers, such as the IBM PC might require special handling to hold a segment and offset under certain circumstances. The actual size required is not too important so long as we have a way of informing the compiler that what we want to store is an address.

Such a variable is called a pointer variable (for reasons which hopefully will become clearer a little later). In C when we define a pointer variable we do so by preceding its name with an asterisk. In C we also give our pointer a type which, in this case, refers to the type of data stored at the address we will be storing in our pointer. For example, consider the variable declaration:

   int *ptr;

ptr is the name of our variable (just as k was the name of our integer variable). The '*' informs the compiler that we want a pointer variable, i.e. to set aside however many bytes is required to store an address in memory. The int says that we intend to use our pointer variable to store the address of an integer. Such a pointer is said to "point to" an integer. However, note that when we wrote int k; we did not give k a value. If this definition is made outside of any function ANSI compliant compilers will initialize it to zero. Similarly, ptr has no value, that is we haven't stored an address in it in the above declaration. In this case, again if the declaration is outside of any function, it is initialized to a value guaranteed in such a way that it is guaranteed to not point to any C object or function. A pointer initialized in this manner is called a "null" pointer.

The actual bit pattern used for a null pointer may or may not evaluate to zero since it depends on the specific system on which the code is developed. To make the source code compatible between various compilers on various systems, a macro is used to represent a null pointer. That macro goes under the name NULL. Thus, setting the value of a pointer using the NULL macro, as with an assignment statement such as ptr = NULL, guarantees that the pointer has become a null pointer. Similarly, just as one can test for an integer value of zero, as in if(k == 0), we can test for a null pointer using if (ptr == NULL).

But, back to using our new variable ptr. Suppose now that we want to store in ptr the address of our integer variable k. To do this we use the unary & operator and write:

    ptr = &k;

What the & operator does is retrieve the lvalue (address) of k, even though k is on the right hand side of the assignment operator '=', and copies that to the contents of our pointer ptr. Now, ptr is said to "point to" k. Bear with us now, there is only one more operator we need to discuss.

The "dereferencing operator" is the asterisk and it is used as follows:

    *ptr = 7;

will copy 7 to the address pointed to by ptr. Thus if ptr "points to" (contains the address of) k, the above statement will set the value of k to 7. That is, when we use the '*' this way we are referring to the value of that which ptr is pointing to, not the value of the pointer itself.

Similarly, we could write:

 printf("%d\n",*ptr);

to print to the screen the integer value stored at the address pointed to by ptr;.

One way to see how all this stuff fits together would be to run the following program and then review the code and the output carefully.

------------ Program 1.1 ---------------------------------

/* Program 1.1 from PTRTUT10.TXT 6/10/97 */

#include

int j, k;
int *ptr;

int main(void)
{
j = 1;
k = 2;
ptr = &k;
printf("\n");
printf("j has the value %d and is stored at %p\n", j, (void *)&j);
printf("k has the value %d and is stored at %p\n", k, (void *)&k);
printf("ptr has the value %p and is stored at %p\n", ptr, (void *)&ptr);
printf("The value of the integer pointed to by ptr is %d\n", *ptr);

return 0;
}

Related Links :

C Language




In this section you will get the all information & programs like Armstrong number, pascals triangle, even odd number, Fibonacci's series & many more logical program under one roof....

Related Links :


If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE


More Useful Topics...

 

History Of C..

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.

My 1st Program...


#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t*******Pankaj *******\n");
printf ("\t\t\t********************************\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}

Next Step...


#include
#include

void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");

printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");

getch ();

}

Hits!!!