• 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

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

Printing intermediate Integers between one element & next element of array

Reverse a Linked List in groups of given size

Client Server C program

Trapping Rain Water

Test Cases for Round Function

Cisco Hiring Event 21st – 22nd Feb 2015

CodeChef Code SGARDEN

Maximum difference between two elements s.t larger element appears after the smaller number

Spanning Tree

Sort an array according to the order defined by another array

Python Array String

strtok()

Walmart Labs Interview Experience

Find the number ABCD such that when multipled by 4 gives DCBA.

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

FizzBuzz Solution C C++

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

Generic Object Oriented Stack with Template

Add Sub Multiply very large number stored as string

Urban Ladder Written Test.

Templates in C++

Find next greater number with same set of digits

Right view of Binary tree

Find and print longest consecutive number sequence in a given sequence in O(n)

DFS (Depth First Search)

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

Flipkart Set 1 On Campus with Answers

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

Length of the longest substring without repeating characters

Copyright © 2025 · Genesis Framework · WordPress · Log in