Week 8 (10.25.)
Task descriptions are on the portal
Task 1
#include <stdio.h>
#include <string.h>
void swap(char* a, char* b) {
char temp = *a;
*a = *b;
*b = temp;
}
void remove_spaces(char* input) {
size_t length = strlen(input);
for (int i = 0; i < (length/2); i++) {
swap(&input[i], &input[length - i - 1]);
}
}
int main(void) {
char text[50+1];
strcpy(text, "Hello, this is text!");
//[50];
//scanf("%s", text);
printf("%s\n", text);
remove_spaces(text);
printf("%s", text);
return 0;
}
Task 2
#include <stdio.h>
#include <string.h>
void remove_spaces(char* input) {
size_t length = strlen(input);
for (int i = 0; i < length; i++) {
while (input[i] == ' ') {
for (int j = i; j < length; j++) {
input[j] = input[j+1];
}
}
}
}
int main(void) {
char text[50+1];
strcpy(text, "Hello, this is text!");
//[50];
//scanf("%s", text);
printf("%s\n", text);
remove_spaces(text);
printf("%s", text);
return 0;
}
Task 3
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* remove_spaces(char* input) {
size_t input_length = strlen(input);
char * output = (char*)malloc((input_length+1)*sizeof(char));
size_t output_length = 0;
for (int i = 0; i < input_length; ++i) {
if (input[i] != ' ')
output[output_length++] = input[i];
}
output[output_length] = '\0';
return output;
}
int main(void) {
char text[50+1];
strcpy(text, "Hello, this is text!");
printf("%s\n", text);
char* spaceless = remove_spaces(text);
printf("%s", spaceless);
free(spaceless);
return 0;
}
- ← Previous
Week 7 (10.18.) - Next →
Week 9 (11.07.)