Posts

Showing posts with the label Skillrack

Printing X Pattern-Skillrack

Image
C++ Solution #include <iostream> using namespace std; int main(){     int n, i, j, len, k=1, n1;     cin>>n;     len=2*n-1;     n1=n;     for(i=0;i<len;i++)     {         for(j=0;j<len;j++)         {             if(i<n-1)             {                if(j==i||j==len-1-i)                {                    cout<<k;                }                else                cout<<"*";             }             else if(i==n-1)             {                 if(j==n-1)                 {                     cout<<k;                 }                 else                 cout<<"*";             }             else             {                 if(j==n1-2)                 {                     cout<<k;                     n1--;           

Reverse Middle Words in a String

You are given string S as a input. You has to reverse all the words in the input except first and last word. INPUT "hello how are you" OUTPUT hello woh era you JAVA Solution import java.util.*; public class Hello {     public static void main(String[] args) {         Scanner s = new Scanner(System.in);         String str = s.nextLine();         String str1[] = str.split(" ");         int i, len = str1.length, len1 = len-1;         for(i=0;i<len;i++)         {             if(i!=0&&i!=len1)             {                 StringBuilder sb = new StringBuilder(str1[i]);                 System.out.print(sb.reverse().toString()+" ");             }             else             System.out.print(str1[i]+" ");         }     } } If anyone come up with better solution or having queries leave it in comment. Keep Programming....!!!