void reverseWords(string &s) {
     reverse(s.begin(), s.end());
     int prev=-1;
     while(s.find(' ', prev+1)!=string::npos) {
         int curr = s.find(' ', prev+1);
         reverseWords(s, prev+1, curr-1);
         prev=curr;
     }
     if(prev!=s.length()-1) reverseWords(s, prev+1, s.length()-1);
 }
Beispiel #2
0
int main() {
    char str1[] = "    hello  world        ds   "; 
    reverseWords(str1);
    printf("%s$\n",str1);

    char str2[] = " 1";
 
    reverseWords(str2);
    printf("%s$\n",str2);
    return 0;
}
Beispiel #3
0
/* Driver function to test above functions */
int main() {
  char s[] = "     0123456789 The quick    brown fox jumps over the lazy dog.";
  char *temp = s;
  reverseWords(s);
  printf("%s", s);
  return 0;
}
Beispiel #4
0
int main(int argc, char **argv)
{
	char s1[] = "the sky is blue";
	char s2[] = "     the    sky is    blue      ";
	char s3[] = " ";
	char s4[] = "pz! .xwy.,cga. vua frjrmcjf, xxw'izz vgthvpfhl sldudifok wvls orujxroi w. oo c?ymxlptr ff'?rh bsjjoyjwvx tj pqv.zlq ,jlu',j dg izq.fo wtvwqn teual jnsv.a .oclyrvg tkgag'a' !wsz?sclc pvhl.ypq vyin cn?z,kxg , u l?l glr zf'hew l'wmi .nlvgr u zfwzra? ot!emgg. rg,'.d.kp fn vat ba.myfqxe znzdrhh xjeubr taztndst v nep?bs .,pwin. e pi";
	reverseWords(s1);
	reverseWords(s2);
	reverseWords(s3);
	reverseWords(s4);
	printf("'%s'\n", s1);
	printf("'%s'\n", s2);
	printf("'%s'\n", s3);
	printf("'%s'\n", s4);
	return 0;
}
Beispiel #5
0
int main(int argc, char **argv) {
	char *s=(char*)malloc(50*sizeof(char));
	gets(s);
	reverseWords(s);
	printf("%s\n",s);
	return 0;
}
Beispiel #6
0
int main(int argc, const char* argv[])
{
	string s = " 1";
	reverseWords(s);
	cout << s << endl;
	return 0;
}
Beispiel #7
0
int main() {
  char str[] = "I am a student.";
  reverseWords(str);
  std::cout << str << std::endl;

  return 0;
}
int main(int argc, char** argv){
	char s[100] = " The sky is blue ";
	printf("raw string: %s\n", s);
	reverseWords(s);
	printf("reverse string: \"%s\"\n", s);

	return 0;
}
    void reverseWords(string &s) {
        int len = s.size();
        if (len == 0) {
            return;
        }
        int start = 0;
        int end = 0;
        
        // trim leading or trailing whitespace and the inner white space set to one
        for (int i=0; i<len; i++) {
            while (i < len && s[i] == ' ') {
                i++;
            }
            while (i < len && s[i] != ' ') {
                s[start++] = s[i++];
            }
            if (i < len) {
              s[start++] = ' ';
            }
        }

        if (s[start-1] == ' ') {
            start --;
        }
        len = start;
        if (len <= 0) {
            s = "";
            return;
        }
        s = s.substr(0, len);
        
        
        
        for (int i=0; i<len; i++) {
            start = i;
            // find the next word
            while (i < len && s[i] != ' ') {
                i ++;
            }
            end = i - 1;
            reverseWords(s, start, end);
        }
        
        reverseWords(s, 0, len-1);
    }
