• 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

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 ?

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

Length of the longest substring without repeating characters

Maximum of all subarrays of size k

Spanning Tree

Urban Ladder Written Test.

LeetCode: Container With Most Water

Print Power Set of a Set

Calculate price of parking from parking start end time prices

Sort an array according to the order defined by another array

Print all nodes that are at distance k from a leaf node

Maximum path sum between two leaves

Reversal of LinkedList

How strtok() Works

LeetCode : Word Search

Advanced SQL Injection

Binary Tree in Java

C++ OOPs Part1

Apriori algorithm C Code Data Mining

Skiing on Mountains Matrix

Trapping Rain Water

Binary Tree Isomorphic to each other

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

Puzzle : 100 doors in a row Visit and Toggle the door. What state the door will be after nth pass ?

Linked List V/S Binary Search Tree

strtok()

The Magic HackerEarth Nirvana solutions Hiring Challenge

Find if a binary tree is height balanced ?

Printing Longest Common Subsequence

The greedy coins game Dynamic Programming

Copyright © 2025 · Genesis Framework · WordPress · Log in