Thread: Reverse String

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    9

    Reverse String

    The reverse-problem was not forgotten, and from time
    to time I have tried to find a solution. Now I found
    an answer but for Windows only. - This program run correctly:


    Code:
    //Reverse a sentence by "mh2x", and "strrev" reverse it 
    //than word by word. For Windows only. - Oktober 2018
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    char *ReverseChars(char * pStart, char * pEnd);
    char *ReverseSentence(char * pSentence);
    
    
    int main(void)
    {
      char sentence[] = "They never come back!";
    
    
      ReverseSentence(sentence);
    		
    	//Bad! ;)
      printf("%s\n", strrev(sentence));
    
    
    	return 0;
    }
    
    
    char *ReverseChars(char *pStart, char *pEnd)
    {
    	if (NULL == pStart || NULL == pEnd)
    		return NULL;
    
    
    	char *pString = pStart;
    	
    	while (pEnd > pStart)
    	{
    		char temp = *pStart;
    		*pStart = *pEnd;
    		*pEnd = temp;
    
    
    		pStart++;
    		pEnd--;
    	}
    	return pString;
    }
    
    
    char *ReverseSentence(char *pSentence)
    {
    	if (NULL == pSentence)
    		return NULL;
    
    
    	char *pStart = pSentence;
    	char *pEnd = pSentence;
    
    
    	while (*pEnd)
    	{
    		if (*pEnd == ' ')
    		{
    			//Reverse a word
    			ReverseChars(pStart, pEnd - 1);
    			pStart = pEnd + 1;
    		}
    		pEnd++;
    	}
    	//Revese last word
    	ReverseChars(pStart, pEnd-1);
    	
    	//Reverse the whole sentence
    	ReverseChars(pSentence, pEnd - 1);
    
    
    	return pSentence;
    }
    Reverse String-reverse-sent-jpg

  2. #2
    Registered User
    Join Date
    Sep 2018
    Posts
    9
    This is still a problem:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    char *ReverseChars(char * pStart, char * pEnd);
    char *ReverseSentence(char * pSentence);
    
    
    int main(int argc, char *argv[])
    {
    	if (argc < 2) return 1;
    	char *dateiname = argv[1];
    	FILE *dz = fopen(dateiname, "r");
    	if (dz == NULL) return 1;
    	
    	char sentence[100];
    	char c;
    	while((sentence[] = fgetc(dz)) != EOF)
    	{
    		//sentence[c]; Error!
    	}
    	fclose(dz);	
     
      ReverseSentence(sentence);
    		
    	//Bad! ;)
      printf("%s\n", strrev(sentence));
    
    
    	return 0;
    }
    . . . and so on.

    Where is the thinking error?

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,238
    So please describe the problem.

    If you're getting compile errors post the complete error messages, all of them, exactly as they appear in your development environment.

    By the way do you realize that strrev() is a non-standard function?

  4. #4
    Registered User
    Join Date
    Sep 2018
    Posts
    9
    Quote Originally Posted by jimblumberg View Post
    So please describe the problem.

    If you're getting compile errors post the complete error messages, all of them, exactly as they appear in your development environment.

    By the way do you realize that strrev() is a non-standard function?
    Code:
    int main(int argc, char *argv[])
    {
        if (argc < 2) return 1;
        char *dateiname = argv[1];
        FILE *dz = fopen(dateiname, "r");
        if (dz == NULL) return 1;
        
        char sentence[100];
        char c;
        //Mit "[]" oder ohne - Fehler
        while((sentence[] = fgetc(dz)) != EOF)
        {
            sentence[c];
        }
        fclose(dz);    
     
      ReverseSentence(sentence);
            
        //Bad! ;)
      printf("%s\n", strrev(sentence));
    
    
        return 0;
    }
    The compile errors:
    Reverse String-reverseii2018-10-15_205440-jpg

    If the comment code out than are no errors, but also no output. Look first screenshot above. I understand the message from the compiler but I don't find an answer. - Thanks for Your effort.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    25,962
    Focus on this particular part:
    Code:
    sentence[] = fgetc(dz)
    What are you trying to do here? In particular, in your understanding, what does sentence[] mean in this context?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Sep 2018
    Posts
    9
    The original code is so (this run correctly):

    Code:
    int main(int argc, char *argv[])
    {
        //This program reads a file and prints it word by word.
        if (argc < 2) return 1;
        char *dateiname = argv[1];
        FILE *dz = fopen(dateiname, "r");
        if (dz == NULL) return 1;
        
        char c;
                     
        while((c = fgetc(dz)) != EOF)
        {
             printf("%c", c);
        }
        fclose(dz);    
        
        return 0;
    }
    . . . and I thought I could doing the input in the array "sentence[]". Wrong thought!

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    25,962
    Quote Originally Posted by Kernelpanic
    . . . and I thought I could doing the input in the array "sentence[]". Wrong thought!
    It's not a wrong thought (as in idea), but a wrong implementation. Look into the use of the fgets function instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reverse a string without string.h library
    By antros48 in forum C Programming
    Replies: 6
    Last Post: 09-10-2011, 06:01 PM
  2. [Help] About reverse string
    By kingofdcp in forum C Programming
    Replies: 3
    Last Post: 06-06-2009, 02:53 AM
  3. Reverse a string (without using any string functions?)
    By geetard in forum C Programming
    Replies: 2
    Last Post: 11-15-2006, 07:42 PM
  4. Help! I need to reverse a string!!
    By Josh7289 in forum C++ Programming
    Replies: 32
    Last Post: 12-25-2004, 07:54 AM
  5. reverse string
    By b-ref in forum C Programming
    Replies: 2
    Last Post: 11-25-2001, 09:41 AM

Tags for this Thread