LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-20-2022, 12:27 PM   #16
FFX
LQ Newbie
 
Registered: Jul 2021
Distribution: Ubuntu
Posts: 27

Original Poster
Rep: Reputation: Disabled

Quote:
Originally Posted by michaelk View Post
Code:
for (i=0; i<1; i++) {
    printf("%d\n",i);
}
printf("%d\n",i);
Assuming your argv[1] is a "." which is a single character i.e. strlen(argv[1])=1

In simple terms, the first iteration of the loop i=0.
The second iteration, i is incrementated (i++) and then tested to see if it meets the conditional i=1 and 1<1 is false and the loop is exited.

And therefore at the posted code i=1.
Clear, thanks.
 
Old 10-20-2022, 12:30 PM   #17
FFX
LQ Newbie
 
Registered: Jul 2021
Distribution: Ubuntu
Posts: 27

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
An example might give some ideas.
Code:
    const char *arg= argv[1];
    size_t len= strlen(arg);
    size_t nDot= 0;
    size_t iFirstDot, iLastDot;
    size_t i;
    for (i=0; i<len; ++i) {
        if (arg[i]=='.') {
            ++nDot;
            if (nDot==1) iFirstDot= i;
            iLastDot= i;
        }
    }
    printf ("'%s': len=%d; %d dot(s) found\n", arg, (int)len, (int)nDots);
    if (nDot>=1) {
        if (iFirstDot>0) printf ("Before the first dot there is a '%c'\n", arg[iFirstDot-1]);
        if (iLastDot+1<len) printf ("After the last dot there is a '%c'\n", arg[iLastDot+1]);
    }
}
Example:
Code:
./FFX a.bcd.e
'a.bcd.e': len=7; 2 dot(s) found
Before the first dot there is a 'a'
After the last dot there is a 'e'
Why do you make use of size_t? I don't get that.
 
Old 10-20-2022, 03:35 PM   #18
michaelk
Moderator
 
Registered: Aug 2002
Posts: 23,677

Rep: Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184
size_t is an unsigned integer data type used to represent the size of objects i.e. the length of a string. It can also be a loop variable since they are usually >= 0.
 
1 members found this post helpful.
Old 10-20-2022, 03:36 PM   #19
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,387
Blog Entries: 1

Rep: Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665
Don't hesitate to use google.
https://stackoverflow.com/questions/...is-size-t-in-c
https://cplusplus.com/reference/cstring/size_t/
 
Old 10-20-2022, 03:41 PM   #20
FFX
LQ Newbie
 
Registered: Jul 2021
Distribution: Ubuntu
Posts: 27

Original Poster
Rep: Reputation: Disabled
Thanks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing a file character by character with a bash builtin command (script). stf92 Linux - Newbie 4 06-30-2012 09:41 PM
Find and Replace character/special character from the file MyRelam Red Hat 8 05-21-2012 01:52 AM
Bash scripting: parsing a text file character-by-character Completely Clueless Programming 13 08-12-2009 10:07 AM
To know the function on checking whether a character is ascii or unicode character. murugesan Programming 2 01-23-2009 02:07 PM
about wide character and multiple byte character George2 Programming 5 05-23-2006 02:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration