Jump to content

1st CS homework, any help appreciated

- - - - -

  • Please log in to reply
33 replies to this topic

#1
ClumsySpelunker

ClumsySpelunker

    Newbie

  • Members
  • PipPip
  • 15 posts
Hello, I'm embarrassed to be begging for help on a forum, but I've been working on this all afternoon and I just don't get it. This is like the 5th program I've written and is so far beyond the scope of what we've been doing... I just have no idea how to approach this. I'm not looking for "the answer" but some clarification on some of the fundamental details would be nice.

This is the assignment

Write a program that
5.1. Asks the user to insert a word
5.2. Reads the string from standard input (assume the work does not have more than 100 characters)
5.3. Converts all the characters to lower case.
5.4. Prints the string characters in reverse order. For example, if the input is "Luke", the program should print "ekul".

Here's what I have so far (without attempting 5.4)



#include<stdio.h>

#include<string.h>


char str1[100];

char str2[100];

char str3[100]; //for the 3rd part of the problem

int x;

int y;

    int main()

{

    printf("Enter your first name:\n");

    fgets(str1, sizeof(str1), stdin);

    sscanf(str1, "%s", str1); //Saving the input as str1


    str1[x]=y; /* this is where I'm confused, I don't know how to work with the array position variable vs. the array value... */


    while((y >= 'A') && (y <= 'Z')) { /*my shot in the dark on which loop to use and how to use it*/

    y= y+32;

    str1[x++];

    break;

    }


    strcpy(str2, str1); //because... I don't know...


    printf("Your name is %s", str1);


    return(0);

}


I assume we can't use other libraries. I know there are functions that will convert to upper/lower case and I'm fairly sure we won't get credit for using them.

So it's probably obvious that (I have no idea what I'm doing) I don't know how to work with strings! Our 10 minute run down on strings didn't cover reassigning values to elements. The textbook doesn't get this in-depth within the chapters we've covered, reading ahead hasn't helped, reading another C book I have hasn't helped, I can't find anything helpful through google (everyone suggests using different libraries)...

tldr: I'm very frustrated and would appreciate any help anyone might offer.

#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Don't be embarrassed, we all had to learn those easy tasks at some point.

Right, first note I'd give is, avoid global variables. Sometimes they are necessary but should be avoided if possible. Next part:

    fgets(str1, sizeof(str1), stdin);

    sscanf(str1, "%s", str1); //Saving the input as str1

Here, first line will save input from stdin to str1 so second line is not needed.

Loop part; why don't you try with for loop? You can use strlen() function to get str1 length or you can manually count (you have to watch for \0, null terminator). Assigning values to arrays is really easy. You just say at which index you'd like to change value to what. Like so:

str[index] = new_value;


One question; do you have to save strings for each part of assignment? Because it can easly be done with just 1 string. Reversing string is easy aswell; why not just print it backwards (start at n-th element and go towards start)?

Also, are you allowed to write your own functions? It's not necessary but could make code look prettier. :)
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#3
ClumsySpelunker

ClumsySpelunker

    Newbie

  • Members
  • PipPip
  • 15 posts
Thanks so much for the speedy response. We went over for loops briefly in class, I'll try that out.

so if I do:
y = y+32;
str1[x]=y;
x++;
in a loop, that will assign the new value and move to the next array element?

