Answer to Question #178368 in C++ for Tayyab

Question #178368

Write a C++ program in which, read a c-string sentence as input from File. Now your task is to reverse each word of sentence

1
Expert's answer
2021-04-05T11:06:14-0400
#include <iostream>
#include <fstream>
#include <cstring>
#include <algorithm>

int main() {
  std::ifstream in("file.txt");
  char buffer[256];
  in.getline(buffer, 256);
  auto length = strlen(buffer);
  for(size_t left = 0, right = 0; right <= length; right++) {
    if(!isalpha(buffer[right]) || right == length) {
      for(size_t i = left, j = right - 1; right && i <= j; i++, j--) {
        std::swap(buffer[i], buffer[j]);
      }
      // skip spaces
      while(right < length && buffer[right] == ' ') right++;
      left = right;
    }
  }
  std::cout << buffer << '\n';
  return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS