Thread: Reversing Words and Array of Pointers

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    3

    Reversing Words and Array of Pointers

    I've been working on a program that will take an array of pointers that contains multiple strings and print those strings out with the words in reverse order.

    For example, if the array is:
    Code:
    char* str[] = {"one two three", "four five six"};
    I want it to print out:
    Code:
    Words before reverse     one two three
                             four five six
    
    Words after reverse      eno owt eerht
                             ruof evif xis
    This is what I've come up with so far. Right now the code will execute, but the output is not as what I described above.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    
    //char* strdup( const char*);
    int words_reverse( char* str, char* words[], int maxwords );
    
    int main(){
    	int i;
    	const char* str[] = {"one two three", "four five six"};
    	char* words[100];
    	const int NB_TESTS = 2;
    
    	printf("Words before reverse \t%s \n\n", str);
    	printf("Words after reverse \t");
    
    	for(i=0; i < NB_TESTS; i++){
    		
    		char* input = (char*)_strdup(str[i]); 
    		words_reverse(input, words, 100);
    	}
    
    	printf("\n\n");
    	system("pause");
    	return 0;
    }
    
    int words_reverse( char* str, char* words[], int maxwords ){
        	
    	int i, j, k; 
    	int start = 0;
    	
    	for (i = 0; i <= strlen(str); i++){
    		if (!isalnum(str[i]))
    		{
    			k = 0;
    			for (j = start; j < i; j++){
    				words[k] = &str[j];
    				k++;
    			}
    			words[k] = '\0';
    			_strrev(words);
    			printf("%s ", words);
    			start = i + 1;
    		}
    	} 
    	return 0; 
    }
    Right now the code will execute, but the output is incorrect. I'm still a novice at C and I've always had difficulty with pointers and arrays. Can anyone help me out please?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... hard way...
    Code:
    	for (i = 0; i <= strlen(str); i++){
    		if (!isalnum(str[i]))
    		{
    			k = 0;
    			for (j = start; j < i; j++){
    				words[k] = &str[j];
    				k++;
    			}
    			words[k] = '\0';
    			_strrev(words);
    			printf("%s ", words);
    			start = i + 1;
    Easy way...
    Code:
    for (i = strlen(str) - 1; i > 0, i--)
      printf("%c",str[i]);

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    Thank you very much CommonTater! All I have to do is adjust the spacing a bit and its complete.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Striker87 View Post
    Thank you very much CommonTater! All I have to do is adjust the spacing a bit and its complete.
    People seem to forget that loops can count down too...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help scanning words into array
    By bezer in forum C Programming
    Replies: 2
    Last Post: 04-07-2010, 12:56 PM
  2. Syntax for constant array of array pointers
    By BMintern in forum C Programming
    Replies: 4
    Last Post: 05-14-2008, 08:21 AM
  3. dynamically creating an array of pointers
    By cs32 in forum C Programming
    Replies: 4
    Last Post: 02-06-2008, 09:42 AM
  4. sorting with pointer of pointers to array
    By dunpealslyr in forum C++ Programming
    Replies: 6
    Last Post: 10-01-2007, 11:26 PM
  5. extracting words from an array of words
    By axon in forum C++ Programming
    Replies: 2
    Last Post: 04-02-2003, 11:21 PM