473,788 Members | 2,800 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

program to find the length of the string manually

26 New Member
write a program to find the length of the string without using control structures and without using string.h header files???
Nov 9 '07 #1
9 5968
PuzzleC
9 New Member
write likeness a standart strlen function.

Sorry, it is strig.... string str; str.length(); ????
Nov 9 '07 #2
goelvivek
26 New Member
but that will use a control structure like
while(s!='\0')
l++;

and we don't have to use that

can is their is method so that we print string and then calculate the pointer position at that time
Nov 9 '07 #3
PuzzleC
9 New Member
see above... or convert to char* sl.c_str() and working as a character type
Nov 9 '07 #4
weaknessforcats
9,208 Recognized Expert Moderator Expert
write a program to find the length of the string without using control structures and without using string.h header files???
A C string is a char array with a \0 as the last character. Just count the characters in the array without counting the \0.
Nov 9 '07 #5
goelvivek
26 New Member
A C string is a char array with a \0 as the last character. Just count the characters in the array without counting the \0.
is their is no answer
Nov 11 '07 #6
PuzzleC
9 New Member
A C string is a char array with a \0 as the last character. Just count the characters in the array without counting the \0.
what is a type C string?
C++ string
approximate:
Expand|Select|Wrap|Line Numbers
  1. cpp
  2. class string
  3.  {
  4. private:                  
  5.  struct ChatT
  6.   {
  7.   ...
  8.   ...
  9.   blablabla;
  10.   };
  11.  ChatT *s;
  12.  int __length;  //<---- private mamber of class string
  13. public:
  14.  int length() { return this->__length; }
  15.  } ;
  16.  
Nov 11 '07 #7
Studlyami
464 Recognized Expert Contributor
when you say the length of the string, does string refer to a variable of string type or is this a character array?
Nov 11 '07 #8
chazzy69
196 New Member
If you are using character arrays e.g. char string[256], Then all you have to do is to read each element of the string from the start 0 or 1 until char string[x] = "\0".
You can do this in any loop.

For example, no error checking done

Expand|Select|Wrap|Line Numbers
  1. char string[256];
  2. int x;
  3.  
  4. cin>> string; // or physically assign each element e.g. string[1] = a and string[2] =b
  5.  
  6. while (string(x) <> "\0") {
  7. x++;
  8. }
  9.  
  10. cout<< (x-1); // this will give the last value of the string before string(x) = "\0"
  11.  
Hope this helps
Nov 12 '07 #9
weaknessforcats
9,208 Recognized Expert Moderator Expert
Thre previous 9 posts are riddled with errors:

Originally:
write a program to find the length of the string without using control structures and without using string.h header files???
Using the string.h header file indicates that the OP is interested in C strings which makes the following seem odd:

what is a type C string?
C++ string
approximate:

Code: ( text )
cpp
class string
{
private:
struct ChatT
{
...
...
blablabla;
};
ChatT *s;
int __length; //<---- private mamber of class string
public:
int length() { return this->__length; }
} ;
since there are no classes in C. This is a C++ example, and a poor one, since there is a C++ string class that is part of the C++ Standard Library. But all of that is irrelevant since the OP is interested in a C string.

A C string is an array of char with the last element a binary 0 ('\0').

when you say the length of the string, does string refer to a variable of string type or is this a character array?
The length of a C string is the number of characters counting from the start of the array to (but not including) the \0.

If you are using character arrays e.g. char string[256], Then all you have to do is to read each element of the string from the start 0 or 1 until char string[x] = "\0".
You can do this in any loop.

For example, no error checking done


Code: ( text )
char string[256];
int x;

cin>> string; // or physically assign each element e.g. string[1] = a and string[2] =b

while (string(x) <> "\0") {
x++;
}

cout<< (x-1); // this will give the last value of the string before string(x) = "\0"
There are a lot of errors in this code sample:
1) cin >> string will only fetch a string that has no whitespace in it. Usually, this means you fetch only one word of the input.

