Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
DDD
/** * CSCA48 Summer 2020 * Exercise 1 - Fun with strings! * * Please read the comments below carefully, they describe your task in detail. * Any clarifications and corrections will be posted to Piazza so please keep * an eye on the forum! * * Starter code: Mustafa Quraish, 2020 */ #include <stdio.h> // NO additional imports allowed. You *can* make helper functions, and in fact I strongly // encourage doing so to make your life easier. // Maximum length of a string #define MAX_STRING_LEN 1024 int lengthy(char input[MAX_STRING_LEN]) { int count = 0; for (int i=0; i<MAX_STRING_LEN; i++) { if (input[i] != '\0') { count ++; } else { return count; } } return count; } //----------------------------------------------------------------------------- void replaceSubstring(char str[], char a[], char b[]) { /** * If `a` is a substring of `str`, then the first occurence of `a` in `str` * is replaced with `b`. * * For example: * * char str[50] = "Hello World!"; * replaceSubstring(str, "llo W", "---"); * printf("%s\n", str); * * should print out "He---orld!" * * You may assume that `str[]`` has enough space to store the modified string. * Remember that all the strings here all null-terminated, and you must * also null-terminate the modified string. */ int i=0; int start_index=0; int end_index=0; int occur=0; char str2[MAX_STRING_LEN]; str2[lengthy(str)]='\0'; for (int b=0;b<lengthy(str);b++) { str2[b]=str[b]; } while(str[i]!='\0') { if(str[i]==a[0]) { int k=0; int counter=0; while(a[k]!='\0') { if (str[i+k]==a[k]) { counter++; if (counter==lengthy(a)) { occur++; start_index=i; end_index=i+k; if(occur==1) { for (int p=0;p<start_index;p++) { str[p]=str[p]; } int count=0; for (int m=start_index;m<lengthy(b)+start_index;m++) { str[m]=b[count]; count++; } int n=0; while(str2[end_index+n]!='\0') { str[start_index+n+lengthy(b)]=str2[end_index+n+1]; str[start_index+lengthy(b)+end_index]='\0'; n++; } } } } k++; } } i++; } return; // Not returning anything. `str` is modified directly. } void changeSubstringCase(char str[], char a[]) { /** * If `a` is a substring of `str`, then change the case (capitalization) of * the first occurence of the corresponding substring in `str`. Special * characters (' ', '.', ...) do not need to be handled. You should only * change the letters. * * For example: * * char str[100] = "This IS a SENTence WITH BAd CAPITalizaTION"; * changeSubstringCase(str, "IS a SENTence WI"); * printf("%s\n", str); * * should print out "This is A sentENCE wiTH BAd CAPITalizaTION". */ int i=0; int start_index=0; int end_index=0; int occur=0; char str2[MAX_STRING_LEN]; str2[lengthy(str)]='\0'; for (int b=0;b<lengthy(str);b++) { str2[b]=str[b]; } while(str[i]!='\0') { if(str[i]==a[0]) { int k=0; int counter=0; while(a[k]!='\0') { if (str[i+k]==a[k]) { counter++; if (counter==lengthy(a)) { occur++; start_index=i; end_index=i+k; if(occur==1) { for (int p=0;p<start_index;p++) { str[p]=str[p]; } int count=0; for (int m=start_index;m<lengthy(a)+start_index;m++) { if (str2[m]>='a'&& str2[m]<='z') { str[m]=str2[m]-32; } else if (str2[m]>='A'&& str2[m]<='Z') { str[m]=str2[m]+32; } int n=0; while(str2[end_index+n]!='\0') { str[start_index+n+lengthy(a)]=str2[end_index+n+1]; str[start_index+lengthy(a)+end_index]='\0'; n++; } } } } } k++; } } i++; } return; // Not returning anything. `str` is modified directly. } //----------------------------------------------------------------------------- // Do not change the lines above and below the main function. These // are here to ensure that the main() function defined here does not // Conflict with the automarker when testing your code. Changing them // will result in a 0 for this exercise. You are free to change anything // inside the main() function itself. #ifndef __testing__ int main() { char str[MAX_STRING_LEN] = "CSCA48 Exercise 1"; char str2[MAX_STRING_LEN] = "Hello World!"; // This should replace the substring: replaceSubstring(str, "A48 Ex", "A48 sUMMER 2020 Ex"); // This should NOT replace anything, since some of the letters // in `a` have a different capitalization than in the string, so it is // NOT a valid substring. replaceSubstring(str, "summer 2020", "Winter 2020"); // This should change the capitalization of the substring: changeSubstringCase(str, "sUMMER 2020"); // This should NOT change the capitalization, since the corresponding letters // in `a` are lowercase, and it is not a valid substring. changeSubstringCase(str, "csca48"); printf("Expected result: \"CSCA48 Summer 2020 Exercise 1\"\n"); printf(" Your solution: \"%s\"\n", str); return 0; } #endif
run
|
edit
|
history
|
help
0
función malloc()
Character testing
5 trabajo semanal 4
SOLUCION USANDO UN CICLO Y PUNTERO
18BCE2182 ASSESS_1 Q2-1
CheckProcessorEndianness
MATRICES TRIANGULARES
good Job
domain.com
18BCE2182 MIDTERM PART-B ATOMIC