Beispiel #10
0
int main()
{
  char s[] = "i like this program very much";
  char *temp = s;
  reverseWords(s);
  printf("%s", s);
  getchar();
  return 0;
}
Beispiel #11
0
int main() {
    char str[100];
    while (gets(str)) {
        printf("'%s'\n", str);
        reverseWords(str);
        printf("'%s'\n\n", str);
    }
    return 0;
}
Beispiel #12
0
int main(int argc, char const *argv[])
{
	char *p = "Let's take LeetCode contest";
	char *q = reverseWords(p);
	while(*q++ != '\0') {
		printf("%c\n", *q);
	}
	return 0;
}
int main() {
    
    std::string input("Hello There   ");
    
    Trim(input);
    std::cout << "Trimmed: " << input << std::endl;
    reverseSentence(input);
    std::cout << "Reversed Sentence:" << input << std::endl;
    reverseWords(input);
    std::cout << "REversed Words: " << input << std::endl;
}
Beispiel #14
0
int main(void) {
    char string[] = {' ',' ',' ',' ',' ',
        'b','o','y','c','o','t','t',' ',' ','b','e','e',' ',
        'I',' ','l','i','k','e',' ',' ','s','o',' ','b','e','a','u','t','i','f','u','l',' ',
        't','h','e',' ','s','k','y',' ',' ','i','s',' ',' ',' ','b','l','u','e',
        ' ',' ',' ',' ','\0' };
    char *s = string;
    printf("origin string: |%s|\n", s);
    reverseWords(s);
    printf("after reverse: |%s|\n", s);
    return 0;
}
Beispiel #15
0
TEST(ReverseWordsInAStr, Test5) {

	std::string output;
	testing::internal::CaptureStdout();

	std::string s = "a";
	s = reverseWords(s);

	output = testing::internal::GetCapturedStdout();

	EXPECT_EQ("a", s);
}
Beispiel #16
0
TEST(ReverseWordsInAStr, Test2) {

	std::string output;
	testing::internal::CaptureStdout();

	std::string s = "the sky is blue";
	s = reverseWords(s);

	output = testing::internal::GetCapturedStdout();

	EXPECT_EQ("blue is sky the", s);
}
Beispiel #17
0
TEST(ReverseWordsInAStr, Test4) {

	std::string output;
	testing::internal::CaptureStdout();

	std::string s = "  hello world!  ";
	s = reverseWords(s);

	output = testing::internal::GetCapturedStdout();

	EXPECT_EQ("world! hello", s);
}
int main(int argc, char const *argv[])
{
	/* reverses the individual words */
	printf("Enter the string\n");
	char str[100];
	// scanf doesnt accept space ?????
	//scanf("%s", str);
	// char str[] = {"Enter the string"};
	gets(str);

	reverseWords(str);
	printf("\n");
	return 1;
}
Beispiel #19
0
int main()
{
	char*s = (char*)malloc(100*sizeof(char));
	char*str = "  the   sky     is blue!";
	//char*str = "   a   b  c d   e  ";
	//char*str = "hello world!";
	int len = strlen(str);
	int i;
	for (i = 0; i < len; i++)
	{
		s[i] = str[i];
	}
	s[len] = '\0';
	reverseWords(s);
	len = strlen(s);
	printf("%s===%d\n", s, len);
	return 0;
}
/* Driver function to test above functions */
int main()
{
  int t;
  
  scanf("%d",&t);
  getchar();
// printf("%d\n",t);
while(t--){
//char s[s] = "i like this program very much";
//printf("%d",t);
char s[1005];
scanf ("%[^\n]%*c",s);
reverseWords(s);
printf("%s\n", s);
}
//getchar();
return 0;
}
Beispiel #21
0
int main(int argc, char *argv[])
{
  switch(toInt(argv[argc-1]))
    {
    case 1: 
      printf("%i\n", checkInt(argv[1]));
      break;
    case 2: 
      printf("%f\n", checkFl(argv[1]));
      break;
    case 3: 
      intParse(argv[1]);
      break;
    case 4: 
      printf("%s\n", toUpper(argv[1]));
      break;
    case 5: 
      printf("%s\n", toLower(argv[1]));
      break;
    case 6: 
      reverseString(argv[1]);
      break;
    case 7: 
      isPalindrome(argv[1]);
      break;
    case 8: 
      reverseWords(argv[1]);
      break;
    case 9: 
      subsetCheck(argv[1], argv[2]);
      break;
    case 10: 
      partialCopy(argv[1], argv[2]);
      break;
    default:
      printf("Error: Your selection did not match any of the available.\n");
      break;
    }
  return 0;
}
int main ( void ){


  char *myString = malloc(sizeof(char) * MAX_LINE);

  printf("\nPlease enter a string: ");
  fgets(myString, MAX_LINE - 1, stdin);
  myString = removeNewline(myString);

  printf("\nReverse the following string:\n%s\n", myString);

  myString = reverseWords(myString);


  printf("The string with the words reversed is:\n%s\n", myString);

  free(myString);

  printf("\n\n\n");

  return 0;
}
Beispiel #23
0
int main(void){
	char s[100];
	gets(s);
	reverseWords(s)	;
	puts(s);
} 
Beispiel #24
0
int printWords(int iochar, int argcNext, FILE *fileNext, int extraCredit, FILE *fileOut){ //Does all the printing to screen

char array[BUF_SIZE + 1], blank[BUF_SIZE + 1]; //I chose this value for max charcters because
int c = 0, d = 0;			       //the standard number of charcters
int i=0, j=0;				       //per line is 80 so I set it to 100 for some wiggle
char *tmp = array;
int count=0;		//counts characters in line so array knows when to stop printing
int haschar=0;
int prevchar;
int checkedReturn=0;
int maxlineReached=0;	//indicates whether a huge line was entered
int n;			// to check for a no new line at end of file

//Check if file is null
if(iochar == EOF){
    fprintf(stderr,"File is Empty\n");
    return 2;
}

do{
    if(iochar == '\n'){
        if(haschar==1){
            while (*(array+c) != '\0') { 		//while loop to remove spaces and tabs between words
                char *ptr = NULL;
                if(c==0){
                    for(ptr = array; *ptr == ' '; ptr++) c++; // removes leading spaces and tabs
                }
                if (*(array+c) == ' ' ) {		// if statements removes all extra spaces between words
                    int tmp2 = c + 1;
                    if (*(array+tmp2) != '\0') {
                        while ((*(array+tmp2) == ' ') && *(array+tmp2) != '\0') {
                            if (*(array+tmp2) == ' ') {
                                c++;
                            }  
                            tmp2++;
                        }
                    }
                }
                *(blank+d) = *(array+c);	//replacing old array with extra spaces with compacted array
                c++;
                d++;
		prevchar=*(array+c);	//stores current character for next loop
            }
	    if(*(blank+(d-1)) == ' '){	//if there is a space at end of line then delete
		d--;  
	    }	
            *(blank+d) = '\0';
            c=0;
            d=0;		
            reverseWords(blank);
	    if(extraCredit==1){
	        fprintf(fileOut,"%s\n",blank);
	    }
	    else{
                printf("%s\n",blank);
	    }
        }
        for(i=count+2; i>=0; i--){      //resets array to null
           *(array+i)= 0;
        }
	for(i=count+2; i>=0; i--){      //resets blank to null
                *(blank+i)= '\0';
        } 
        count=0;
        haschar=0;
        j=0;
	prevchar=0;
    }
    else{
        if(iochar == '\t')		//changes all tabs to a space
	    iochar=' ';
	if(iochar != ' ') //checks if there is a non whitespace character in line.
	    haschar=1;
	if(j==98){
	    fprintf(stderr,"Max number of charcters per line has been reached\n");
	    checkedReturn=3;
	    for(i=count+1; i>=0; i--){      //resets array to null
                *(array+i)= 0;
            }   
	    maxlineReached=1;
	    count=0;
            haschar=0;
            j=0;
	    prevchar=0;	
	}
	else{
            *(array+(j++))= iochar;
            count++;
	}

    }
    if(argcNext >= 2){			//if file getc
    	if(maxlineReached==1){
	    do{
                iochar=getc(fileNext);
	    }
	    while(iochar != '\n');
	    maxlineReached=0;	
	}
	iochar=getc(fileNext);
    }
    else{				// if standard out getc
    	if(maxlineReached == 1){
	    do{
                iochar=getc(stdin);
	    }
	    while(iochar != '\n');
	    maxlineReached=0;
	}
	iochar=getc(stdin);
    }
    if(iochar==EOF && n==0 && prevchar != '\n'){ //if no new line then inserts one
    	iochar= '\n';
	n++;
    }
    
}
while(iochar != EOF);
return checkedReturn;
}
Beispiel #25
0
int main(){
    char str2[100] = " a  b";
    reverseWords(str2);
    puts(str2);
}
Beispiel #26
0
int main() {
    assert(reverseWords("This is a test") == "test a is This");
}
int main(int argc, const char * argv[]) {
    char s[] = "Hello World";
    reverseWords(s, 0, strlen(s)-1);
    printf("%s\n", s);
    return 0;
}