LinuxQuestions.org
Review your favorite Linux distribution.
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-19-2022, 09:47 AM   #1
FFX
LQ Newbie
 
Registered: Jul 2021
Distribution: Ubuntu
Posts: 27

Rep: Reputation: Disabled
How to check if there is no character in C


Hi,

I am coding a simple calculator in C, to practice. I make use of command line arguments. In my code if I find a . in the first argument, I want to check if there is no character right in front of the . , and right behind of the . Can someone tell me how in the following code I can achieve that? I want to put the solution between
Code:
if(point == 1){
		
		
		
	}
Code:
#include <stdio.h>
#include <string.h>


int main(int argc, char *argv[]){


int i, point;


for(i=0; i < strlen(argv[1]); i++){


	if((argv[1][i]) == '.'){

		point++;
		

	}
	
}

	
	if(point == 1){
		
		Please place your code here.
		
	}
	
		
	
return 0;

}

Thanks

Ben
 
Old 10-19-2022, 10:02 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 23,677

Rep: Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184
Code:
   int i, point=0; // make sure you initialize the variable point
...
   if ( (argv[1][i]) == '.' ) {
     point++;		
   }
If i>0 there is a character(s) left or in front of the decimal and if i<strlen(argv[1]) there is a character(s) to the right or behind the decimal point.

Last edited by michaelk; 10-19-2022 at 10:19 AM.
 
Old 10-19-2022, 10:24 AM   #3
FFX
LQ Newbie
 
Registered: Jul 2021
Distribution: Ubuntu
Posts: 27

Original Poster
Rep: Reputation: Disabled
Thanks.

I get it. Only I want to check if there are no charcters right in front of the . , and right behind the . So the right code for me is:

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


int main(int argc, char *argv[]){


int i, point;


for(i=0; i < strlen(argv[1]); i++){


	if((argv[1][i]) == '.'){

		point++;
		

	}
	
}

	
	if(i <= 1){
				
		printf("Succeeded!");
	}
	

return 0;

}
 
Old 10-19-2022, 10:50 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 23,677

Rep: Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184
If all you want to check if argv[1] equals a decimal point then:
Code:
if (strcmp(argv[1],".") == 0) printf("Succeedded\n");

Last edited by michaelk; 10-19-2022 at 10:52 AM.
 
1 members found this post helpful.
Old 10-19-2022, 11:35 AM   #5
FFX
LQ Newbie
 
Registered: Jul 2021
Distribution: Ubuntu
Posts: 27

Original Poster
Rep: Reputation: Disabled
Thanks!
 
Old 10-19-2022, 12:01 PM   #6
EdGr
Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 894

Rep: Reputation: 435Reputation: 435Reputation: 435Reputation: 435Reputation: 435
Using argv is fine if you want to learn how to parse command-line arguments. Be aware that the shell will interpret special characters. If you don't want that, read from the standard input instead.
Ed
 
Old 10-19-2022, 12:04 PM   #7
FFX
LQ Newbie
 
Registered: Jul 2021
Distribution: Ubuntu
Posts: 27

Original Poster
Rep: Reputation: Disabled
Oke
 
Old 10-19-2022, 01:18 PM   #8
FFX
LQ Newbie
 
Registered: Jul 2021
Distribution: Ubuntu
Posts: 27

Original Poster
Rep: Reputation: Disabled
I ran into another problem with the code I showed. On a certain spot in the code I expect i to be 0, but instead it is 1. I do not understand why. Please check:

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


int main(int argc, char *argv[]){


int i, point;


for(i=0; i < strlen(argv[1]); i++){


	if((argv[1][i]) == '.'){

		point++;
		

	}
	
}

Here i is 1 and not 0, what I would expect. Why is that and how can I resolve it?

	
	if(point == 1){
		
	
		
	}
	
		
	
return 0;

}
Thanks

Ben
 
Old 10-19-2022, 01:40 PM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 23,677

Rep: Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184
When the for loop completes i equals 1 if argv[1] is a single character.

Last edited by michaelk; 10-19-2022 at 01:44 PM.
 
Old 10-19-2022, 01:58 PM   #10
FFX
LQ Newbie
 
Registered: Jul 2021
Distribution: Ubuntu
Posts: 27

Original Poster
Rep: Reputation: Disabled
Yes this works with the code I showed, but I am now working with a piece of code that differs a bit. Please check:

Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>




