Reverse Words of String

import 'package:test/test.dart';

String reverseStringWords(String s) {
  String res = "";
  int m = s.length;
  int j = m - 1;
  for (int i = m - 1; i >= 0; i--) {
    if (s[i] == '.') {
      for (int j1 = i + 1; j1 <= j; j1++) {
        res += s[j1];
      }
      res += s[i];
      j = i - 1;
    } else if (i == 0) {
      for (int j1 = i; j1 <= j; j1++) {
        res += s[j1];
      }
    }
  }
  return res;
}

void main() {
  test("reverseStringWords single word returns same word", () {
    expect(reverseStringWords("word"), equals("word"));
  });

  test("reverseStringWords w1.w2 returns w2.w1", () {
    expect(reverseStringWords("w1.w2"), equals("w2.w1"));
  });

  test("reverseStringWords on empty string returns empty string", () {
    expect(reverseStringWords(""), equals(""));
  });

  test("reverseStringWords", () {
    expect(reverseStringWords("abhishek.is.a.good.boy"),
        equals("boy.good.a.is.abhishek"));
  });
}
Algerlogo

Β© Alger 2022

About us

We are a group of programmers helping each other build new things, whether it be writing complex encryption programs, or simple ciphers. Our goal is to work together to document and model beautiful, helpful and interesting algorithms using code. We are an open-source community - anyone can contribute. We check each other's work, communicate and collaborate to solve problems. We strive to be welcoming, respectful, yet make sure that our code follows the latest programming guidelines.