If the string is input from stdin:

scanf(" %s %d", R, &n);

then is it possible to get the length of R as a by product from scanf as it already has scanned through the input? As you can see there can be spaces in front and after words and there is a int input. Is it possible to refer to like give me the length you scanned for token[0], token[1] etc. Like in perl where lots of things are caught and stored implicitly in $ variables and also there are some variables like 'errno' in C, I was wondering whether there are any tricks I might be unaware of.

Thanks.

>>is it possible to get the length of R as a by product from scanf
No.

commented: Best answer I've seen all day +4
commented: actually, it's not a very good answer -1

You could pad your format string with %n conversions, which would give you a length with a minimal calculation.

You could pad your format string with %n conversions, which would give you a length with a minimal calculation.

how would that help?

That doesn't work

1 2 HelloWorld 3
len = 1
Press any key to continue . . .

int main(){	
	char R[255] = {0};
int s, e, n;
scanf(" %n%s%n %d", &s, R, &e, &n);
int len = e - s;
cout << "len = " << len << '\n';
return 0;
	}

That doesn't work

int main(){	
	char R[255] = {0};
int s, e, n;
scanf(" %n%s%n %d", &s, R, &e, &n);
int len = e - s;
cout << "len = " << len << '\n';
return 0;
	}

Pay attention! This is not the C++ forum

Pay attention! This is not the C++ forum

True, but you failed to answer the question, most likely because you don't know the answer any more than I do.

commented: Let me show you how to give negative reputation: Pay attention! Don't be lame! -2

@AD:

That doesn't work

1 2 HelloWorld 3
len = 1
Press any key to continue . . .

int main() {	
  char R[255] = {0};
  int s, e, n;
  scanf(" %n%s%n %d", &s, R, &e, &n);
  int len = e - s;
  cout << "len = " << len << '\n';
  return 0;
}

Pay attention! This is not the C++ forum

True, but you failed to answer the question, most likely because you don't know the answer any more than I do.

It seems to me that it's working correctly, with your input: 1 2 HelloWorld 3 ,
but remember: spaces are delimiters.

First a string is read: "1" (length = 1).
Then an integer is read: 2 .
The remainder stays in the input buffer.

This code fragment illustrates better what I mean:

#include <stdio.h>

int main( void )
{
  char R[81];
  int s, e, n, len;
  scanf( " %n%80s%n %d", &s, R, &e, &n );
  len = e - s;
  printf( "String entered = \"%s\"\tLength = %d\n", R, len );
  printf( "Integer entered = %d\n", n );
  return 0;
}

Sample runs:

1 2 HelloWorld 3
String entered = "1" Length = 1
Integer entered = 2

Hello 89
String entered = "Hello" Length = 5
Integer entered = 89

@AD & @Salem:
Doesn't there have to be a check which prevents an array boundary overrun?
For example:

char str[15];
scanf( "%10s", str );

for reading 10 characters at maximum.

@Salem:
Are you using C99 ?

int s, e;
scanf(" %n%s%n %d", &s, R, &e, &n);
int len = e - s; /* variable declaration after statement */

True, but you failed to answer the question, most likely because you don't know the answer any more than I do.

While it is human to err, one of the trends of a decent programmer is to pay attention to details. By posting rushed C++ code in the C forum, when we know you can do it in C, shows your carelessness about answering any question or asking for the same matter, but rather the eagerness to post quick and often. Specially, If you feel you are right.

commented: How the hell cares? -6
commented: Doesn't deserve neg rep. +8
commented: N/A +9

>>It seems to me that it's working correctly, with your input:
What I meant was that it doesn't answer the OPs question of how to get the string length without calling strlen() (or similar function).

>>Doesn't there have to be a check which prevents an array boundary overrun?
Yes, there should be, but its not required. You can let scanf() scribble all over memory if you want to.

Well, what does "%n" do in scanf()? I couldn't find any reference to it here. And it seems to not do anything in the code that's been posted in this thread either. But if I enter
HelloWorld 1 2
Then the string length is correct, for some odd reason.

While it is human to err, one of the trends of a decent programmer is to pay attention to details. By posting rushed C++ code in the C forum, when we know you can do it in C, shows your carelessness about answering any question or asking for the same matter, but rather the eagerness to post quick and often. Specially, If you feel you are right.

Answer by Ancient Dragon: Negative reputation with the illuminating phrase:

How the hell cares? - Ancient Dragon

If you meant "Who the hell cares?" I just did, didn't I? If not clear: "I care!", at least enough to call you on.

*shrug*
I just threw in some crumbs to show the general idea of how the problem was solvable.

Tarting it up for security and suitable for compiling with whatever C.X.Y compiler was left as an exercise for the readers :)

commented: You know what it is said about "Do not throw pearls before swine". But crumbs are OK. ;) +8
commented: Oh, okay, maybe I "overreacted" a bit. +8
commented: "tarting it up" ... that will be my new phrase +7
commented: I left my English to English dictionary somewhere... +4

Well, what does "%n" do in scanf()? I couldn't find any reference to it here.

http://web.archive.org/web/20050207005628/http://dev.unicals.com/papers/c89-draft.html#4.9.6.2:

n No input is consumed. The corresponding argument shall be a pointer to integer into which is to be written the number of characters read from the input stream so far by this call to the fscanf function. Execution of a %n directive does not increment the assignment count returned at the completion of execution of the fscanf function.

commented: Too bad you did not post for him his famous "Google auto-assistant helper" +8

Well, what does "%n" do in scanf()? I couldn't find any reference to it here. And it seems to not do anything in the code that's been posted in this thread either. But if I enter
HelloWorld 1 2
Then the string length is correct, for some odd reason.

Perhaps you should read the thread before replying? Tux4Life gave a beautifully detailed explanation just a few posts earlier, that even the most inexperienced newbie can understand.

and seriously, cplusplus.com is a nice reference to point new programmers to, just as a simplistic reference to get them started, but it clearly is not a definitive or complete specification. you should know this.

If only by virtue of your longevity, reputation, post and post-solved counts, newer programmers assume you are one of the top C/C++ "experts" on this site, and often take your words as literal truth. Whether you like it or not. So you should at least refrain from asking incredulous, noobtastic questions before doing some your own nominal investigation using credible sources. And quit with the silly sniping at Aia. Her tone may annoy you, but you should be more careful about posting C++ in a C forum, considering it's a major issue here.

#include<stdio.h>


int main()
{
 string a;
a="sdfsdfsdfsdfsdfsdf";

int b= findlength(a);
printf("length=%d",b);
}

int findlength(string a)
{
char * p=a;
int cnt=0;

if(*p!=null)
{*p++;
cnt++;
}

return cnt;
}
commented: Don't post code, and certainly not when it's rubbish. -2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.