Question:
Character String Help in C?
anonymous
2007-11-30 09:08:01 UTC
I have a problem with character string manipulation, im trying to prompt the user to enter a float (i.e. a numerical value) but the user can decide to close the program if he/she enters "exit". I tried using the isdigit and isalpha functions, but they only return a value of 0, which would force me to re-prompt the user. Any suggestions on how to immediately quit the program once the user enters "exit" ?
Three answers:
BigRez
2007-11-30 09:11:23 UTC
Read in the character array (string) value and then test it being equal to exit. If not, then use the atof() function to try and convert the value to a float.



For example,



char myChar[20];

float myVal=0.0;

scanf("%s",myChar);



if (strcmp(myChar,"exit")) {

/* input is NOT equal to exit */

myVal = atof(myChar);

...



myVal would then contain the numerical value of their input according to the conversion rules of atof().



(massiv_x: a "string" is a character array terminated by a null character; what you've given is actually in error because you've defined 27 bytes, BUT because the value is enclosed in double-quotes, it stores 28 bytes (including the null terminator.) You can't use any string functions with that variable as it doesn't contain space for the string terminator \0. Perhaps just a typo. ;)
massiv_x
2007-11-30 09:23:22 UTC
if i recall...the isdigit and isalpha function take only a single value...



in c, strings are actually just character arrays...you know:

char thisstring[27] = "twenty-seven bytes of data.";



in order for your method to work, youll have to pass in every letter on by one...this wont work for your cause tho..

when you pass the values into isalpha or isdigit you have to use the array index with the string variable..if you dont, youre just passing in a memory pointer value..this may work for you, you just gotta access the array (string) directly from memory using pointers..
?
2016-10-10 03:05:52 UTC
use nested for loops bool isVowel = fake; for (int a = 0; a < 10; a++) { __isVowel = fake __for (int b = 0; b < 5; b++) __{ ____if (a == b) { isVowel = real } __} __if (isVowel == fake) {print a} } that's of course sudo code in spite of the incontrovertible fact that it might supply you the main dazzling concept. we examine each and every character interior the be conscious to the vowels and if it does not experience any then we print a. The underscores symbolize areas to make this code extra readable


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...