473,796 Members | 2,658 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

number of characters in a string

Hi,
Is there a function in C that computes the number of characters in a
string.
I want to be able to compute the number of chars in the string '1 3 4
5' by suppressing the ' ', so length should be 4 instead of 7...
Thanx
Sam
Nov 13 '05 #1
6 3322
berthelot samuel wrote:
Is there a function in C that computes the number of characters in a
string.
Yes, it's strlen(). Don't forget #include <string.h>.
I want to be able to compute the number of chars in the string '1 3 4
5' by suppressing the ' ', so length should be 4 instead of 7...


What is the exact text of the problem you have to solve for your course?
My guess is that you are taking the wrong approach.

Best regards,

Sidney

Nov 13 '05 #2

"Sidney Cadot" <si****@jigsaw. nl> schrieb im Newsbeitrag
news:bp******** **@news.tudelft .nl...
berthelot samuel wrote:
Is there a function in C that computes the number of characters in a
string.


Yes, it's strlen(). Don't forget #include <string.h>.
I want to be able to compute the number of chars in the string '1 3 4
5' by suppressing the ' ', so length should be 4 instead of 7...


What is the exact text of the problem you have to solve for your course?
My guess is that you are taking the wrong approach.


Can it be, that the OP has wide char strings?
In this case wcslen() is what he/she needs

Just a wild guess
Robert
Nov 13 '05 #3
sa************* *@voila.fr (berthelot samuel) wrote:
Hi,
Is there a function in C that computes the number of characters in a
string.
I want to be able to compute the number of chars in the string '1 3 4
5' by suppressing the ' ', so length should be 4 instead of 7...


There's no such standard function, but it's easy to write one
yourself. Just lookup the various is...() functions declared
in ctype.h and do something like:

#include <ctype.h>

int strGraphCount( const char *s )
{
int res = 0;
for( ; *s; ++s )
if ( isgraph( *s ) )
++res;
return res;
}

HTH
Regards
--
Irrwahn
(ir*******@free net.de)
Nov 13 '05 #4
Robert Stankowic wrote:
"Sidney Cadot" <si****@jigsaw. nl> schrieb im Newsbeitrag
news:bp******** **@news.tudelft .nl...
berthelot samuel wrote:

Is there a function in C that computes the number of characters in a
string.


Yes, it's strlen(). Don't forget #include <string.h>.

I want to be able to compute the number of chars in the string '1 3 4
5' by suppressing the ' ', so length should be 4 instead of 7...


What is the exact text of the problem you have to solve for your course?
My guess is that you are taking the wrong approach.

Can it be, that the OP has wide char strings?
In this case wcslen() is what he/she needs


Perhaps (that would be a problem for the course in advanced C
programming, I presume).

The OP's question smells like homework - I guess that's what I am trying
to say.

Regards,

Sidney

Nov 13 '05 #5
Irrwahn Grausewitz <ir*******@free net.de> wrote in message news:<ge******* *************** **********@4ax. com>...
sa************* *@voila.fr (berthelot samuel) wrote:
Hi,
Is there a function in C that computes the number of characters in a
string.
I want to be able to compute the number of chars in the string '1 3 4
5' by suppressing the ' ', so length should be 4 instead of 7...
There's no such standard function, but it's easy to write one
yourself. Just lookup the various is...() functions declared
in ctype.h and do something like:

#include <ctype.h>

int strGraphCount( const char *s )
{
int res = 0;


I'd go with size_t, rather than int.
for( ; *s; ++s )
if ( isgraph( *s ) )
if ( isgraph( (unsigned char) *s ) )
++res;
return res;
}

HTH
Regards


--
Peter
Nov 13 '05 #6
ai***@acay.com. au (Peter Nilsson) wrote:
Irrwahn Grausewitz <ir*******@free net.de> wrote:

<snip>
#include <ctype.h>

int strGraphCount( const char *s )
{
int res = 0;


I'd go with size_t, rather than int.

That'd be better design, yes.
for( ; *s; ++s )
if ( isgraph( *s ) )


if ( isgraph( (unsigned char) *s ) )

Uck, that was a real mistake, thanks for correction.
++res;
return res;
}


Regards
--
Irrwahn
(ir*******@free net.de)
Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1541
by: Ken Fine | last post by:
Suppose I have strString. I want to determine the length of strString in # of characters, and write that number of characters on a subsequent line as a dummy character, say a hypen. In other words, I want to be able to dynamically create the following presentation in ASCII: Title title title title title title -------------------------
6
3121
by: Nick | last post by:
Hi, How can I check if a number exists by itself in this string by using the RegExp object? --- var mystring = "11,111,01,011"; var match = "1"; var re = new RegExp( match );
7
3004
by: Raphi | last post by:
Hi, I'm trying to clean up a large database in Access. I have one field for address, which needs to be broken up into Street Number, Street Name, and Street Label (St., Road, etc.) The problem is that the data is very dirty. So some addresses will be standard "456 XYZ Road," while others won't have a number and will just say "XYZ Industrial Park," meaning I can't just use Instr to search for the first space because sometimes the...
8
2476
by: shumaker | last post by:
I see other posts where some say fields that will hold a number with leading zeros should be stored as text instead of numbers. This is very inefficient though, as a string of digit characters takes up much more memory than a 32bit or even a 64bit int. Storing as a number still allows for formatting when displaying the number, correct? I am creating a table and would like opinions regarding this please.
2
634
by: John S | last post by:
I have created an event handler that is trying to format each number as it is typed. Example If the first char is 1 = output is $1.00 second char is 3 = output is $13.00 next is 5 = $135.00 next is 7 = $1,3576.00 Here is my code but I cannot get it to work. public static void ReturnFormattedNumber(object sender,
3
2091
by: Jason | last post by:
If I've got a string like x = " Intel(R) PRO/1000 CT Network Connection" How do I get x = "Intel(R) PRO/1000 CT Network Connection" I basically want to make a comparison to Intel(R) PRO/1000 CT Network Connection, since the numeric part seems to change per system.
8
20750
by: Preben Randhol | last post by:
Hi If I use len() on a string containing unicode letters I get the number of bytes the string uses. This means that len() can report size 6 when the unicode string only contains 3 characters (that one would write by hand or see on the screen). Is there a way to calculate in characters and not in bytes to represent the characters. The reason for asking is that PyGTK needs number of characters to set the width of Entry widgets to a...
6
2014
by: androoo | last post by:
Hello all, I have a string for example : strTest = "a lineof text (60) witha number in it" I need to extract the number from between the brackets, the postion of the number in brackets is never the same... So in the above example i need to extract 60
7
11339
by: Sharkie | last post by:
I need a regular expression which will evaluate to false if number of consecutive characters (non-whitespace) exceeds certain number (10 in this example). For example, I have this function: function test() { var sValue="short unusuallyLongAndWayTooLongString short2"; var regEx=/\S{10,}/; return regEx.test(sValue);
8
2590
by: Alexandros H. Halatsis | last post by:
Let's say I have a cstring: char str = "Hello world"; and I have a needle: char n = "wo"; I find that the needle starts at the 5+1 position of the string. How am I going to copy the characters up to this point, using the <cstringheader? -- Using <string(c++ string) it is possible in two ways: 1) for (int counter = 0; i < str.find(n); i++) tmp += str;
0
9685
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
9533
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10461
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10239
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...
0
10019
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
5447
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4122
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
3736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.