Sign in to follow this  
Followers 0
justsum13lse

CS 101 Homework(C). Writing a sentence backwards.

4 posts in this topic

I am attempting to make a program that rewrites a sentence backwards.  It is freezing up at the beginning of the for loop.  I am not sure if I am properly entering the array, or if the str[N] is even being recorded as an arrays through the gets function at the top.  Any help is appreciated. 

It is recording my str of i array as italics.  It is all supposed to be str[of i] but i changed it to j in the places it kept making it italics.  just pretend all the str[arrays] are str [ of i].

 

 

#include <stdio.h>
#include <string.h>

#define N 35
int main(void)

{
    char str[N];
    int i, j, temp;
    int length;

    printf("Enter a sentence up to %d characters long.\n", N);
    gets(str);
    printf("The sentence entered is: \n");
    puts(str); //Check to see if it is recording string.
    length = strlen(str);
    printf("%d", length);//Check to make sure it is recording the proper length
    for(i=0;i<length-1;i++)//Not sure if this is right.  I tried "for(i=length;i>=0;i--) and it froze up.
    {
       do
        {                       //Not sure if strings and arrays are mixed, if if str is being recorded as an array above.
        temp = str[j];             //Beginning at the for statement to the end of its loop is where I need help.
        str[j] = str[i+length-1];
        str[i+length-1] = temp;
        }
    }
    while{(str[i+length-1] !=str[j])}
    printf("%s", str);
    return 0;
}
 

Share this post


Link to post
Share on other sites

You should just iterate through the string backwards and print out each character one by one.

#include <stdio.h>
#include <string.h>
#define N 35
int main(void) {
    char str[N];
    int i;
    printf("Enter a sentence up to %d characters long: ", N);
    fgets (str, N, stdin);
    printf("Reversed sentence: ");
    for (i = strlen(str) - 1; i >= ; i--) {
        if (str[i] != '\n') {
            printf("%c", str[i]);
        }
    }
    printf("\n");
    return ;
}

Let me know if you have any other questions.

-Lee

Edit: That's supposed to say return 0; at the end. I'm not sure why the 0 keeps disappearing.

Share this post


Link to post
Share on other sites

We haven't really messed with strings yet.  Everything I have done with strings I have learned on my own.  stdin, i have watched a video that had that in it.  I know it stands for standard input.  But I haven't used fgets at all.  What exactly does fgets do?  How does it differ from gets?  I know fgets(<variable name>, <Value?>, <stdin?  Have no idea>;  Not sure what that line does.  I understand the for(i = strlen(str), but I didn't know you could do it like that.  Why is the "i >= ;"?  I take it there was a 0 there and it didn't get printed on the message.  

But if you could explain the fgets to me, that would be helpful.  I get the for loop.  Does the array/string <code> str [ i ] </code> recognize the last character in the array as a /n, or press of the enter button?  Is that recorded from the user input?

 Also, how do you enter code like you did above on this site?  Some sites do it with <code></code>, but apparently this one doesnt.

Thanks for the help.  I kind of gets this.

 

Nevermind on the fgets(variable, amount, stdin)  I watched a video and see that stdin is the keyboard.  But it records the last entry in the array as a \n?  

Share this post


Link to post
Share on other sites

Google will answer most of your questions regarding fgets and how it works.

Yes, it records \n, but I checked for this in the loop and didn't print it out.

It is i >= 0 because you are iterating over the string (char array) from the last character in the string at index strlen(str) - 1 to the first character in the string at index 0. (I guess that 0 doesn't display either.)

To enter a formatted code block, you just have to click the '<>' symbol above the text box.

Let me know if you have any other questions.

-Lee

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0