Skip to content

Instantly share code, notes, and snippets.

@jullle3
Created October 6, 2016 18:47
Show Gist options
  • Save jullle3/2b645ed0cbe5f04d3f741d139d86bd1f to your computer and use it in GitHub Desktop.
Save jullle3/2b645ed0cbe5f04d3f741d139d86bd1f to your computer and use it in GitHub Desktop.
Uge 6.3 opgave
/*
============================================================================
Name : 3.c
Author :
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define SIZE 50
int main () // Main start
{
setvbuf(stdout, NULL,_IONBF, 0); // Force flush
setvbuf(stdout, NULL, _IOLBF, 0); // Another force flush
int a = '0'; // Opretter forskellige datatyper
double b = '2';
float c = '0';
puts("Indtast 3 talv�rdier");
scanf("%d%lf%f", &a, &b, &c); // Tager input fra brugeren og gemmer
// det i de givne datatyper
char s[] = "Vores tekst string";
printf("int a: %d \ndouble b: %lf \nfloat c: %f \n", a, b, c); // Printer v�rdierne
printf("Line entered ");
puts(s); // Printer string
puts("_______________________________________");
// Tester 10 string funktioner! //
if( isdigit(a) ) // isdigit - Tester om given int er et tal
{
printf("\n|isdigit|a = |%d| is a digit\n", a );
}
else
{
printf("\n|isdigit|a = |%d| is not a digit\n", a );
}
if( isalpha(b) ) // isalpha - Tester om given int er et bogstav
{
printf("|isalpha|b = |%lf| is a letter\n", b );
}
else
{
printf("|isalpha|b = |%lf| is not a letter\n", b );
}
char m = "x"; // putchar - udskriver given char
printf("|putchar| Character entered: ");
putchar(m);
puts("\n|puts| S�tning printet af puts"); // puts - bruges meget til at udskrive tekst
char sprintfstring[SIZE]; // sprintf - printer givne v�rdier ind i en string, istedet for sk�rmen
sprintf(sprintfstring, "%d, %lf, ", a, b);
printf("|sprintf| Udfyldt array (sprintfstring[]), best�ende af a(%d) og b(%lf): %s\n", a, b, sprintfstring);
int a2; // Definerer to nye v�rdier, som sscanf skal gemme v�rdier i
double b2;
sscanf(sprintfstring, "%d, %lf", &a2, &b2); // sscanf - scanner fra string, istedet fra tastaturet
printf("|sscanf| L�ste fra forrige array, og gemte v�rdierne i a2 = %d, b2 = %lf\n\n", a2, b2);
puts("De to strings:"); // Udskriver de nye strings, for overskuelighed
printf("sprintfstring: %s \ns: %-15s", sprintfstring, s);
strcpy(s, sprintfstring); // strcpy - kopierer(overwriter) array til array
printf("\n\n|strcpy| Kopierer(og overwriter) sprintfstring ind i s, s�ledes s bliver: %s \n", s);
strcat(s, sprintfstring); // strcat - Tilf�jer string ind i en anden string(uden at overwrite!)
printf("|strcat| Tilf�jer sprintfstring ind i s, s�ledes s bliver: %s\n\n", s);
puts("De to strings nu:");
printf("sprintfstring: %s \ns: %-15s", sprintfstring, s);
puts("\n\nLaver to nye strings");
const char *array1 = "A nicely long string :)"; // OBS: Bem�rk, pointer til string
const char *array2 = "Test";
puts("De to nye strings:");
printf("array1: %s \narray2: %s\n\n", array1, array2);
int ret;
ret = strcmp(array1, array2); //strcmp - sammenligner to strings ASCII v�rdier.
if(ret < 0) // if return mindre end 0
{
printf("|strcmp|array1 is less than array2\nstrcmp returned -1");
}
else if(ret > 0) // if return st�rre end 0
{
printf("|strcmp|array 1 is greater than array2\nstrcmp returned 1");
}
else // if return = 0
{
printf("|strcmp|array1 is equal to array2\nstrcmp returned: 0");
}
puts("\n\n|strstr|"); // Viser hvilken funktion vi tester nu
const char key1[]= "password"; // S�gen�gle 1
const char key2[]= ":)"; // S�gen�gle 2
// strstr - s�ger en given v�rdi i en string
if( strstr(array1, key1) != NULL) // s�ger for key1
{
printf("\n\n'%s'(key1) was found in '%s'\n", key1, array1);
}
else
{
printf("'%s'(key1) was not found in '%s'\n", key1, array1);
}
if( strstr(array1, key2) != NULL) // s�ger for key2
{
printf("'%s'(key2) was found in '%s'\n", key2, array1);
}
else
{
printf("'%s'(key2) was not found in '%s'\n", key2, array1);
}
system("PAUSE");
return 0;
} // main end
/*
============================================================================
Name : 3.c
Author :
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define SIZE 50
int main () // Main start
{
setvbuf(stdout, NULL,_IONBF, 0); // Force flush
setvbuf(stdout, NULL, _IOLBF, 0); // Another force flush
int a = '0'; // Opretter forskellige datatyper
double b = '2';
float c = '0';
puts("Indtast 3 talv�rdier");
scanf("%d%lf%f", &a, &b, &c); // Tager input fra brugeren og gemmer
// det i de givne datatyper
char s[] = "Vores tekst string";
printf("int a: %d \ndouble b: %lf \nfloat c: %f \n", a, b, c); // Printer v�rdierne
printf("Line entered ");
puts(s); // Printer string
puts("_______________________________________");
// Tester 10 string funktioner! //
if( isdigit(a) ) // isdigit - Tester om given int er et tal
{
printf("\n|isdigit|a = |%d| is a digit\n", a );
}
else
{
printf("\n|isdigit|a = |%d| is not a digit\n", a );
}
if( isalpha(b) ) // isalpha - Tester om given int er et bogstav
{
printf("|isalpha|b = |%lf| is a letter\n", b );
}
else
{
printf("|isalpha|b = |%lf| is not a letter\n", b );
}
char m = "x"; // putchar - udskriver given char
printf("|putchar| Character entered: ");
putchar(m);
puts("\n|puts| S�tning printet af puts"); // puts - bruges meget til at udskrive tekst
char sprintfstring[SIZE]; // sprintf - printer givne v�rdier ind i en string, istedet for sk�rmen
sprintf(sprintfstring, "%d, %lf, ", a, b);
printf("|sprintf| Udfyldt array (sprintfstring[]), best�ende af a(%d) og b(%lf): %s\n", a, b, sprintfstring);
int a2; // Definerer to nye v�rdier, som sscanf skal gemme v�rdier i
double b2;
sscanf(sprintfstring, "%d, %lf", &a2, &b2); // sscanf - scanner fra string, istedet fra tastaturet
printf("|sscanf| L�ste fra forrige array, og gemte v�rdierne i a2 = %d, b2 = %lf\n\n", a2, b2);
puts("De to strings:"); // Udskriver de nye strings, for overskuelighed
printf("sprintfstring: %s \ns: %-15s", sprintfstring, s);
strcpy(s, sprintfstring); // strcpy - kopierer(overwriter) array til array
printf("\n\n|strcpy| Kopierer(og overwriter) sprintfstring ind i s, s�ledes s bliver: %s \n", s);
strcat(s, sprintfstring); // strcat - Tilf�jer string ind i en anden string(uden at overwrite!)
printf("|strcat| Tilf�jer sprintfstring ind i s, s�ledes s bliver: %s\n\n", s);
puts("De to strings nu:");
printf("sprintfstring: %s \ns: %-15s", sprintfstring, s);
puts("\n\nLaver to nye strings");
const char *array1 = "A nicely long string :)"; // OBS: Bem�rk, pointer til string
const char *array2 = "Test";
puts("De to nye strings:");
printf("array1: %s \narray2: %s\n\n", array1, array2);
int ret;
ret = strcmp(array1, array2); //strcmp - sammenligner to strings ASCII v�rdier.
if(ret < 0) // if return mindre end 0
{
printf("|strcmp|array1 is less than array2\nstrcmp returned -1");
}
else if(ret > 0) // if return st�rre end 0
{
printf("|strcmp|array 1 is greater than array2\nstrcmp returned 1");
}
else // if return = 0
{
printf("|strcmp|array1 is equal to array2\nstrcmp returned: 0");
}
puts("\n\n|strstr|"); // Viser hvilken funktion vi tester nu
const char key1[]= "password"; // S�gen�gle 1
const char key2[]= ":)"; // S�gen�gle 2
// strstr - s�ger en given v�rdi i en string
if( strstr(array1, key1) != NULL) // s�ger for key1
{
printf("\n\n'%s'(key1) was found in '%s'\n", key1, array1);
}
else
{
printf("'%s'(key1) was not found in '%s'\n", key1, array1);
}
if( strstr(array1, key2) != NULL) // s�ger for key2
{
printf("'%s'(key2) was found in '%s'\n", key2, array1);
}
else
{
printf("'%s'(key2) was not found in '%s'\n", key2, array1);
}
system("PAUSE");
return 0;
} // main end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment