write a program to find the length of the string without using control structures and without using string.h header files???
9 5968
write likeness a standart strlen function.
Sorry, it is strig.... string str; str.length(); ????
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
see above... or convert to char* sl.c_str() and working as a character type
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.
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
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: - cpp
-
class string
-
{
-
private:
-
struct ChatT
-
{
-
...
-
...
-
blablabla;
-
};
-
ChatT *s;
-
int __length; //<---- private mamber of class string
-
public:
-
int length() { return this->__length; }
-
} ;
-
when you say the length of the string, does string refer to a variable of string type or is this a character array?
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 -
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"
-
Hope this helps
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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
|
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...
|
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
*
|
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>
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |