stackoverflow1453

Programming&&Games&&Life

Archive for September, 2013

Write a program that inputs an ASCII code and prints the corresponding character.

/*

Name:
Copyright: Deitel C How to Program
Author: StackOverflow1453
Date: 9/25/2013 3:03:08 PM
Description:

(Printing Letters for Various ASCII Codes) Write a program that inputs an ASCII code and
prints the corresponding character. Modify this program so that it generates all possible three-digit
codes in the range 000 to 255 and attempts to print the corresponding characters. What happens
when this program is run?

Output:

Enter an ASCII character code ( EOF to end ): 90
The corresponding character is 'Z'

*/

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int main(void){

	char c[4];

	puts("Enter an ASCII character code ( EOF to end ): ");

	gets(c);  //gets returns a pointer to entered string

	while (c!=EOF)
	{

		if (atoi(c)>0 && atoi(c)<255)
		{
			printf( "%c\n", atoi(c) );
		}
		else
		{
			puts("Not in ASCII range. Please enter again: ");
		}
		gets(c);

	}

	getch();

}

Write a program that reads a series of strings and prints only those strings that end with the letters “ed.”

/*

Name:
Copyright: Deitel C How to Program
Author: StackOverflow1453
Date: 9/25/2013 12:04:14 PM
Description:

(Strings Ending with "ed") Write a program that reads a series of strings and prints only
those strings that end with the letters “ed.”

*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int i;

int main(void){

	char strings[4][100];
	char comp[3]="ed";

	puts("Enter 4 string: ");

	//get the strings
	for (i = 0; i < 4; i++)
	{
		fgets(strings[i], 99, stdin);
	}

	//print the strings
	for (i = 0; i < 4; i++)
	{
		if ( strstr(strings[i],comp)!=NULL )
		{
			printf("%s", strings[i]);
		}

	}

	getch();
}

Write a program that reads a series of strings and prints only those strings beginning with the letter “b.”

/*

Name:
Copyright: Deitel C How to Program
Author: StackOverflow1453
Date: 9/25/2013 11:35:02 AM
Description:

(Strings Starting with "b") Write a program that reads a series of strings and prints only
those strings beginning with the letter “b.”

*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int i;

int main(void){

	char strings[4][100];

	puts("Enter 4 string: ");

	//get the strings
	for (i = 0; i < 4; i++)
	{
		fgets(strings[i], 99, stdin);
	}

	//print the strings
	for (i = 0; i < 4; i++)
	{
		if (strings[i][0]=='b')
		{
			printf("%s", strings[i]);
		}

	}

	getch();
}

Write a program to sort inputted city names using 2D ARRAY.

/*

Name:
Copyright: Deitel C How to Program
Author: StackOverflow1453
Date: 9/17/2013 11:53:17 AM
Description:

(Alphabetizing a List of Strings) Use the string-comparison functions discussed in
Section 8.6 and the techniques for sorting arrays developed in Chapter 6 to write a program that
alphabetizes a list of strings. Use the names of 10 or 15 towns in your area as data for your program.

Enter a string: Westborough
Enter a string: Wellesley
Enter a string: Natick
Enter a string: Waltham
Enter a string: Framingham
Enter a string: Marlborough
Enter a string: Boston
Enter a string: Ashland
Enter a string: Hopkington
Enter a string: Shrewsbury

The strings in sorted order are:
Ashland
Boston
Framingham
Hopkington
Marlborough
Natick
Shrewsbury
Waltham
Wellesley
Westborough

*/

#include <stdio.h>
#include <stdlib.h>   
#include <string.h>
#include <conio.h>
#define SIZE 10

void sort(char x[SIZE][100]);
void swap(char *first, char *second);

int i,j;
char names[SIZE][100];
char temp[100];