2) string[x] = "\0" won't compile. string[x] is a char. "\0" is an empty string. That is, an array with \0 as element[0]. In C and C++ the array name is the address of element[0] so this compares a char to the address of the char \0. The compiler will gag on this.

3) while (string(x) <> "\0") { won't compile. string(x) is a function call to the function string using the argument x. Proably you meant string[x]. However the <> won't work, again that compares a char to the addess of a char. See (2) above.

Now read my Post #5 again and follow the direction exactly.
Nov 12 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

5
8808
by: Ron Adam | last post by:
Hi, I'm having fun learning Python and want to say thanks to everyone here for a great programming language. Below is my first Python program (not my first program) and I'd apreciate any feedback on how I might do things differently to make it either more consice, readable, or faster. ie... are there better ways to do it in Python? It won't break any records for calculating pi, that wasn't my goal, learning Python was. But it might...
6
3020
by: Juho Saarikko | last post by:
The program attached to this message makes the Python interpreter segfault randomly. I have tried both Python 2.2 which came with Debian Stable, and self-compiled Python 2.3.3 (newest I could find on www.python.org, compiled with default options (./configure && make). I'm using the pyPgSQL plugin to connect to a PostGreSQL database, and have tried the Debian and self-compiled newest versions of that as well. I'm running BitTorrent, and...
2
1284
by: William Payne | last post by:
Hello, I am making a very simple and crude Makefile generator, that currently supports three different options: --project-name=<name_of_project> --source-files=<source_file_names_separated_by_commas> --resource-file=<name_of_resource_file_if_any> The first thing I do is to put all program arguments (excluding argv) in a std::vector of std::strings. Then I call a function to determine which options were passed to the program and their...
2
1557
by: frame | last post by:
Hi, The other day, I was experimenting with Predicates and STL and came across a gotcha with the following program. The objective of the program is to remove those strings, from a vector of strings, whose size is of atleast a specified length (here 5) and contains atleast a specified number of punctuation characters, excluding '@' (here 2). There are 3 classes: Length and NumberOfSpecialChars in 'Parameters' namespace (which are just...
0
7116
by: south622 | last post by:
I'm taking a beginning Java course and I'm stuck in week eight of a nine week course. If anyone could help me I would greatly appreciate it. This assignment was due yesterday and each day I go past the due date 10% of the grade is taken off. I think I'm coming down with the flu and my brain is just not processing this assignment. Here is the assignment: Modify the Inventory program by adding a button to the GUI that allows the user to move...
5
4023
by: jyck91 | last post by:
please~help me to write a program to convert the baseN(2-9) to base10 eg. 101(2)----->5(10) 1211(3)------->X(10) ...... P.S. i am really misunderstand the C programming, plz write the simple statments as you can
1
2665
by: Tony Freixas | last post by:
Hello, I'm trying to create a wrapper for a program. I want to execute program 'X' by running program 'Y', such that 'Y' appears to function pretty much like 'X' both in the way command line options are handled and the way input, output and error messages are reported. Program 'X' is a console application. I had it pretty well figured out, I thought, until I ran into a case where program X spit out ZIP code. In other words, I was...
4
2312
nexcompac
by: nexcompac | last post by:
Ok, I posted a similar post but now need to jump back into it. Here is what I have been able to clean up. I am using textpad and jbuilder. Still getting used to the whole java world and I am trying to compile a program using TextPad. Here is what I have so far. /* * Main.java * * Created on July 19, 2007, 5:54 PM *
6
3263
by: APEJMAN | last post by:
I know what I'm posting here is wired, but it's been 3 days I'm workin g on these codes, but I have no result I post the code here I dont wanne bother you, but if any one of you have time to read my program I appriciate it. the program suppose to print a message on the screen #include <iostream> #include <string>
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10172
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.