Dinesh B

Dinesh B

  • NA
  • 117
  • 41k

Reverse the individual words in a sentence

Nov 12 2015 10:57 PM
By using for loop and if clause we can reverse the individual words in a sentence.
This program is in c#
Class Program
{
static void main(string[] args)
{
Int count=0,i,j;
Console.writeline("Enter the string");
String s = convert.Tostring(console.readline());
for(i=0;i<s.length;i++)
{
if(s[i]!=' ')
{
Count ++;
}
else
{
for(j =i-1;j>=i-count;j--)
{
console.write(s[j]);
}
Count =0;
Console.write(" ");
}}
Console.readline();
}}}
Note : while entering the words give a space after the last word.
The o/p will be correctly display

Answers (8)

0
chaitanya pathak

chaitanya pathak

  • 0
  • 50
  • 1.4k
Nov 16 2015 7:22 AM
Hi Please find the following C# code.
 
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string strInput = "";
string strResult="";
Console.Write("Enter A String : ");
strInput = Console.ReadLine();
for (int i = strInput.Length-1; i >= 0; i--)
{
strResult+=strInput[i].ToString();
}
Console.WriteLine(strResult);
Console.ReadLine();
}
}
}
 
 
 
 
 
0
Raja T

Raja T

  • 328
  • 5.4k
  • 110k
Nov 13 2015 4:43 AM
Hi Dinesh, Please checkout below code
 
static void Main(string[] args)
{
string revese = "";
Console.Write("Enter A String : ");
string s = Console.ReadLine();
foreach (char ch in s.ToArray().Reverse())
revese = revese + (ch);
string[] splitRev = revese.Split(' ');
for (int k = splitRev.Length-1; k < splitRev.Length; k--)
{
if (k != -1)
{
Console.Write(splitRev[k]+" ");
}
else
{
break;
}
}
Console.Read();
}
 
 I think  above code is your requirement.Francis comments is exactly logic correct. I am just adding few lines code for your requirement. 
0
Francis

Francis

  • 0
  • 8.4k
  • 2m
Nov 13 2015 4:06 AM
Hope this will solve your problem:
 
Console.Write("Enter A String : ");
string strInput = Console.ReadLine();
string[] arrStr = strInput.Split(' ');
foreach (string str in arrStr)
{
foreach (char ch in str.ToArray().Reverse())
Console.Write(ch);
Console.Write(' ');
}
 
Check and let me know! 
 
0
Dinesh B

Dinesh B

  • 0
  • 117
  • 41k
Nov 13 2015 2:28 AM
Thanks for Mr.Raja T and Mr.Francis.the program which you wrote is for reverese the string.my program is about reverse the individual word in a sentence.
Example : Hi how are you
Output. : iH woh era uoy.
0
Francis

Francis

  • 0
  • 8.4k
  • 2m
Nov 13 2015 1:42 AM
I have write my own version of solution for your problem:
 
class Program
{
static void Main(string[] args)
{
Console.Write("Enter A String : ");
string s = Console.ReadLine();
foreach(char ch in s.ToArray().Reverse())
   Console.Write(ch);
}
 
Hope this helps! 
0
Raja T

Raja T

  • 328
  • 5.4k
  • 110k
Nov 13 2015 12:48 AM
Hi Dinesh, 
 
class Program
{
static void Main(string[] args)
{
string Str, Revstr = ""; //for storing string value
int Length; //for counting lenght of given string
Console.Write("Enter A String : "); //showing message to user
Str = Console.ReadLine(); //to allow user to input string
Length = Str.Length - 1; //storing the length of given string
while (Length >= 0) //loops the given string length
{
Revstr = Revstr + Str[Length]; //performimg a reverse string according to length of given string
Length--;
}
Console.WriteLine("Reverse String Is {0}", Revstr); // displaying output to user
Console.ReadLine(); // to keep window
}
}
 
more details checkout bellow links
 
http://www.c-sharpcorner.com/UploadFile/0c1bb2/reverse-a-given-string-without-using-function-in-Asp-Net-C-Sharp/
 
http://www.sanfoundry.com/csharp-program-reverse-string-without-reverse-function/ 
0
Dinesh B

Dinesh B

  • 0
  • 117
  • 41k
Nov 13 2015 12:08 AM
If i don't give space after the last word in the sentence,the last word won't reverse.i didn't able to solve this..
0
Francis

Francis

  • 0
  • 8.4k
  • 2m
Nov 13 2015 12:00 AM
What is the issue on your program?