GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Feb-2006, 14:08
Red83 Red83 is offline
Junior Member
 
Join Date: Feb 2006
Posts: 30
Red83 is on a distinguished road

strlen concept question?


Hey everyone...just a quick question. I'm given the expression:

Code:
strlen(S+strlen(S))

Isn't there something wrong with this? I'm just not sure and need
some advice. I'm just not sure what its value is.

Thanks in advance for any help
  #2  
Old 10-Feb-2006, 14:53
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: strlen concept question?


Quote:
Originally Posted by Red83

Code:
strlen(S+strlen(S))

I'll answer the question with a few questions of my own. Answers to my questions would enable someone to answer your question.



1. What is the data type of S in this program?

2. What is the data type of the argument of the C Standard Library function strlen(), and is the data type of S compatible with the argument of strlen()??

3. What is the data type of the function strlen()?

4. Is the value of the expression S+strlen(S) defined. (That is: is it a legal expression?)

5. If the answer to my question 4 is "yes", then what is the data type of the result of evaluating that expression?

6. If the answer to my question 4 is "yes", then is the answer to my question number 5 something that is compatible with the argument of the strlen()?

If you get all of the way through to question 6 and the answer to question 6 is "yes", then it is a legal C expression.

Whether there is something "wrong" depends on you, since a value judgment of "wrong" depends on what you expect it to be (or whoever it was who asked the question in the first place) .

For example, suppose the answers lead to a "yes" for number 6. Are there any particular things that you have to know about the data in the program to guarantee that the value of the expression is defined? (And the only data item is S, so the question is "What do you have to know about the value of S to be able to know the value of the expression?")

Regards,

Dave
  #3  
Old 10-Feb-2006, 15:25
Red83 Red83 is offline
Junior Member
 
Join Date: Feb 2006
Posts: 30
Red83 is on a distinguished road

Re: strlen concept question?


I'm sorry about the vagueness of my question. I apologize. To expand a little.
I'm given a string S in a character array. This is all I'm given(those words exactly). I'm suppose to know if the expression:

strlen(S+strlen(S))

has a value of 0,1,will generate an error, or if the value will just depend on the contents of S. I agree that this question doesn't come with a lot of information. I guess you feel my pain though. I truly do appreciate any and all advice though.

Thanks
  #4  
Old 10-Feb-2006, 16:52
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: strlen concept question?


Quote:
Originally Posted by Red83
I'm sorry about the vagueness of my question.
.
.
.
I guess you feel my pain though.

I understood the question OK, I think.

And, yes, I realize that no one was born knowing this stuff, and it's all kind of confusing at times. (Not just at first, but, perhaps, especially at first.)


So, have you started on the list of questions? I tried to make a sequence that would lead to some discovery in a logical order rather than just saying yes/no at the bottom line and then trying to work out why.

I intended for you to answer the questions, not give me more information so that I could answer them.

For example the data type of the function strlen() is int, right? So, make up some char array, put a null-terminated sequence of chars and feed the elements of the expression to your compiler.

If the compiler gives some error or warning messages about the program, then you can investigate the cause. If the program compiles OK, then you can try to see what the value of the expression is. (Assign it to an int variable and print out the value.)

Here: I'll get you started

CPP / C++ / C Code:


#include <stdio.h>
#include <string.h>

int main()
{
  int expression_value;
  char S[100] = {"Hello"};

  printf("strlen(S) = %d\n", strlen(S));
  /* now build up the expression starting inside and working your
   * out
   */
  
  /* finally: expression_value = xxxxxx */

  /* printf("The value of the expression is %d\n", expresson_value); */

  return 0;
}

Now, if you are supposed to do this without a computer, then write the program anyhow, and pretend you are the computer. Do it a step at a time.

Regards

Dave

"We can face our problem.
We can arrange such facts as we have
with order and method."

Hercule Poirot
--- in Murder on the Orient Express
  #5  
Old 10-Feb-2006, 19:11
Guidelines Plz Guidelines Plz is offline
Junior Member
 
Join Date: Sep 2005
Posts: 87
Guidelines Plz is on a distinguished road

Re: strlen concept question?


Quote:
Originally Posted by Red83
I'm sorry about the vagueness of my question. I apologize.
This is why the Guidelines were written, so you won't waste time having to post more information and wait even longer for an answer
__________________

Please read http://www.gidforums.com/t-5566.html. They were written to help you create a request that is readable and has enough information we can actually tell what you need help with.
  #6  
Old 13-Feb-2006, 07:06
Red83 Red83 is offline
Junior Member
 
Join Date: Feb 2006
Posts: 30
Red83 is on a distinguished road

Re: strlen concept question?


Thank you Dave. With your help I was able to figure out the answer to my question, which was that the expression returns a 0 for any type of string that is entered. Thanks again for the help. I appreciate it.
  #7  
Old 13-Feb-2006, 08:25
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: strlen concept question?


Quote:
Originally Posted by Red83
I was able to figure out the answer to my question

"I love it when a plan comes together."
---Hannibal Smith (George Peppard, 1928-1994)
The A Team, 1983-1987


Regards,

Dave
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Check This New Searchengine Concept Out ! onauc Search Engine Optimization Forum 5 10-Feb-2006 11:23
non-member function question crq C++ Forum 1 03-Feb-2005 22:59
Simple question on arrays--please help! brookeville C++ Forum 16 18-Nov-2004 00:23
Repetition structure problem and question brookeville C++ Forum 17 29-Oct-2004 18:48

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:50.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.