string - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

string

alaatrashalaatrash Posts: 34Member
hello.. I have s:string; and I want to know if the first char is number
because I want to make a condition.. so I wrote

.....................................
uses wincrt;
var first,s:string;
BEGIN
write('F(x)= ');
readln(s);
first:= copy(s,1,1);
if not (first in ['1'..'100']) then
.................................

but an error happend... : (ordinal expresion expected)..
what's wrong and how can I know if the first char is a number in another way if there is ......and thanks
ALA

Comments

  • zibadianzibadian Posts: 6,349Member
    : hello.. I have s:string; and I want to know if the first char is number
    : because I want to make a condition.. so I wrote
    :
    : .....................................
    : uses wincrt;
    : var first,s:string;
    : BEGIN
    : write('F(x)= ');
    : readln(s);
    : first:= copy(s,1,1);
    : if not (first in ['1'..'100']) then
    : .................................
    :
    : but an error happend... : (ordinal expresion expected)..
    : what's wrong and how can I know if the first char is a number in another way if there is ......and thanks
    : ALA
    :
    In can only be used with char and integer types, not with strings. Also how can 1 character (first) equal 3 characters ('100'). Getting the first character of a string can also much faster, because a string is basically an array of char. Thus you can use the array notation to get the first character:
    [code]
    first := s[1];
    [/code]
    where first is of the char type. Then you can use the in operator:
    [code]
    if not (First in ['1'..'9']) then // No strings, but chars
    [/code]
  • PP2005PP2005 Posts: 111Member
    : [code]
    : if not (First in ['1'..'9']) then // No strings, but chars
    : [/code]


    I guess you forgot to include '0', so the code would be:

    [code]
    if not (First in ['0'..'9']) then // No strings, but chars
    [/code]

  • king0deuking0deu Posts: 29Member
    : hello.. I have s:string; and I want to know if the first char is number
    : because I want to make a condition.. so I wrote
    :
    : .....................................
    : uses wincrt;
    : var first,s:string;
    : BEGIN
    : write('F(x)= ');
    : readln(s);
    : first:= copy(s,1,1);
    : if not (first in ['1'..'100']) then
    : .................................
    :
    : but an error happend... : (ordinal expresion expected)..
    : what's wrong and how can I know if the first char is a number in another way if there is ......and thanks
    : ALA
    :

    Why don't you use this function: [code]val(first,x,code)[/code]

    with x is a variable of integer (or real)
    code will be 0 if the first is a number, and 1 if is isn't

    That's simple!
  • zibadianzibadian Posts: 6,349Member
    : : [code]
    : : if not (First in ['1'..'9']) then // No strings, but chars
    : : [/code]
    :
    :
    : I guess you forgot to include '0', so the code would be:
    :
    : [code]
    : if not (First in ['0'..'9']) then // No strings, but chars
    : [/code]
    :
    :
    I didn't forget, because alaatrash's code also started at 1, and not at 0.
  • PP2005PP2005 Posts: 111Member
    : I didn't forget, because alaatrash's code also started at 1, and not at 0.
    :

    You're right, but his code also ended with 100, so I figured he was trying to check if it was a number not only between 1 and 9... :-)
  • alaatrashalaatrash Posts: 34Member
    thank you all.. I'll take your advices

  • alaatrashalaatrash Posts: 34Member
    : : hello.. I have s:string; and I want to know if the first char is number
    : : because I want to make a condition.. so I wrote
    : :
    : : .....................................
    : : uses wincrt;
    : : var first,s:string;
    : : BEGIN
    : : write('F(x)= ');
    : : readln(s);
    : : first:= copy(s,1,1);
    : : if not (first in ['1'..'100']) then
    : : .................................
    : :
    : : but an error happend... : (ordinal expresion expected)..
    : : what's wrong and how can I know if the first char is a number in another way if there is ......and thanks
    : : ALA
    : :
    :
    : Why don't you use this function: [code]val(first,x,code)[/code]
    :
    : with x is a variable of integer (or real)
    : code will be 0 if the first is a number, and 1 if is isn't
    :
    : That's simple!
    :
    but what is the use of the variable x?

  • zibadianzibadian Posts: 6,349Member
    : : : hello.. I have s:string; and I want to know if the first char is number
    : : : because I want to make a condition.. so I wrote
    : : :
    : : : .....................................
    : : : uses wincrt;
    : : : var first,s:string;
    : : : BEGIN
    : : : write('F(x)= ');
    : : : readln(s);
    : : : first:= copy(s,1,1);
    : : : if not (first in ['1'..'100']) then
    : : : .................................
    : : :
    : : : but an error happend... : (ordinal expresion expected)..
    : : : what's wrong and how can I know if the first char is a number in another way if there is ......and thanks
    : : : ALA
    : : :
    : :
    : : Why don't you use this function: [code]val(first,x,code)[/code]
    : :
    : : with x is a variable of integer (or real)
    : : code will be 0 if the first is a number, and 1 if is isn't
    : :
    : : That's simple!
    : :
    : but what is the use of the variable x?
    :
    :
    That's the converted value. See help files for more info.
  • king0deuking0deu Posts: 29Member
    : : : : hello.. I have s:string; and I want to know if the first char is number
    : : : : because I want to make a condition.. so I wrote
    : : : :
    : : : : .....................................
    : : : : uses wincrt;
    : : : : var first,s:string;
    : : : : BEGIN
    : : : : write('F(x)= ');
    : : : : readln(s);
    : : : : first:= copy(s,1,1);
    : : : : if not (first in ['1'..'100']) then
    : : : : .................................
    : : : :
    : : : : but an error happend... : (ordinal expresion expected)..
    : : : : what's wrong and how can I know if the first char is a number in another way if there is ......and thanks
    : : : : ALA
    : : : :
    : : :
    : : : Why don't you use this function: [code]val(first,x,code)[/code]
    : : :
    : : : with x is a variable of integer (or real)
    : : : code will be 0 if the first is a number, and 1 if is isn't
    : : :
    : : : That's simple!
    : : :
    : : but what is the use of the variable x?
    : :
    : :
    : That's the converted value. See help files for more info.
    :

    No need to care 'bout it, the important thing is that code is the condition to check if first is a number
    And you should take Zibadian's advice,too
  • zibadianzibadian Posts: 6,349Member
    : : : : : hello.. I have s:string; and I want to know if the first char is number
    : : : : : because I want to make a condition.. so I wrote
    : : : : :
    : : : : : .....................................
    : : : : : uses wincrt;
    : : : : : var first,s:string;
    : : : : : BEGIN
    : : : : : write('F(x)= ');
    : : : : : readln(s);
    : : : : : first:= copy(s,1,1);
    : : : : : if not (first in ['1'..'100']) then
    : : : : : .................................
    : : : : :
    : : : : : but an error happend... : (ordinal expresion expected)..
    : : : : : what's wrong and how can I know if the first char is a number in another way if there is ......and thanks
    : : : : : ALA
    : : : : :
    : : : :
    : : : : Why don't you use this function: [code]val(first,x,code)[/code]
    : : : :
    : : : : with x is a variable of integer (or real)
    : : : : code will be 0 if the first is a number, and 1 if is isn't
    : : : :
    : : : : That's simple!
    : : : :
    : : : but what is the use of the variable x?
    : : :
    : : :
    : : That's the converted value. See help files for more info.
    : :
    :
    : No need to care 'bout it, the important thing is that code is the condition to check if first is a number
    : And you should take Zibadian's advice,too
    :
    The in-operator is much faster and takes less memory than the conversion. This might become very important if you need to perform this check very often, or the memory load must be as low as possible.
Sign In or Register to comment.