int main(int argc, char *argv[]){


int i = 0, foutmelding=0, punt = 0;	




	if (argc < 2){
		
		printf("Dit is een rekenmachine\n");
		printf("Gebruik: Geef op de opdrachtregel 3 argumenten op\n");
		printf("Argument 1) Getal 2) -, +, x of / 3) getal\n");
		
			return 1;
			
			}
		
		
		if (argc > 1 && argc != 4){
			
			printf("Geef 3 argumenten op\n");
			
					return 1;
			}
	





//Meet hoe lang het eerste argument is

for(i=0; i < strlen(argv[1]); i++){

//Als er een punt zich bevind in het eerste argument geef de variabele punt dan een waarde van 1.
	if((argv[1][i]) == '.'){

		punt++;
			

	}
	
}



		if(punt == 1){
			
	
		
			if(i == 1) printf("There is no character on the left of the .");
			
	
			
			
	
		}
	

return 0;

}
 
Old 10-19-2022, 02:13 PM   #11
teckk
Senior Member
 
Registered: Oct 2004
Distribution: Arch
Posts: 4,448
Blog Entries: 5

Rep: Reputation: 1548Reputation: 1548Reputation: 1548Reputation: 1548Reputation: 1548Reputation: 1548Reputation: 1548Reputation: 1548Reputation: 1548Reputation: 1548Reputation: 1548
Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    int i = 0, foutmelding=0, punt = 0;	

    if (argc < 2){
        printf("Dit is een rekenmachine\n");
        printf("Gebruik: Geef op de opdrachtregel 3 argumenten op\n");
        printf("Argument 1) Getal 2) -, +, x of / 3) getal\n");
		
        return 1;
			
    }

    if (argc > 1 && argc != 4){
        printf("Geef 3 argumenten op\n");
        
        return 1;
    }

    //Meet hoe lang het eerste argument is

    for(i=0; i < strlen(argv[1]); i++){
        //Als er een punt zich bevind in het eerste argument geef de variabele punt dan een waarde van 1.
        if((argv[1][i]) == '.'){
            punt++;	
        }
	
    }

    if(punt == 1){
        if(i == 1) printf("There is no character on the left of the .");	
    }
	
    return 0;

}

//gcc mytest.c -o mytest
Code:
./mytest
Dit is een rekenmachine
Gebruik: Geef op de opdrachtregel 3 argumenten op
Argument 1) Getal 2) -, +, x of / 3) getal
What problem are you having?

Last edited by teckk; 10-19-2022 at 02:15 PM.
 
Old 10-19-2022, 02:23 PM   #12
FFX
LQ Newbie
 
Registered: Jul 2021
Distribution: Ubuntu
Posts: 27

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
When the for loop completes i equals 1 if argv[1] is a single character.
That makes sense. Thanks for the help.
 
Old 10-19-2022, 02:40 PM   #13
FFX
LQ Newbie
 
Registered: Jul 2021
Distribution: Ubuntu
Posts: 27

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by teckk View Post
Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    int i = 0, foutmelding=0, punt = 0;	

    if (argc < 2){
        printf("Dit is een rekenmachine\n");
        printf("Gebruik: Geef op de opdrachtregel 3 argumenten op\n");
        printf("Argument 1) Getal 2) -, +, x of / 3) getal\n");
		
        return 1;
			
    }

    if (argc > 1 && argc != 4){
        printf("Geef 3 argumenten op\n");
        
        return 1;
    }

    //Meet hoe lang het eerste argument is

    for(i=0; i < strlen(argv[1]); i++){
        //Als er een punt zich bevind in het eerste argument geef de variabele punt dan een waarde van 1.
        if((argv[1][i]) == '.'){
            punt++;	
        }
	
    }

    if(punt == 1){
        if(i == 1) printf("There is no character on the left of the .");	
    }
	
    return 0;

}

//gcc mytest.c -o mytest
Code:
./mytest
Dit is een rekenmachine
Gebruik: Geef op de opdrachtregel 3 argumenten op
Argument 1) Getal 2) -, +, x of / 3) getal
What problem are you having?
Hi,


Right before the following code I thought that i would be 0.
Code:
if(punt == 1){
        if(i == 1) printf("There is no character on the left of the .");	
    }
With this piece:
Code:
if(i == 1) printf("There is no character on the left of the .");
I thought that I would check if there was no charcater on the left of the . But it would be incorrect. I am to tired to continue, I will go on tommorow.

Ben
 
Old 10-19-2022, 03:32 PM   #14
michaelk
Moderator
 
Registered: Aug 2002
Posts: 23,677

Rep: Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184
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.
 
Old 10-19-2022, 03:45 PM   #15
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,389
Blog Entries: 1

Rep: Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665Reputation: 1665
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'
 
  


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 08:49 AM.

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