Thread: Need help,im beginner

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    2

    Question Need help,im beginner

    Hello guys,im beginner
    Im trying to reverse some texts and delete spaces.if spaces are more than one
    i mean this :
    hello world ---> hello world
    4 spaces ---> 1 space

    i tryed to reverse the texts successfully like this


    Code:
    void reversed(char str[]);
    int main(int argc, char *argv[])
    {
        char str[100];
        printf("enter text : ");
        gets(str);
      reversed(str);
        return 0;
    }
    void reversed(char str[])
    {
         int i;
         for (i = strlen(str) - 1; i>=0; i--)
         printf("%c",str[i]);
         printf("\n");
      system("PAUSE");    
      return 0;
    }



    but i want to delete the spaces at the same time and dont know how
    Thanks.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Maybe you could check if the character is a space before you print it?

    And don't use gets! FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    2
    Quote Originally Posted by rags_to_riches View Post
    Maybe you could check if the character is a space before you print it?

    And don't use gets! FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com


    i found out how to delete the spaces but i want to reverse and delete at the same time.Im not good with pointers could u give me a solution please?
    thanks a lot

    Code:
    int main()
    {
       char text[100], blank[100];
       int c = 0, d = 0;
     
       printf("Enter some text\n");
       gets(text);
     
       while (text[c] != '\0')
       {
          if (!(text[c] == ' ' && text[c+1] == ' ')) {
            blank[d] = text[c];
            d++;
          }
          c++;
       }
     
       blank[d] = '\0';
     
       printf("Text after removing blanks\n%s\n", blank);
     system("PAUSE");
       return 0;
    }

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    51
    First, follow rag_to_riches advise about gets usage.
    Second, nobody here will give you a complete solution.
    Third, normally, a function should do one thing and do it well. So reversing a string AND removing the spaces at the same time may not be the wisest method. Also, you don't have to be bang on the first time. Divide your problem into smaller increments. Then once you have a working solution, see how you could improve upon.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. beginner c++ need help please
    By mouse666666 in forum C++ Programming
    Replies: 2
    Last Post: 09-27-2010, 10:39 PM
  2. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM
  3. Beginner's help
    By rekiller in forum Game Programming
    Replies: 3
    Last Post: 03-17-2003, 08:06 AM
  4. beginner need help
    By evilangel6 in forum C Programming
    Replies: 10
    Last Post: 08-20-2002, 09:27 AM

Tags for this Thread