Search
 
SCRIPT & CODE EXAMPLE
 

C

reverse word in string

One Line
public class Solution 
{
    public string ReverseWords(string s) 
      => string.Join(" ",s.Trim().Split(' ').Where(x=>x.Length>0).Reverse());
}

Another Approaches

public class Solution 
{
    public string ReverseWords(string s) 
    {
        var stack = new Stack<string>();
        int i=0 ,j=0;
        while(j <= s.Length)
        {
            if( j == s.Length || s[j] == ' ' )
            {
                var sub = s.Substring(i,j-i);
                if(j-i >= 0 && sub.Length > 0) stack.Push(sub); 
                i=++j;
            }
            else j++;
        }
        return string.Join(" ",stack);
    }
}
Comment

Given a string, reverse each word in the sentence

function wordsReverser(string){
return string.split("").reverse().join("").split(" ").reverse().join(" ")  
}

console.log(wordsReverser('New string, same results.'));
Comment

reverse every word of given sring

// C++ program to reverse individual words in a given
// string using STL list
#include <bits/stdc++.h>
using namespace std;
 
// reverses individual words of a string
void reverseWords(string str)
{
    stack<char> st;
 
    // Traverse given string and push all characters
    // to stack until we see a space.
    for (int i = 0; i < str.length(); ++i) {
        if (str[i] != ' ')
            st.push(str[i]);
 
        // When we see a space, we print contents
        // of stack.
        else {
            while (st.empty() == false) {
                cout << st.top();
                st.pop();
            }
            cout << " ";
        }
    }
 
    // Since there may not be space after
    // last word.
    while (st.empty() == false) {
        cout << st.top();
        st.pop();
    }
}
 
// Driver program to test function
int main()
{
    string str = "Geeks for Geeks";
    reverseWords(str);
    return 0;
}
Comment

Reverse every Word of given String

 
/* 
 *  C Program to Reverse every Word of given String
 */
#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("%[^
]s", str);
 
/* reads into 2d character array */
    for (i = 0;str[i] != ''; i++)
    {
        if (str[i] == ' ')
        {
            str1[k][j]='';
            k++;
            j=0;
        }
        else
        {
            str1[k][j]=str[i];
            j++;
        }
    }
    str1[k][j] = '';
 
/* 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]);
    }
}
Comment

PREVIOUS NEXT
Code Example
C :: which one is faster loop or recursive function? 
C :: converting time in c 
C :: gnuplot rectangle border color 
C :: how to compress a file in c 
C :: c byte vs char 
C :: Array in element from lowest 
C :: bullseye lxc network problem 
C :: algorithm for sorting numbers in ascending order 
C :: c michael 
C :: send array to child process c 
C :: Print fabionci with fork in C 
C :: profile time bash script 
C :: putting character in the begginig and end of sring C 
C :: c printf affichage 
C :: finding average of elements in array using struct in C? 
C :: Word Processor, Spreadsheet and Presentation Software are the examples of 
C :: bucket sort 
C :: c Write a program to reverse an array or string 
C :: how to do add to an integrr in c 
Dart :: flutter appbar remove debug 
Dart :: flutetr stepper color 
Dart :: round container flutter 
Dart :: flutter trigger show off keyboard 
Dart :: flutter array of strings 
Dart :: setting backgroundColor for snack bar does not change background color 
Dart :: flutter replace string 
Dart :: text should come below if space not available row flutter 
Dart :: how to disable windows build flutter 
Dart :: card border radius flutter 
Dart :: dart enum 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =