Reverse a string (without using any string functions?)

This is a discussion on Reverse a string (without using any string functions?) within the C Programming forums, part of the General Programming Boards category; Hi Slowly learning C and currently making a program that takes a string (sentence) and does these following steps: 1)Print ...

  1. #1
    blah!
    Join Date
    Nov 2005
    Posts
    3

    Reverse a string (without using any string functions?)

    Hi

    Slowly learning C and currently making a program that takes a string (sentence) and does these following steps:

    1)Print total of characters
    2)Print total of e's
    3)Reverse the words
    4)Reverse the words and letters

    Steps 1 and 2 are done; having a little halt with 3 and 4.

    Let's take "Hello everyone" as the string.


    CODE:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int i, totalCharacter, totalE;
        char txt[301];
      
      // Ask user to enter his/her sentence
      printf("Type your sentence: ");
      gets(txt);
        
      // 1) Total characters in the sentence (spaces included)
      for(totalCharacter = 0; totalCharacter < txt[totalCharacter]; totalCharacter++);
          printf("Your sentence has %i characteres (including any spaces).\n\n", totalCharacter);
          
      // 2) Total e's in the sentence
      totalE = 0;
      for(i = 0; i < totalCharacter; i++){
      if(txt[i] == 'e')
      totalE++;}
      printf("There are %i 'e' in your sentence.\n\n", totalE);
      
      // 3) Sentence with words reversed (INCOMPLETED!)
      for(i = 0; i < totalCharacter; i++)
      
      
      
      // 4) Sentence with letters AND words reversed (INCOMPLETED!)
      for(i = 0; i < totalCharacter; i++)
    
      
      system("PAUSE");	
      return 0;
    }
    So, step 1 gives: 14 characters
    #2: 4 e's
    #3: everyone hello
    #4: enoyreve olleh

    My question is: can I print out steps 3 and 4 without using any string functions? I'd like to use printf and 'for' loop only, if possible.

    e.g. Something like this algorithm (:

    1) Set up a counter to find the string length.
    2) Find the last character and print it.
    3) Decrement the counter and print the next successive characters.


    Thanks in advance.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by geetard
    My question is: can I print out steps 3 and 4 without using any string functions? I'd like to use printf and 'for' loop only, if possible.
    Don't use gets -- visit the FAQ for that.

    Traverse the string to find the terminating null, then print back to front: one loop to advance to the end, another to print backwards.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    // 1) Total characters in the sentence (spaces included)
      for(totalCharacter = 0; totalCharacter < txt[totalCharacter]; totalCharacter++);
          printf("Your sentence has %i characteres (including any spaces).\n\n", totalCharacter);
    Your comparison is incorrect. You should be checking to see that txt[ totalCharacter ] is not '\0', not doing a less than test. Also, since you have a ; at the end of your for loop, you shouldn't be indenting printf, as it is not part of the loop.

    This type of string reversal is asked all the time. Search the forum for more.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. How properly inherit from template?
    By 6tr6tr in forum C++ Programming
    Replies: 118
    Last Post: 04-25-2008, 04:30 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21