Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Problem 1
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- int main(int argc,char* argv[])
- {
- for(int i = argc - 1; i >=0; i--) {
- printf("%s \n", argv[i]);
- }
- return 0;
- }//end main
- Problem 2
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main(void)
- {
- FILE* fp = NULL;
- char filename[50];
- printf("Enter your file: ");
- fscanf(stdin, "%s", filename);
- fp = fopen(filename, "r");
- if (fp != NULL){
- printf("File exists");
- }
- else{
- printf("Sorry B");
- }
- fclose(fp);
- return 0;
- }//end main
- COMPETITION PROGRAM Un-Mystified
- #include <stdio.h>
- #include <string.h>
- #include <stdbool.h>
- void FlushInput(void);
- bool FileExists(char*);
- bool CreateFile(char*, char*);
- int main(void){
- FILE* fIn = NULL;
- char textIn[300];
- char fileName[80];
- char saveFile = 'y';
- bool result = false;
- printf("Enter in some text to save:");
- if(fgets(textIn,300,stdin)){
- printf("Your entered in %d characters.\n", (int) strlen(textIn));
- printf("Enter in the name of a text file: ");
- fscanf(stdin,"%s",fileName);
- if(FileExists(fileName) == true){
- printf("The file exists, enter 'y' to overwrite:");
- FlushInput();
- scanf("%c", &saveFile);
- if(saveFile == 'y')
- result = CreateFile(textIn, fileName);
- else
- printf("You chose not to save the data.\n");
- }
- else{
- result = CreateFile(textIn, fileName);
- }//end if-else
- }
- else {
- printf("You did not enter in anything to save.\n");
- }
- if(result)
- printf("You successfully created a file named %s\n", fileName);
- else
- printf("You did not create a file named %s\n", fileName);
- return 0;
- }//end main
- bool FileExists(char* fileName){
- FILE* fIn = fopen(fileName,"r");
- if(fIn == NULL)
- return false;
- else{
- fclose(fIn);
- return true;
- }
- }//end function
- void FlushInput(void){
- while(getchar() != '\n');
- return;
- }//end function
- bool CreateFile(char* textIn, char* fName){
- FILE* fPtr = fopen(fName,"w");
- if(fPtr == NULL){
- printf("Error opening file.\n");
- return false;
- }
- else{
- fputs(textIn, fPtr);
- fclose(fPtr);
- return true;
- }//end if-else
- }//end function
- Problem 3
- #include <stdio.h>
- #include <stdlib.h>
- int main () {
- int i, n;
- int *a = NULL;
- printf("Number of elements to be entered:");
- scanf("%d",&n);
- a = (int*)calloc(n, sizeof(int));
- printf("Enter %d numbers:\n",n);
- for( i=0 ; i < n ; i++ ) {
- scanf("%d",&a[i]);
- }
- printf("The numbers entered are: ");
- for( i=0 ; i < n ; i++ ) {
- printf("%d ",a[i]);
- }
- free( a );
- return(0);
- }
- Problem 4
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main () {
- char *str;
- /* Initial memory allocation */
- str = (char *) malloc(13);
- strcpy(str, "Maryhadalamb");
- printf("String = %s, Address = %u\n", str, str);
- /* Reallocating memory */
- str = (char *) realloc(str, 25);
- strcat(str, ", it stunk!");
- printf("String = %s, Address = %u\n", str, str);
- free(str);
- return(0);
- }
Add Comment
Please, Sign In to add comment