• 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

Serialise Deserialise N-ary Tree

Trapping Rain Water

Sort Stack in place

Number of Islands BFS/DFS

Find if a binary tree is height balanced ?

Longest Increasing Subsequence

Code Chef PRGIFT Solution

Flipkart Set 1 On Campus with Answers

Find the smallest window in a string containing all characters of another string

Search element in a matrix with all rows and columns in sorted order

Generate largest number arranging a no. of given non negative integer numbers

Apriori algorithm C Code Data Mining

N Petrol bunks or City arranged in circle. You have Fuel and distance between petrol bunks. Is it possible to find starting point so that we can travel all Petrol Bunks

‘N’ Story Building, with 1,2,3 steps how many ways can a person reach top of building.

Maximum sum contiguous subarray of an Array

Wrong Directions given find minimum moves so that he can reach to the destination

Circular Linked List

Check Binary Tree is Binary Search Tree or not

Find Percentage of Words matching in Two Strings

VMWare Openings

The Magic HackerEarth Nirvana solutions Hiring Challenge

Printing Longest Common Subsequence

Find min element in Sorted Rotated Array (With Duplicates)

Best Java Book | Top Java Programming Book for Beginners

Convert number to words java

Maximum size of square sub matrix with all 1’s in a binary matrix

Sort an array according to the order defined by another array

Skiing on Mountains Matrix

Test Cases for Round Function

simple sql injection

Copyright © 2025 · Genesis Framework · WordPress · Log in