Solution of your query in c language


Solution of your query in c language

Welcome to ask query 

(q)
date 9/4/08
Hi!
I hope u will be fine.plz help in following program. I need source code.

The question is: A string (e.g.: "I am writing an email") is entered through the keyboard, write a program in C to get its reverse in a column as output i.e.:

email
an
writing
am

Answer:

Code for such program is:

void main()
{
char str[20];
char *ptr=str,*temp;
int i=0,j;
clrscr();
scanf("%[^\n]",str);
while(*ptr){
i++;
ptr++;
}
for(j=0;j
            if(*ptr==' ')
            {
                  temp=ptr;ptr--;temp++;
                  while((*temp!=' ')&&(*temp!='\0')) {
                       printf("%c",*temp);
                        temp++;
                  }
                  printf("\n");
            }
            else
            {
                  ptr--;
            }
}
while(*ptr!=' ') {
printf("%c",*ptr);
ptr++;
}
getch();
}

(q)
I want a C program to check whether a string is a palindrome or not where the string to be checked is passed as command line argument during execution.
(pahal ray
8/25/08

Answer:

#include"string.h"
void main(int counter,char**string)
{
char *rev;
char str[15];
int i,j;
clrscr();
strcpy(str,string[1]);
printf("%s",str);
for(i=strlen(str)-1,j=0;i>=0;i--,j++)
rev[j]=str[i];
rev[j]='\0';
if(strcmp(rev,str))
printf("\nThe string is not a palindrome");
else
printf("\nThe string is a palindrome");
getch();
}

(q)
How to write a c program to display the source code of the program?
(ramya)
8/23/08
Answer: 
If source code is available 
#include"stdio.h"
void main()
{
   char str[70];
   FILE *p;
   clrscr();
 if((p=fopen("mod.c","r"))==NULL)
   {
      printf("\nUnable t open file string.txt");
      exit(1);
   }
   while(fgets(str,70,p)!=NULL)
            puts(str);
   fclose(p);
   getch();
}

(q) Swapping of two number without using third variable

Answer

void main()
{
int a=5,b=10;
clrscr();
//process one
a=b+a;
b=a-b;
a=a-b;
printf("a= %d  b=  %d",a,b);

//process two
a=5;b=10;
a=a+b-(b=a);
printf("\na= %d  b=  %d",a,b);
//process three
a=5;b=10;
a=a^b;
b=a^b;
a=b^a;

printf("\na= %d  b=  %d",a,b);
//process four
a=5;b=10;
a=b-~a-1;
b=a+~b+1;
a=a+~b+1;
printf("\na= %d  b=  %d",a,b);
//process five
a=5,b=10;
a=b+a,b=a-b,a=a-b;
printf("\na= %d  b=  %d",a,b);
getch();
}

(q) How to convert decimal to binary in c program?

Answer:

void main()
{
  long int m,no=0,a=1;
  int n,rem;
  clrscr();
  printf("Enter any decimal number->");
  scanf("%d",&n);
  m=n;
  while(n!=0)
  {
          rem=n%2;
          no=no+rem*a;
          n=n/2;
          a=a*10;
  }
   printf("The value %ld in binary is->",m);
   printf("%ld",no);
  getch();
}

21 comments:

Daljinder said...

in c language we can't scan two consecutive char. for exp. scanf("%c%c",&ch1,&ch2); why?

Anonymous said...

I m not able to run my program after it has been compiled. It shows "not an exe file" what does that mean? I m using dos box to run my turbo c. please help me with my problem

Unknown said...

main()
{
Int i=400*400/400;
if(i==400)
printf(“Filibusters”);
else
printf(“Sea gherkins”);
}

Unknown said...

#include
void main()
{
float a=0.7;
if(a<0.7)
printf("stoned");
else
printf("Avenged");
}

Anonymous said...

#include
main()
{
char i=240;
printf("%d %x ",i,i);
}
output= -16 fffffff0
can any one explain this output especially hexadecimal value..

kalyan said...

#include
#include

int main()
{
int a=8,b=8,c=9,d=10;
printf("%d,%d,%d\n",++a,a++,a++);
printf("%d,%d,%d\n,%d,%d,%d\n",b,c,d,b++,c++,d++);
printf("Hello world!\n");
return 0;
}

Kavi - Ma Vie said...

Given two strings s1 and s2 as parameters to your function, to find the number of occurrences of each character of s2 in s1 and return a string which has the count(in s1) of each character of s2 in order that the characters occur in s2. (Note: The function should be case sensitive.)

reply

Anonymous said...

include

void main()

{

int x = 1, y = 0, z = 5;

int a = x && y && z++;

printf("%d", z);
printf("%d", a);

}

how this z remain 5 ,is that related to priority value
and value a I am getting is 0 i.e Ok.
I have problem why 5 is remain even after increment

Unknown said...

dfd

Unknown said...

void SetColor(int ForgC)
{
WORD wColor;
//We will need this handle to get the current background attribute
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;

//We use csbi for the wAttributes word.
if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
//Mask out all but the background attribute, and add in the forgournd color
wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
SetConsoleTextAttribute(hStdOut, wColor);
}
return;
}

