Need matlab coding for the given c program

#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 件のコメント

darova
darova 2020 年 3 月 30 日
You are declaring a variable but not assigning any value (no initialization). How should it work?
leo john
leo john 2020 年 3 月 30 日
Kindly compile it online compiler it will work.input is type any text or line until dollar sign is given it will display the same text .similar coding is needed in matlab
James Tursa
James Tursa 2020 年 3 月 31 日
@darova: The scanf line is assigning a value to a[i]

サインインしてコメントする。

回答 (2 件)

darova
darova 2020 年 3 月 30 日

0 投票

try this
i = 1;
a{1} = '1';
while ~strcmp(a(i),'&')
str = input('','s');
i = i + 1;
a{i} = str;
end

6 件のコメント

leo john
leo john 2020 年 3 月 31 日
Thanks for replying me darova .Above code works only when '&' sign is given in the beginning of the new line, means string comapare function compares each string with '&' but i need character by character comparison so that given any number of lines or text can be terminated by entering '&' or '$' in the middle of the text or lines. If there is any possible kindly do the needful thanks in advance.
leo john
leo john 2020 年 3 月 31 日
Answer fixed:
a{1} = ' ';
while a{i}~='$'
str = input('','s');
c = c + 1;
i=c;
a{c} = str;
end
disp(char(a));
darova
darova 2020 年 3 月 31 日
Use strfind
while ~isempty(strfind((a{i},'&')))
leo john
leo john 2020 年 4 月 18 日
Dear Darova sir,
Very many thanks for solving my issues.I would like to acknowledge you specially in my book publication work. I am writing a "Matlab programming for beginners" book kindly sent your designation and institution or place of work and city details if interested.
Thanks and Regards
Dr.F.Leo John
Walter Roberson
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 '$'
leo john
leo john 2020 年 4 月 18 日
Yes single line text should be terminated only by $ sign that is the program not by zero vector.Else err msg will be given

サインインしてコメントする。

Walter Roberson
Walter Roberson 2020 年 3 月 31 日

0 投票

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.

カテゴリ

ヘルプ センター および File ExchangeMATLAB Report Generator についてさらに検索

質問済み:

2020 年 3 月 30 日

コメント済み:

2020 年 4 月 18 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by