TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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
Reply
Answers (
8
)
0
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
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
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
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
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
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
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
0
8.4k
2m
Nov 13 2015 12:00 AM
What is the issue on your program?
How to design a keypad in C#?
export label values in excel