Thread: Unexpected output

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    39

    Unexpected output

    'm new to c can someone help me out i'm trying to write a program that reverses each letter in a sentence. eg - going home = gniog emoh. but i keep getting strange char's heres the code:
    Code:
    #include<stdio.h>
    int main()
    {
       char info[100];
       char temp;
       int i = 0;
       int n;
       int j = 0;
       int count;
    
       printf("Enter info to encrypt: \n");
       scanf("%[^\n]s", info);
    
       while(info[n] != '\0')
       {
          n = 0;
          while(info[j] != ' ')
          {
             
             j++;
          }
    
          count = j;
          printf("%d", j);
    
          while(i <= count)
          {
    
             temp = info[i];
             info[i] = info[j];
             printf("%c", info[i]);
             info[i] = temp;
             j--;
             i++;
    
          }
    
          j = count;
          j++;
          if(info[j] == ' ')
          {
             n = info[j];
             printf("%c", n);
             j++;
          }
          n++;
       }
    
       return 0;
    }
    Last edited by GolDRoger; 11-18-2011 at 01:58 PM. Reason: made a typing mistake

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    How do you expect loops like this to ever exit?
    Code:
          while(info[j] != ' ')
          {
             j=0;
             j++;
          }
    Step through at least 3 iterations of your loop in your head or on paper. What is 'j' each time?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    39

    Hey

    Hey i said i'm new to programming so just help me out don't dis me.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I'm not dis'ing you, I'm telling you how to figure out your problem. Instead of getting defensive for no reason, maybe you should just try the suggestion.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    39
    'j' is the number for the last word before each blank space.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    39
    i think i changed sumthin coz that part of the code worked prreviously

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    39
    Oh okay but wat about the rest of the code?

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    39
    Yeah i saw where i went wrong but my problem is with the rest of the code.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I have to say I'm with grumpy on this one. I think you coded all this before you knew exactly what it was you wanted to do. That is, you didn't plan well before you started coding. I probably spend more time reading and planning than I do actually coding, by a factor of 3 to 1. We know you're new, but honestly, learning to plan before you code is one of the first skills any programmer should learn.

    You need to be able to solve this problem by hand, on paper, before you ever touch the keyboard. Work through several examples by hand. Try examples like sentences with one word, words with single characters, words with several spaces between them, punctuation, etc. Make sure you can handle all the bizarre scenarios too. Pay very careful attention to every little step you did. If you can explain each step in detail, in plain English (or your native language), then the code will become much, much easier to write.

    Then, when it comes time to code, you need to start small, and code little bits at a time, compiling and testing often, and fixing all issues before moving on. For example, first I would simply make my program print out the full sentence the user entered. Then I would make it print out only the words, ignoring the punctuation, so you know you can properly isolate each word. Then I might have it print out the words in reverse, without spaces or punctuation, to make sure your reversal code works. Lastly I would have it print out the words in reverse with the spaces and punctuation back in.

    It might help if you used better names than i and j, like start and end. start should always be set to the first letter of a word, and end to the last. Then, you print from end back to start, set start to the character after end, and look for the beginning of a word again. Always make sure you don't go off the end of the array, or you will end up in the land of undefined behavior, which will cause strange problems for your program and headaches for you.

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    39
    WOW! Thanx alot i'll try that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unexpected output
    By juice in forum C Programming
    Replies: 24
    Last Post: 11-18-2011, 11:18 PM
  2. Unexpected Output, modal window!
    By gaurav_13191 in forum Windows Programming
    Replies: 13
    Last Post: 06-29-2011, 03:47 PM
  3. Unexpected Output in structure
    By bhagwat_maimt in forum C++ Programming
    Replies: 1
    Last Post: 12-29-2006, 10:50 PM
  4. unexpected output
    By crash88 in forum C Programming
    Replies: 2
    Last Post: 05-16-2006, 09:03 PM