Featured Post

Variable Sized Arrays Discussions | C++ | HackerRank Variable Length Arrays in C and C++ - GeeksforGeeks

Variable Sized Arrays Variable Sized Arrays Discussions | C++ | HackerRank Consider an  -element array,  , where each index   in the array c...

Saturday, April 3, 2021

Printing Tokens Discussions | C | HackerRank Hacker Rank Solutions: Printing Tokens






Given a sentence, , print each word of the sentence in a new line.

Input Format

The first and only line contains a sentence, .

Constraints

Output Format

Print each word of the sentence in a new line.

Sample Input 0

This is C 

Sample Output 0

This
is
C

Explanation 0

In the given string, there are three words ["This", "is", "C"]. We have to print each of these words in a new line.

Sample Input 1

Learning C is fun

Sample Output 1

Learning
C
is
fun

Sample Input 2

How is that

Sample Output 2

How
is
that


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
 {
    char *s;
    s = malloc(1024 * sizeof(char));
    scanf("%[^\n]", s);
    s = (realloc(s, strlen(s) + 1));
    int l =strlen(s);
    //Write your logic to print the tokens of the sentence here.
    
    for(int i=0;i<l;i++)
     {  if(s[i] == ' ')
         {   printf("\n");
         }
        else
         printf("%c",s[i]);
    }
    return 0;
} 




Related searches

Array Reversal Discussions | C | HackerRank Array Reversal in C - Hacker Rank Solution

Array Reversal Discussions | C | HackerRank

Array Reversal in C - Hacker Rank Solution




 Array Reversal 

Given an array, of size , reverse it. 

Example: If array, , after reversing it, the array should be, .

 Input Format The first line contains an integer, , 

denoting the size of the array. 

The next line contains spaceseparated integers denoting the elements of the array. 

Constraints , where is the element of the array. 

Output Format The output is handled by the code given in the editor, which would print the array.

 Sample Input 0

 6 

16 13 7 2 1 12 

Sample Output 0 

12 1 2 7 13 16 

Explanation 0 

Given array, = . 

After reversing the array, = 

Sample Input 1 

1 13 15 20 12 13 2 

Sample Output 1 

2 13 12 20 15 13 1

 Sample Input 2

 8 

15 5 16 15 17 11 5 11

 Sample Output 2

 11 5 11 17 15 16 5 15




#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num, *arr,j, i,*temp;
    scanf("%d", &num);
    arr = (int*) malloc(num * sizeof(int));
    
    for(i = 0; i < num; i++) {
        scanf("%d", arr + i);
    }

temp=(int*)malloc(num*sizeof(int));

    /* Write the logic to reverse the array. */
    for(i=num-1,j=0;i>=0;i--,j++)
    {      
      *(temp+j)= *(arr+i);
      
     
    }
    
    
    for(i = 0; i < num; i++)
        printf("%d ", *(temp + i));
    return 0;
}






Friday, April 2, 2021

1D Arrays in C step by step hackerrank solution C Solution For HackerRank Problem: 1D Arrays in C 1D Arrays in C | HackerRank

1D Arrays in C

1D Arrays in C step by step hackerrank solution  C Solution For HackerRank Problem: 1D Arrays in C      1D Arrays in C | HackerRank

 An array is a container object that holds a fixed number of values of a single type. 

To create an array in C, we can do int arr[n];. 

Here, arr, is a variable array which holds up to integers. 


The above array is a static array that has memory allocated at compile time. 

A dynamic array can be created in C, using the malloc function and the memory is allocated on the heap at runtime. 

To create an integer array, of size , int *arr = (int*)malloc(n * sizeof(int)) , where points to the base address of the array. 

In this challenge, you have to create an array of size dynamically, input the elements of the array, 

sum them and print the sum of the elements in a new line.

 Input Format The first line contains an integer, . 

The next line contains space-separated integers.

Output Format Print in a single line the sum of the integers in the array. 

Sample Input 0 

16 13 7 2 1 12

 Sample Output 0 

51 

Sample Input 1

 7 

1 13 15 20 12 13 2

 Sample Output 1

 76



 #include <stdio.h>

#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int n, sum=0;
    scanf("%d",&n);
      
    int *arr=(int*)malloc(n*sizeof(int));
    for(int i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }
    for(int i=0;i<n;i++)
    {
        sum+=arr[i];
    }
    
    printf("%d",sum);
    return 0;
}












Arrays Introduction Discussions | C++ | HackerRank Arrays Introduction in C++ - Hacker Rank Solution

Arrays  C++ Introduction HacakerRank

An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier. 

Declaration: int arr[10]; //Declares an array named arr of size 10, i.e; you can store 10 integers.


 Accessing elements of an array: Indexing in arrays starts from 0.

So the first element is stored at arr[0],the second element at arr[1]...arr[9] 

You'll be given an array of integers and you have to print the integers in the reverse order. 

Input Format The first line of the input contains ,where is the number of integers.

The next line contains integers separated by a space.

 Constraints , where is the integer in the array

. Output Format Print the integers of the array in the reverse order in a single line separated by a space. 

Sample Input 

4 1 4 3 2


 Sample Output 

2 3 4 1


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 
    int n,i;
    cin>>n;
    int arr[n];
   // cout<<endl;
    for(i=0;i<n;i++)
    {
        cin>>arr[i];
    }
   // cout<<endl;
    
    for (i=n-1  ; i>=0; i--)
     {
    cout<<arr[i]<<" ";
     }
    
    return 0;
}














Pointer Pointer Discussions | C++ | HackerRank Pointer Discussions | C++ | HackerRank

 

Pointer  

Pointer Discussions | C++ | HackerRank 

A pointer in C

A pointer in C is a way to share a memory address among different contexts (primarily functions).

 They are primarily used whenever a function needs to modify the content of a variable, of which it doesn't have ownership.

 In order to access the memory address of a variable, , 

we need to prepend it with sign. E.g., &val returns the memory address of . 

This memory address is assigned to a pointer and can be shared among various functions. E.g. will assign the memory address of to pointer . 

To access the content of the memory to which the pointer points, prepend it with a * . 

For example, *p will return the value reflected by and any modification to it will be reflected at the source ( ).

 void increment(int *v) { (*v)++; } 

int main() 

{ int a; scanf("%d", &a); 

increment(&a);

 printf("%d", a);

 return 0; 


You have to complete the function void update(int *a,int *b), which reads two integers as argument, and sets with the sum of them, and with the absolute difference of them. 

Input Format Input will contain two integers, and , separated by a newline. 

Output Format You have to print the updated value of and , on two different lines. 

P.S.: Input/ouput will be automatically handled. 

You only have to complete the void update(int *a,int *b) function. 

Sample Input 

4 5 

Sample Output

9 1


Explanation: 

4+5=9

4-5= 1


#include <stdio.h>

void update(int *a,int *b) {
    // Complete this function 
    int temp;
    temp=*a; 
    *a+=*b;
    *b=temp-*b;
    if(*b<0) 
    *b= (-1* (*b));
}

int main() {
    int a, b;
    int *pa = &a, *pb = &b;
    
    scanf("%d %d", &a, &b);
    update(pa, pb);
    printf("%d\n%d", a, b);

    return 0;
} 









https://www.blogger.com/blog/posts/1819317331536741523