int main(void){

	//get the names of the cities
	puts("Enter names of cities");
	for (i = 0; i < SIZE; i++)
	{		
		fgets( names[i], 99, stdin );
	}
	//print entered names
	puts("\nUnsorted Names:\n");
	for (i = 0; i < SIZE; i++)
	{
		printf("%s", names[i]);
	}

	sort(names);

	//print sorted names
	puts("\nSorted Names:\n");
	for (i = 0; i < SIZE; i++)
	{
		printf("%s", names[i]);
	}

	getch();
}

//sorting function
void sort(char dummy[SIZE][100]){

	for (i = 0; i < SIZE-1; i++)
	{
		for (j = i+1; j < SIZE; j++)
		{
			if (strcmp( dummy[i], dummy[j] ) >0)
			{
				swap(dummy[i],dummy[j]);
			}

		}

	}

}

//swapping function
void swap(char *first, char *second){

	strcpy(temp, second);
	strcpy(second, first);
	strcpy(first, temp);

}

Write a program to sort inputted city names using Array Pointers.

/*

Name:
Copyright: Deitel C How to Program
Author: StackOverflow1453
Date: 9/17/2013 11:53:17 AM
Description:

(Alphabetizing a List of Strings) Use the string-comparison functions discussed in
Section 8.6 and the techniques for sorting arrays developed in Chapter 6 to write a program that
alphabetizes a list of strings. Use the names of 10 or 15 towns in your area as data for your program.

Enter a string: Westborough
Enter a string: Wellesley
Enter a string: Natick
Enter a string: Waltham
Enter a string: Framingham
Enter a string: Marlborough
Enter a string: Boston
Enter a string: Ashland
Enter a string: Hopkington
Enter a string: Shrewsbury

The strings in sorted order are:
Ashland
Boston
Framingham
Hopkington
Marlborough
Natick
Shrewsbury
Waltham
Wellesley
Westborough

*/

#include <stdio.h>
#include <stdlib.h>   
#include <string.h>
#define SIZE 5

void sort(char **dummy);
void swap(char *first, char *second);

int i,j;
char *names[SIZE];
char *temp;

int main(void){

	//get the names of the cities
	puts("Enter names of cities");
	for (i = 0; i < SIZE; i++)
	{
		names[i]=malloc(100);
		fgets( names[i], 99, stdin);
	}
	//print entered names
	puts("\nUnsorted Names:\n");
	for (i = 0; i < SIZE; i++)
	{
		printf("%s", names[i]);
	}

	sort(names);


	//print sorted names
	puts("\nSorted Names:\n");
	for (i = 0; i < SIZE; i++)
	{
		printf("%s", names[i]);
	}

	getch();
}

//sorting function
void sort(char **dummy){

	for (i = 0; i < SIZE-1; i++)
	{
		for (j = i+1; j < SIZE; j++)
		{
			if (strcmp( dummy[i], dummy[j]) >0)
			{
				swap(&dummy[i], &dummy[j]);
			}

		}

	}

}

//swapper function
void swap(char **first, char **second){

	temp=*second;
	*second=*first;
	*first=temp;

}

Simplest game

/*

Name:
Copyright: 
Author: StackOverflow1453
Date: 9/18/2013 2:52:28 PM
Description: Simple game!!!


*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void){

	char check[]="StackOverflow1453";
	//guestName is in stack because we gave how wide it is while declaration
	//so we dont have to use malloc()
	char guestName[50];
	char *p;

	puts("Who are you?");
	//guestName=malloc(100);
	fgets(guestName, 49, stdin);

	/*
	After fgets you press enter so for comparison to be healthy, 
	this '\n' must be removed.
	*/
	p=strchr(guestName, '\n');
	*p='';
	
	//Compare two strings
	if (!strcmp(guestName, check))
	{
		puts("You can pass!!");
	}
	else
	{
		puts("You can not enter");
	}

	getch();

}

What is stack? What is heap?

http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap

http://stackoverflow.com/questions/18193850/whats-the-error-with-fgets-in-this-code?rq=1