Reverse the words in a sentence. The string “have a nice day” should be changed to “day nice a have”.

Reverse the words in a sentence. The string “have a nice day” should be changed to “day nice a have”.

Ques:- Reverse the words in a sentence. The string “have a nice day” should be changed to “day nice a have”.
8 7431

8 Answers on this Question

  1. #include
    #include
    #include

    void reverse(char *a)
    {
    int i, j, size;
    char tmp;

    size = strlen(a);
    j=size-1;

    for(i=0; i<size/2; i++)
    {
    tmp=a[i];
    a[i]=a[j];
    a[j]=tmp;
    j–;
    }

    }

    void reverseAll(char *a)
    {
    int size;

    reverse(a);

    size = strlen(a);
    char *new = (char*)malloc(size+1);

    char *token = strtok(a, " ");
    reverse(token);
    strcpy(new, token);
    printf("%s ", new);
    while(token != NULL)
    {
    reverse(token);
    token = strtok(NULL, " ");
    strcat(new, token);
    }

    }

    int main()
    {
    char a[15]= "have a nice day";

    reverseAll(a);
    printf("%s ", a);
    return 0;
    }

  2. #include
    #include
    using namespace std;
    string reversestr(string s,int i,int j)
    {
    char temp;
    for(i;i<j;i++,j–)
    {
    temp=s[i];
    s[i]=s[j];
    s[j]=temp;
    }
    return s;
    }

    int main()
    {
    string s;
    getline(cin,s);

    int j=0;
    int start=0;
    while(j<=s.length())
    {
    if((int)s[j]==32||j==s.length())
    {
    s=reversestr(s,start,j-1);
    start=j+1;
    }

    j++;
    }
    s= reversestr(s,0,(s.length()-1));
    cout<<s;

    }

  3. public class Main
    {
    public static void main(String[] args) {
    System.out.println(“Hello World”);
    String name =”have a nice day”;
    String[] nameL= name.split(” “);
    System.out.println(nameL[3] + ” ” + nameL[2] + ” ” + nameL[1] + ” ” + nameL[0]);
    }
    }

  4. #include
    #include

    using namespace std;

    int main()
    {
    string str = “This is example of string reverse”;
    cout<<"Original string is :"<<str<> word)
    {
    res = word + ” ” + res;
    }
    cout<<"Reverse string is :"<<res<<endl;
    return 0;
    }

  5. str = “have a nice day”.split()
    new_str = “”
    for word in str:
    new_str = word+ ” ” +new_str
    print(new_str)

  6. $str = explode(” “,”have a nice day”);
    $array_rev = array_reverse($str);
    echo implode(” “,$array_rev);

  7. str=’Have a nice day’
    new_str=str.split(‘ ‘);
    final_str=[]
    for(let i = new_str.length-1;i>=0; i–){
    final_str.push(new_str[i])
    // console.log(i );
    }
    console.log(final_str.join(‘ ‘));

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top