void ClearColor(){
SetColor(15);
}
void ClearConsoleToColors(int ForgC, int BackC)
{
WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
//Get the handle to the current output buffer...
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
//This is used to reset the carat/cursor to the top left.
COORD coord = {0, 0};
//A return value... indicating how many chars were written
// not used but we need to capture this since it will be
// written anyway (passing NULL causes an access violation).
DWORD count;

//This is a structure containing all of the console info
// it is used here to find the size of the console.
CONSOLE_SCREEN_BUFFER_INFO csbi;
//Here we will set the current color
SetConsoleTextAttribute(hStdOut, wColor);
if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
//This fills the buffer with a given character (in this case 32=space).
FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);

FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count );
//This will set our cursor position for the next print statement.
SetConsoleCursorPosition(hStdOut, coord);
}
return;
}
void SetColorAndBackground(int ForgC, int BackC)
{
WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
return;
}
anyone can describe it for me

Unknown said...

about WORD and STD_OUTPUT_HANDLE

The Dravidian Nomad said...
This comment has been removed by the author.
manoj hargude said...

Consider the following piece of code:
if( a > 10 )
if( b < 5 )
b = a * 10;
else
if( a < 5 )
if ( b > 10 )
b = a * b;
else
if( c > 5 && b == 10 )
if( c < 10 )
c = b * a;
else
c = a * c;
The compiler will…
(i) associate the first ‘else’ statement with ‘if( b < 5 )’
(ii) associate the first ‘else’ statement with ‘if( a > 10 )’
(iii) throw an error outright
(iv) none of the above

manoj hargude said...

Consider the following piece of code:
if( a > 10 )
if( b < 5 )
b = a * 10;
else
if( a < 5 )
if ( b > 10 )
b = a * b;
else
if( c > 5 && b == 10 )
if( c < 10 )
c = b * a;
else
c = a * c;
The compiler will…
(i) associate the first ‘else’ statement with ‘if( b < 5 )’
(ii) associate the first ‘else’ statement with ‘if( a > 10 )’
(iii) throw an error outright
(iv) none of the above

Rajat said...

printf("%d",300*300/300);
it's output is 81 bur i don't understand how ??????

Unknown said...

When a charc is entered suppose a and then uu press enter this enter remains in buffer and is given to next charc variable since \n is also a character

Better use scanf("%c %*c %c",&a,&b)

Unknown said...

When a charc is entered suppose a and then uu press enter this enter remains in buffer and is given to next charc variable since \n is also a character

Better use scanf("%c %*c %c",&a,&b)

Unknown said...

Can someone explain me this program:-
#include
#include
#include
void main()
{
clrscr();
char a[20];
int b[123],i;
printf("Enter any string\n");
gets(a);
for(i=0;i<123;i++)
b[i]=0;
for(i=0;i<strlen(a);i++)
b[a[i]]++;
for(i=0;i<123;i++)
if(b[i]!=0)
printf("%c(%d) ",i,b[i]);
getch();
}

YASH said...

printf("%d",a>b?a>c?a:c:b>c?b:c);
suppose a=7
b=8
c=6
Now in the first condition a>b=F a>c=T
now by that logic c should be printed without going to the next instruction.
PLEASE HELP (BEGINNER)

Unknown said...

printf(“\n All prime Numbers between 1 - 500\n\n”);

for(n=1;n<=500;n++)

{

for(i=2;i<n;i++)

{

if(n%i==0)

{

break;

}

}

if(i==n)

{

printf(“%d\t”,i);

}

}



}

Please explain this program...

Unknown said...

Please explain this program