Yes, it all has to be one single program in the end. Woops, looks like we went over functions in our last lecture... I guess that's an option then.

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Please, when posting code use code tags (# button), makes it easier to read. Yes, that code would be correct, except that y will equal to garbage. Just assign value to y from x-th element.

for loop has following syntax:

for ( start; end_condition; increment ) {

    //code

}

So you could write something like this:

for (int x = 0; x < strlen(str1); ++x) {

    //your code

}

Note that there is a small "flaw" in code above; strlen() function will be called every time to check wheter x is still lower than size of str1 array. In homework tasks this usually isn't a problem, but when you'll do bigger projects, you'll probably want to save that size value to another variable.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#5
ClumsySpelunker

ClumsySpelunker

    Newbie

  • Members
  • PipPip
  • 15 posts
This is my latest stab at it. I found an example in the lecture slides that made it look like I should use while loop. Still have no idea if I'm doing it right. Am I handling the variable correctly now, or should I go back to trying to use 2?
Oh, I also added another string so there would be 1 exclusively for input and output. It didn't magically fix things like I'd hoped >:/


#include<stdio.h>

#include<string.h>


    int main(){


char str1[100];

char name [100];

    printf("Enter your first name:\n");

    fgets(str1, sizeof(str1), stdin);

    sscanf(str1, "%s", name);


int i = 0;


while (i <= strlen(name)){

  if((name[i] >= 'A') && (name[i] <= 'Z')){

    name[i]+32;

    i++;

    }

  if (name[i]=='\0'){

  break;

  }}


    printf("%s", name);

    return(0);

    }



#6
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Looks good, you only need to save your lower case string.

name[i] + 32;

This takes i-th value from name and adds 32 to it, but does not save it.


name[i] = name[i] + 32; // or name[i] += 32;

Also, that if isn't necessary since you check if you're out of range in while condition.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#7
ClumsySpelunker

ClumsySpelunker

    Newbie

  • Members
  • PipPip
  • 15 posts
#include<stdio.h>

#include<string.h>


    int main(){


char str1[100];

char name [100];

    printf("Enter your first name:\n");

    fgets(str1, sizeof(str1), stdin);

    sscanf(str1, "%s", name);


int i = 0;


while (i <= strlen(name)){

  if((name[i] >= 'A') && (name[i] <= 'Z'))

    name[i] = name[i] + 32;

    i++;}




    printf("%s", name);

    return(0);

    }


Well... I have to admit: I am teary eyed.

Me and homework have had some _____ times, but never anything like that. It works! Finally, finally!!

Thanks so, so much for your help!!!

note to self: you're not done. At least I can relax for a bit and revel in this brief satisfaction. Thanks again!

#8
ClumsySpelunker

ClumsySpelunker

    Newbie

  • Members
  • PipPip
  • 15 posts
Well, my teary jubilation was nice while it lasted. I'm back again, stuck on the last part.

I feel like what I have should work. Although I'm not sure if I need to be saving the reversed order to a new string. Regardless, it doesn't seem to be saving the reversed characters and returns nothing. And of course I could be WAY off (again)...

I think I once again have a redundant if i == '\0' line, but I threw that in trying random things to make it work.

So close yet so far!

#include<stdio.h>

#include<string.h>


    int main() {


char str1[100];

char name [100];


    printf("Enter your first name:\n");

    fgets(str1, sizeof(str1), stdin);

    sscanf(str1, "%s", name);


int i = 0;


    while (i <= strlen(name)){

        if((name[i] >= 'A') && (name[i] <= 'Z'))

            name[i] = name[i] + 32;

        i++;

    }



int y = strlen(name);

i = 0;

    while(i <= strlen(name)) {

    name[i] = name[y];

    i++;

    y--;

        if(name[i] == '\0')

        break;

    }


    printf("%s", name);

return(0);

}


#9
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1

name[i] = name[y];

This will at some point copy copied data, you should use separate array to store reversed string. You also have to append \0 after you've reversed the string.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#10
ClumsySpelunker

ClumsySpelunker

    Newbie

  • Members
  • PipPip
  • 15 posts
#include<stdio.h>

#include<string.h>


    int main() {


char str1[100];

char name [100];


    printf("Enter your first name:\n");

    fgets(str1, sizeof(str1), stdin);

    sscanf(str1, "%s", name);

    name[strlen(name)-1] = '\0';


int i = 0;


    while (i <= strlen(name)){

        if((name[i] >= 'A') && (name[i] <= 'Z'))

            name[i] = name[i] + 32;

        i++;

    }


char eman[100];

int y = strlen(name);

i=0;


    for(i=0; i < strlen(name); i++){

    name[i] = eman[y];

    y--;

    }


    printf("%s", eman);


return(0);

Flying Dutchman said:



You also have to append \0 after you've reversed the string.
I'm not sure I understand that. Add the \0 back in?

So something like
eman[sizeof(eman)] = '\0';
?

#11
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Yes (not with sizeof operator but strlen() function), like you've done it here (after fgets):

name[strlen(name)-1] = '\0';

Although there it wasn't necessary since fgets() does that for you.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#12
ClumsySpelunker

ClumsySpelunker

    Newbie

  • Members
  • PipPip
  • 15 posts
But but... this is actually something I was confused about in our lecture when we learned about appending the '\0'

We learned strlen gets the size of the string minus the '\0', so I thought of it as basically sizeof - 1.

So in my mind if I've copied the name in the string, assigning string[strlen(string)-1] = '\0' is going to copy over the last character.

AHHHHHH! So close! It prints a W before the reversed word now...

#include<stdio.h>

#include<string.h>


    int main() {


char str1[100];

char name [100];


    printf("Enter your first name:\n");

    fgets(str1, sizeof(str1), stdin);

    sscanf(str1, "%s", name);


int i = 0;


    while (i <= strlen(name)){

        if((name[i] >= 'A') && (name[i] <= 'Z'))

            name[i] = name[i] + 32;

        i++;

    }

printf("%s\n", name);


char eman[100];

int y = strlen(name);

i=0;


    for(i=0; i < strlen(name); i++){

    eman[y] = name[i];

    y--;

    }


    eman[strlen(eman)-1] = '\0';


    printf("Y U NO WORK %s", eman);


return(0);

}





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users