lynes_tony_lab6.c - /reads data from input fgets input SIZE...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
/* *Anthony Lynes *Lab6, CSCI 112 *4/3/16 */ #include <string.h> //needed for new string commands i.e. memset #include <stdlib.h> //needed for strtol function #include <ctype.h> //needed for using isalnum function #include <stdio.h> //needed for fgets function #define SIZE 80 //defines SIZE as 80 // function prototypes void get_input(char *); void reverse(char *); // program entry point int main(int argc, char * argv[]) { //checks for correct number of args if(argc != 2){ printf("ERROR: Please provide an integer greater than or equal to 0\n");//error message return 0;//exits program after displayign error } //convert arg to int an check that it is within range int N = (int) strtol( argv[1], NULL, 10); //checks to see if entry is less than 0 if ( N < 0 ){ printf("ERROR: Please provide an integer greater than or equal to 0\n");//error message return 0;//exits program after displaying error message } // limits input to the defined size char input[SIZE]; int i; for(i=0; i < N; i++) {get_input(input); reverse(input); printf("%s\n", input); } return 0;//exits program when completed } void get_input(char * input)
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: { //reads data from input fgets( input, SIZE, stdin ); //stores as pointer //searches a string for the first occurance of the given character char * ptr = strchr(input, '\n'); if ( ptr ) {ptr = '\0'; }} //function to reverse the input void reverse(char * line) { char copy[SIZE]; char word[SIZE]; //sets memory to a specific value memset(copy, 0, SIZE); //gets the number of characters in a string int line_len = strlen(line); int word_len = 0; int i; for( i=line_len ; i >= 0; --i ) {// allocates a space for reversing the line if ( line[i] == ' ' && word_len > 0 ) { memset(word, 0, SIZE); //copies some number of characters out of a string strncpy(word, line + i + 1, word_len); //concatenates one string onto another strcat( copy, word ); strcat(copy, " "); word_len = 0; //printf("word = %s\n", word); //checks if a character is alphanumeric }else if( isalnum( line[i] ) || line[i] == '\'' ) {word_len++;}} if (word_len > 0 ){ memset(word, 0, SIZE); strncpy(word, line, word_len); strcat( copy, word);} strcpy( line, copy );}...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern