• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Printing each word reverse in string

July 29, 2014 by Dhaval Dave

Below is the code to print each word backward
Example if Input is “This is Sparta”
Out put should be  “sihT si atrapS”


Thanks to Dhaval for suggesting this approach

#include<stdio.h>
#include<string.h>

int main()
{
    int j,i; int lastWhiteSpace = 0; //there were no white spaces
    char str[100]="This is Sparta";
    printf("nString in Reverse Ordern");

    i=0;
    while(str[i]!='')
    {
        if(str[i]==' ')
        {
            for(j=i-1;j>=lastWhiteSpace;j--) printf("%c",str[j]);
//Loop starts from last char and decrements upto the character after the last white space 
            printf(" ");
            lastWhiteSpace = i + 1; 
//the character after this white space 
        }
        i++;
    }

    //later edit
    for(j=i-1;j>=lastWhiteSpace;j--)
        printf("%c",str[j]);
    printf("n");
}

Similar Articles

Filed Under: Adobe Interview Questions, Interview Questions, Microsoft Interview Questions, problem Tagged With: string

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

TicTacToe Game As Asked in Flipkart

Diagonal Traversal of Binary Tree

SAP Off Campus Hiring_ March 2015 Analytical Aptitude

There are N nuts and N bolts, u have to find all the pairs of nuts and bolts in minimum no. of iteration

Find min element in Sorted Rotated Array (With Duplicates)

Find loop in Singly linked list

DFS (Depth First Search)

Spanning Tree

Hackerearth : Counting Subarrays

Subset Sum Problem Dynamic programming

write a c program that given a set a of n numbers and another number x determines whether or not there exist two elements in s whose sum is exactly x

Binary Tree Isomorphic to each other

Client Server C program

Find an index i such that Arr [i] = i in array of n distinct integers sorted in ascending order.

N teams are participating. each team plays twice with all other teams. Some of them will go to the semi final. Find Minimum and Maximum number of matches that a team has to win to qualify for finals ?

Maximum of all subarrays of size k

Skiing on Mountains Matrix

C++ OOPs Part1

SAP Interview Questions

Get K Max and Delete K Max in stream of incoming integers

Find Nearest Minimum number in left side in O(n)

Check if an array has duplicate numbers in O(n) time and O(1) space

How strtok() Works

Python List

Leetcode: Merge Intervals

System Design: Designing a LLD for Hotel Booking

SAP Off Campus Hiring_ March 2015 Sample Questions

Naurki.com Security Breach

CodeChef’ RRCOPY

FizzBuzz Solution C C++

Copyright © 2026 · Genesis Framework · WordPress · Log in