Saturday, June 30, 2012

Reverse all string

Q. Write a C program to read a string from user and display string in reverse order.


Example:
User entered string: This is a good blog
Result/output: blog good a is This


Ans.


/*c program for reverse all string*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
 char str[30];
 int i,tmp;
 printf("Enter any string: ");
 gets(str);
 for(i=0; str[i]!='\0'; i++);
 for(i--; i>=0;i--)
 {
  if(str[i-1]==' ' || i==0)
  {
   for(tmp=i; str[tmp]!='\0' && str[tmp]!=' '; tmp++)
      printf("%c",str[tmp]);
  }
  printf(" ");
 }
 getch();
 return 0;
}


/***************Output*****************/


Reverse all string C program output
Screen shot for reverse all string C program



Related Programs:

  1. Reverse all words but not string
  2. Reverse each first character of word & add extra word
  3. Change case of string(Toggle/Title Case)
  4. Display string vertically
  5. Search sub string from main string
  6. Search sub string individual from main string
  7. Position and repetition of character in string

Wednesday, June 27, 2012

Pyramid triangle

Q. Write a C program for following structure:

9
0 1
2 3 4
5 6 7 8
9 0 1 2 3

Ans.

/*c program for triangle*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int i,c,r,x=0,n=4;
 printf("9\n");
 for(r=1; n>=r; r++)
 {
  for(c=0; c<=r; c++,x++)
  {
   if(x<10)
     printf("%d ",x);
   else
   {
     for(i=0; i<=3; i++)
        printf("%d ",i);
     break;
   }
  }
  printf("\n");
 }
 getch();
 return 0;
}

/****************Output******************/
Triangle Structure C program
Screen shot for triangle structure C program

Monday, June 4, 2012

Display string vertically

Q. Write a C program to read a string from user and display it in vertical.
For example: Entered String is : Sample String
After programmed, resulted string is : 
S
a
m
p
l
e


S
t
r
i
n
g


Ans.


/*c program for display a string in vertical*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int i;
 char str[30];
 printf("Enter any string : ");
 gets(str);
 for(i=0; str[i]!='\0'; i++)
   printf("%c\n",str[i]);
 getch();
 return 0;
}


/***************Output******************/


Output of display string vertically C program
Screen shot for display string vertical C program


Related programs:

  1. Reverse all string
  2. Reverse all words but not string
  3. Reverse each first character of word & add extra word
  4. Change title case of string
  5. Search sub string from main string
  6. Search sub string individual from main string
  7. Position and repetition of character in string

Friday, June 1, 2012

Reverse all words but not string

Q. Write a C program to reverse all words but not string.
Let's assume string is: This Is A Good Blog
We wants to do: sihT sI A dooG golB


Ans.


/*c program for reverse all words in string*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
 char str[100];
 int i,temp;
 printf("Enter any string : ");
 gets(str);
 for(i=0; str[i]!=NULL; i++)
 {
  if(str[i+1]==' ' || str[i+1]==NULL)
  {
   for(temp=i; temp>=0 && str[temp]!=' '; temp--)
     printf("%c", str[temp]);
  }
  printf(" ");
 }
 getch();
 return 0;
}


/***************Output********************/
Output of reverse all words but not string C program
Screen shot for reverse words in string c program


Related Programs:

  1. Reverse all string C program
  2. Reverse each first character of word & add extra character
  3. Change case of string(toggle/Title Case) C program
  4. Search sub string from main string C program
  5. Search sub string individual from main string
  6. Display string vertically C program
  7. Position and repetition of character from string C program