Need matlab coding for the given c program
2 次查看(过去 30 天)
显示 更早的评论
#include <stdio.h>
int main()
{
char a[100];
int i;
i=0;
scanf("%c",&a[i]);
while(a[i]!='$')
{
i++;
scanf("%c",&a[i]);
}
a[i]='\0';
i=0;
while(a[i]!='\0')
{
printf("%c",a[i]);i++;
}
return 0;
}
3 个评论
回答(2 个)
darova
2020-3-30
try this
i = 1;
a{1} = '1';
while ~strcmp(a(i),'&')
str = input('','s');
i = i + 1;
a{i} = str;
end
6 个评论
Walter Roberson
2020-4-18
Note that using input() like that is not the same as the C code. The C code retrieves one character at a time from standard input, and finds the first '$' character, leaving standard input positioned immediately after the '$'. The loop with input() on the other hand does the equivalent of getline() each time and checks whether the input line has at least one '$' character. This is a very different requirement.
Remember, input() with 's' option fetches a line. You store the entire line into a{i}. You then test whether that entire line ~= '$', which is a vector test. The test fails if any element of the vector is 0, which would occur if any element of a{i} did equal '$'
Walter Roberson
2020-3-31
MATLAB cannot code that. scanf() reads from standard input, but MATLAB does not have standard input.
If you had a fileid of an opened file, then
s = fscanf(fid, '%c%[^$]$', 2);
fprintf('%s', s);
This has the same behaviour as the C code: it extracts at least one character, and up to but excluding a $ character, and consumes the $ character leaving the pointer right after it; and it outputs the extracted string to the display without any newline.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!