Write a function, named "convert", that takes a single argument, a string (char pointer/array). C language | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Write a function, named "convert", that takes a single argument, a string (char pointer/array). C language

Write a function, named "convert", that takes a single argument, a string (char pointer/array). The function should modify the string so that all single quote characters are replaced with double quotes. 2 input cases: 1 #include <stdio.h> 2 #include <string.h> 3 void convert(char * string); 4 5 char string[] = "'abc'def\"'"; 6 printf("\nStart = %s\n", string); 7 convert(string); 8 printf("End = %s\n", string); 9 assert(strcmp(string, "\"abc\"def\"\"") == 0); 10 INPUT OF THE TEST CASE 1 #include <stdio.h> 2 #include <string.h> 3 void convert(char * string); 4 5 char string[] = "My name is 'Josh'! \"Scare Quotes\""; 6 printf("\nStart = %s\n", string); 7 convert(string); 8 printf("End = %s\n", string); 9 assert(strcmp(string, "My name is \"Josh\"! \"Scare Quotes\"") == 0); Al up code is my try and complete on it, please Write it as a code to be more clear

22nd Mar 2021, 7:57 PM
STOP
STOP - avatar
5 Answers
+ 1
void convert(char *s) { while ((s = strchr(s, '\' ')) *s = ' " '; } s = strchr() will jump to any singlequote in the string and set it to a double quote. but if there is no singlequote it will be NULL and it exits the loop.
22nd Mar 2021, 8:26 PM
Damyian G
Damyian G - avatar
0
Damyian G Does it mean that the function searches for the quotation mark and then stops, or must a stop condition be set?
22nd Mar 2021, 8:29 PM
STOP
STOP - avatar
0
yeah, it searches for singlequotes and returns a pointer to that location in the string. that's why *s = ' " ' will set that char to a double quote. the stop condition is set when strchr() returns NULL (end of string or no more single quotes).
22nd Mar 2021, 8:35 PM
Damyian G
Damyian G - avatar
0
Damyian G alright please check my code void convert(char * string) { for(*string; *string!='/0'; *(string++)) { if(*string=='\'') *string='"'; } } what is wrong here?
22nd Mar 2021, 8:39 PM
STOP
STOP - avatar
0
the null byte should be '\0' instead of '/0', but also *string and *(string++) will not do anything so if you want you can leave that empty. for ( ; *string != '\0'; string++) *string will return the character of the string where that string pointer is in memory, but you dont need to get the character to increase the pointer (if that makes sense).
22nd Mar 2021, 8:42 PM
Damyian G
Damyian G - avatar