• 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

Binary Tree in Java

Subset Sum Problem Dynamic programming

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

Coin Collection Dynamic Programming

Amazon Interview Experience – SDE Chennai

Find min element in Sorted Rotated Array (Without Duplicates)

Facebook Interview Question : Interleave List

Given a sorted array and a number x, find the pair in array whose sum is closest to x

Find loop in Singly linked list

Find if two rectangles overlap

Knight Tour Problem (Graph – Breadth First Search)

Count Possible Decodings of a given Digit Sequence

LeetCode: Container With Most Water

Given a string, find the first character which is non-repetitive

Right view of Binary tree

BFS (Breath First Search)

Printing Longest Common Subsequence

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

Minimum insertions to form a palindrome

How strtok() Works

Circular Linked List

Best Java Book | Top Java Programming Book for Beginners

Cisco Hiring Event 21st – 22nd Feb 2015

robot standing at first cell of an M*N matrix. It can move only in two directions, right and down. In how many ways, it can reach to the last cell i.e. (M, N) Code it

LeetCode : Word Search

SAP Off Campus Hiring_ March 2015 Sample Questions

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

Find the element that appears once others appears thrice

Top 10 Interviews Techniqes for Campus Interview in IIT NIT BITS for MTech

Closed Parentheses checker

Copyright © 2025 · Genesis Framework · WordPress · Log in