• 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

Check a String is SUBSEQUENCE of another String Find Minimum length for that ( DNA Matching )

Find the element that appears once others appears thrice

LeetCode: Container With Most Water

Python List

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

Password Predictor

Sort an array according to the order defined by another array

Rectangular chocolate bar Create at least one piece which consists of exactly nTiles tiles

Find two non repeating elements in an array of repeating elements

Python Array String

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

Find min element in Sorted Rotated Array (With Duplicates)

Leetcode: Merge Intervals

Templates in C++

Cisco Hiring Event 21st – 22nd Feb 2015

Minimum insertions to form a palindrome

FizzBuzz Solution C C++

Find Pythagorean Triplets in an array in O(N)

Reversal of LinkedList

Amazon Interview On-Campus For Internship – 1

simple sql injection

SAP Off Campus Hiring_ March 2015 Verbal Skills

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

Count Possible Decodings of a given Digit Sequence

Implement a generic binary search algorithm for Integer Double String etc

Fibonacci Hashing & Fastest Hashtable

Number of Islands BFS/DFS

Best Java Book | Top Java Programming Book for Beginners

BlueStone E-commerce Interview Experience

Copyright © 2025 · Genesis Framework · WordPress · Log in