[spoiler]Write a program that given 2 ASCII stings as an input, checks if they are exactly equal.
Code: Select all
#include <stdio.h>
#define STRING1 "equal"
#define STRING2 "Equal"
int main()
{
char* a = STRING1;
char* b = STRING2;
if (a == b)
{
printf("OK\n");
}
else
{
printf("Very bad\n");
}
return 0;
}[spoiler]Write a program that checks if one string is part of another (if it's a substring).
Code: Select all
#include <stdio.h>
#define SIZE 100
#define STRING1 "this is a string"
#define STRING2 "this is a"
int main()
{
char a[SIZE] = STRING1;
char b[SIZE] = STRING2;
int x, y;
y = strlen(STRING1);
while (a[x] == b[x])
{
if (b[x] == '\0')
{
break;
}
else
{
x++;
}
}
printf("%d-character long substring, out of %d-character long main string\n", x, y);
return 0;
}I think I'm missing something here.
BTW, when I compiled it with terminal, I got this:
It ran fine though.c.c:14:6: warning: incompatible implicit declaration of built-in function ‘strlen’ [enabled by default]
I remember doing this before, but I get it scr..ed now. I'll try this again later.Write a program that given an array of 10 numbers, it orders it ascendantly.
[spoiler]Write a program that given 2 ASCII stings as an input, checks if they are exactly equal, but case-insensitive (that is, given "hello" and "HeLLo", it should say they are equal) This one could be a bit tricky
Code: Select all
#include <stdio.h>
#define STRING1 "STRING"
#define STRING2 "string"
int main()
{
char s1[] = STRING1;
char s2[] = STRING2;
int l = 0;
int equal;
for (l = 0; l <= 5; l++)
{
if (s1[l] > 64 && s1[l] < 91)
{
s1[l]+=32;
}
if (s2[l] > 64 && s2[l] < 91)
{
s2[l]+=32;
}
}
while (s1[l] == s2[l])
{
if (l <= 5)
{
l++;
}
else
{
equal = 1;
break;
}
}
if (equal = 1)
{
printf("The strings are equal, but are case-different.");
}
else
printf("The strings are unequal.")
return 0;
}A few notes about this one..
- - I looked at snailface's code, but I took what I needed and rewrote it in my own way. Thanks for the unintended help.
- I didn't want to do a blind copy & paste, so I inspected it deeper for a bit. At first I didn't understand what the "> 64" and "<91" meant, but I then remembered you said computers work by numbers, something with ASCII. There are 26 ABC letters, so inbetween 64 and 91 is probably the range of numeric values for capital letters. Adding 32 is supposed to change them into small letters. A little search with google and this table confirmed it. Yeah, I'm a hard working student.
[spoiler]Write a program that given two matrices float a[3][3] and float b[3][3], adds them.
Code: Select all
#include <stdio.h>
int main()
{
float a[3][3] = {4, 2, 7, 1, 7, 2, 5, 6, 2};
float b[3][3] = {0, 9, 5, 3, 6, 8, 1, 4, 3};
int x, y = 0;
for (x = 0; x <= 2; x++)
{
for (y = 0; y <= 2; y++)
{
printf("[%1.0f + %1.0f] = [%1.0f]\n", a[x][y], b[x][y], a[x][y]+b[x][y]);
}
}
return 0;
}Not sure if that's what you meant.. here's one with an output similar to how it looks on WIkipedia:
[spoiler]
Code: Select all
#include <stdio.h>
int main()
{
float a[3][3] = {4, 2, 7, 1, 7, 2, 5, 6, 2};
float b[3][3] = {0, 9, 5, 3, 6, 8, 1, 4, 3};
int x, y, temp1, temp2;
for (x = 0; x <= 2; x++)
{
for (y = 0; y <= 1; y++)
{
temp2 = a[x][y] + b[x][y];
printf("[%1.0f %1.0f] = [%1.0f %1.0f] = [%1.0f + %1.0f %1.0f + %1.0f] = [%1.0f %1.0f]\n", a[x][y], a[x][y+1], b[x][y], b[x][y+1], a[x][y], b[x][y], a[x][y+1], b[x][y+1], a[x][y]+b[x][y], a[x][y+1]+b[x][y+1]);
}
if (x <= 1)
{
printf("\n");
}
}
return 0;
}That printf line looks like a total mess imo.

