'Convert array of n strings to symbol *
I need help solving this task if anyone had a similar problem it would help me a lot.
The task is:
Write a function (not allowed
using other functions) that modifies the string s
by changing each character c to *. Prototype
The function is:void c2asterisk(char *s, char c);
Write a function (only use is allowed
function c2asterisk) which each on n given strings
modifies by changing each character c to *.
The prototype of the function is:void c2asterisk_arr(char **arr, int n, char c);
Write the main function you need to with
standard input load a string of five strings, a
then (using the c2asterisk_arr function)
modify each loaded string so that each
the digit in the string to be replaced by *. At the end,
print a string of (modified) strings.
I did like this:
#include <stdio.h>
#include <stdlib.h>
void c2asterisk(char *s, char c);
void c2asterisk_arr(char **arr, int n, char c);
int main()
{
char s[100][100];
int n;
printf("Enter how much strings you want:");
scanf("%d",&n);
while(n<1 || n>100)
{
printf("Enter how much strings you want:");
scanf("%d",&n);
}
for(int i=0;i<n;i++)
{
printf("%d.string:",i+1);
scanf("%s",s[i]);
}
for(int i=0;i<n;i++)
{
c2asterisk_arr(&s,n,'*');
}
for(int i=0;i<n;i++)
{
printf("%s",s[0][i]);
}
return 0;
}
void c2asterisk(char *s, char c)
{
int i;
for(i=0;s[i]!=0;i++)
{
s[i]=c;
}
}
void c2asterisk_arr(char **arr, int n, char c)
{
for(int i=0;i<n;i++)
{
c2asterisk(arr[0][i],'*');
}
}
The biggest problem for me is this double pointer on the arr in function, I just don't know what is passed to it.
The program just breaks, so I would ask someone to help me solve this task, I would be grateful to him.
Thanks in advance!
Best regards!
Solution 1:[1]
Just use memset;
for c2asterisk you can use the following:
void c2asterisk(char *s, char c)
{
memset(s,c,strlen(s));
}
and for c2asterisk_arr you can use the following:
void c2asterisk_arr(char **arr, int n, char c)
{
for(int i=0;i<n;i++)
{
memset(arr[i],c,strlen(arr[i]));
}
}
Solution 2:[2]
Write a function (not allowed using other functions) that modifies the string s by changing each character c to *.
You're supposed to change only the characters that are the same as character 'c' to a '*', not all characters to a '*'. In the code below I fixed this, and changed the call to c2asterisk_arr so that it requests to change all 'o' to a '*'.
The biggest problem for me is this double pointer on the arr in function, I just don't know what is passed to it.
It requires a pointer to a list of pointers to character arrays. This would be declared with "char **s" or "char *s[100]", but "char s[100][100]" has an incompatible memory layout and so you cannot use it.
Clearly this is homework, so have you been taught malloc() yet? The approach I would take would be to malloc the list of pointers and the individual strings, but you could get away with the "char *s[100]" approach and malloc only the individual strings. So I took that approach in order to not fix your code in a way that you might not understand.
If you haven't been taught malloc() yet then I'm not sure what you would do, unless perhaps you've been taught some alternative to scanf() which will allocate the buffer that it writes the string into.
#include <stdio.h>
#include <stdlib.h>
void c2asterisk(char *s, char c) {
int i;
for(i=0;s[i]!=0;i++) {
if (s[i] == c) s[i] = '*';
}
}
void c2asterisk_arr(char **arr, int n, char c) {
for(int i=0;i<n;i++) {
c2asterisk(arr[i], c);
}
}
int main() {
char *s[100];
int n;
printf("Enter how much strings you want:");
scanf("%d",&n);
while(n<1 || n>100) {
printf("Enter how much strings you want:");
scanf("%d",&n);
}
for(int i=0;i<n;i++) {
printf("%d.string:",i+1);
s[i] = malloc(100);
scanf("%s",s[i]);
}
c2asterisk_arr(s,n,'o');
for(int i=0;i<n;i++) {
printf("%s\n",s[i]);
}
return 0;
}
It does this:
Enter how much strings you want:5
1.string:one
2.string:two
3.string:three
4.string:four
5.string:five
*ne
tw*
three
f*ur
five
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Majid Hajibaba |
| Solution 2 | Coleo |
