wow no error....except this

This is a discussion on wow no error....except this within the C Programming forums, part of the General Programming Boards category; So code compiles fine, but it doesn't print anything , what am i doing wrong. I haven't delved into strings ...

  1. #1
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    wow no error....except this

    So code compiles fine, but it doesn't print anything, what am i doing wrong. I haven't delved into strings yet.

    Thank you
    Code:
    /* backWORDS.c prints a user inputed string backwards */
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    int main(void)
    
    {
       int count;
       char word[BUFSIZ];
    
       /* caputure word */
    
       printf("Enter a word\n");
       gets(word);
    
       /* print word backwards */
    
       for (count = '\0' /*strlen(word) -1*/ ; count >= word[0]; count--){
          printf("%c", word[count]);
    
          }
    
    
    
    
      getchar();
      return (0);
    
    }
    Last edited by caroundw5h; 01-05-2004 at 12:02 AM.

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157

    Re: wow no error....except this

    try:

    for (count = strlen(word) -1 ; count >= 0; count--)
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    > gets(word);
    *cough*FAQ
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    4
    for (count = '\0' /*strlen(word) -1*/ ; count >= word[0]; count--)
    Why are you initializing the variable "count" to the NULL character? In ASCII that's 0.. and if you count backwards with your "count--" your test string "count >= word[0]" will never logically be true.. so your for loop doesn't do anything.

    I'm no expert on this but I believe that's your problem.

  5. #5
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Thank you for your reply. A more indepth study of Strings would help out and to remeber that in C everything is pretty much an integer value.

    I assumed the value of '\0' would mean the end of the string, and that I could count from there backwards. Obviously that is not the case. That is why i had strlen as well. Thank you for you help.


    salem: I'm getting to fgets, soon.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What makes WoW so damn addictive?
    By cboard_member in forum A Brief History of Cprogramming.com
    Replies: 91
    Last Post: 06-28-2006, 11:38 PM
  2. Wow. This is new to me. EVIL STL!
    By DeepBlackMagic in forum C++ Programming
    Replies: 13
    Last Post: 02-10-2003, 12:18 PM
  3. wow...technology today...
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 29
    Last Post: 02-01-2003, 06:37 PM
  4. wow sub 7? remote?
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 05-11-2002, 07:24 PM
  5. wow
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-13-2002, 08:39 PM

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