Program Discussion :: Basics
105 / 279
write a program to reverse all the word in given sentence .
Ex. India is a Great Country.
Output: aidni si a taerg yrtnuoc.
Answer:
#include
#include
#include
using namespace std;
void main()
{
int i, j = 0, k = 0, x, len;
char str[100], str1[10][20], temp;
cout str;
/* reads into 2d character array */
for (i = 0;str[i] != '\0'; i++)
{
if (str[i] == ' ')
{
str1[k][j]='\0';
k++;
j=0;
}
else
{
str1[k][j]=str[i];
j++;
}
}
str1[k][j] = '\0';
/* reverses each word of a given string */
for (i = 0;i
Asked In ::
Language:
Manjari
7 Jul, 2017 9:30 AM
#include <stdio.h>
#include <string.h>
void main()
{
int i, j = 0, k = 0, x, len;
char str[100], str1[10][20], temp;
printf("enter the string :");
scanf("%[^\n]s", str);
/* reads into 2d character array */
for (i = 0;str[i] != '\0'; i++)
{
if (str[i] == ' ')
{
str1[k][j]='\0';
k++;
j=0;
}
else
{
str1[k][j]=str[i];
j++;
}
}
str1[k][j] = '\0';
/* reverses each word of a given string */
for (i = 0;i <= k;i++)
{
len = strlen(str1[i]);
for (j = 0, x = len - 1;j < x;j++,x--)
{
temp = str1[i][j];
str1[i][j] = str1[i][x];
str1[i][x] = temp;
}
}
for (i = 0;i <= k;i++)
{
printf("%s ", str1[i]);
}
}
Language:
Aarkin
7 Jul, 2017 9:30 AM
#include <stdio.h>
#include <string.h>
#include<iostream>
using namespace std;
void main()
{
int i, j = 0, k = 0, x, len;
char str[100], str1[10][20], temp;
cout<<"enter the string :";
cin>> str;
/* reads into 2d character array */
for (i = 0;str[i] != '\0'; i++)
{
if (str[i] == ' ')
{
str1[k][j]='\0';
k++;
j=0;
}
else
{
str1[k][j]=str[i];
j++;
}
}
str1[k][j] = '\0';
/* reverses each word of a given string */
for (i = 0;i <= k;i++)
{
len = strlen(str1[i]);
for (j = 0, x = len - 1;j < x;j++,x--)
{
temp = str1[i][j];
str1[i][j] = str1[i][x];
str1[i][x] = temp;
}
}
for (i = 0;i <= k;i++)
{
cout<<str1[i];
}
}
Language:
Krishna
5 Dec, 2017 8:29 PM
import java.util.*;
public class Reverse {
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String str="";
str=sc.nextLine();
String str2 ="";
String ch[]=str.split(" ");
for(int i=0;i<ch.length;i )
{
String word=ch[i];
String reverse1="";
for(int j=word.length()-1;j>=0;j--)
{
reverse1 =word.charAt(j);
}
str2 =reverse1 " ";
}
System.out.println(str2);
}
Language:
Ram Prakash
31 Mar, 2018 8:13 PM
#include <stdio.h>
#include <string.h>
int main()
{
char hel[] = "India is a Great Country";
char *anj = hel;
reverseWord(anj);
printf("%s",hel);
return 0;
}
void reverseWord (char *word)
{
char *word_temp = word;
char *temp = word;
while (*temp)
{
temp++;
if (*temp == '\0')
{
reverse(word_temp, temp-1);
}
if (*temp == ' ')
{
reverse(word_temp, temp-1);
word_temp = temp+1;
}
}
}
reverse(char *begin, char *end)
{
char c;
while (begin < end)
{
c = *begin;
*begin++ = *end;
*end-- = c;
}
}