string StringToUpper(string strToConvert)
{
//change each element of the string to upper case
for(unsigned int i=0;i<strToConvert.length();i++)
{
strToConvert[i] = toupper(strToConvert[i]);
}
return strToConvert;//return the converted string
}
string StringToLower(string strToConvert)
{
//change each element of the string to lower case
for(unsigned int i=0;i<strToConvert.length();i++)
{
strToConvert[i] = tolower(strToConvert[i]);
}
return strToConvert;//return the converted string
}
วันจันทร์ที่ 13 กรกฎาคม พ.ศ. 2552
C Convert cases of strings (tolower/toupper)
Check input is number or not
#include// Need ctype.h for isdigit and isalnum functions. #include int main(void) { char var[10]; // This is the variable to store input. int i = 0; int varisnum = 0; // Is var all numbers? 1 for yes, 0 for no. scanf("%s", var); while (isalnum(var[i]) != 0) { // Loop until it a character is not alpha-numeric. if (isdigit(var[i]) != 0) { // Is var[i] a numeric digit? varisnum = 1; } else { varisnum = 0; break; // If we encounter a non-numeric character, there is no need to keep looping, so just break out. } i++; // Move counter to the next element. } if (varisnum == 0) printf("Input was not all numbers.\n"); else printf("Input was all numbers.\n"); return 0; }
C scanf(), scanf_s()
| c | Character. When used with scanf() functions, specifies single-byte character; when used with wscanf() functions, specifies wide character. White-space characters that are ordinarily skipped are read when c is specified. To read next non–white-space single-byte character, use %1s; to read next non–white-space wide character, use %1ws. | Pointer to char when used with scanf() functions, pointer to wchar_t when used with wscanf() functions. | Required. Size does not include space for a null terminator. |
| C | Opposite size character. When used with scanf() functions, specifies wide character; when used with wscanf functions, specifies single-byte character. White-space characters that are ordinarily skipped are read when C is specified. To read next non–white-space single-byte character, use %1s; to read next non–white-space wide character, use %1ws. | Pointer to wchar_t when used with scanf functions, pointer to char when used with wscanf() functions. | Required. Size argument does not include space for a null terminator. |
| d | Decimal integer. | Pointer to int. | No. |
| i | Decimal, hexadecimal, or octal integer. | Pointer to int. | No. |
| o | Octal integer. | Pointer to int. | No. |
| u | Unsigned decimal integer. | Pointer to unsigned int. | No. |
| x | Hexadecimal integer. | Pointer to int. | No. |
| e, E, f, g, G | Floating-point value consisting of optional sign (+ or –), series of one or more decimal digits containing decimal point, and optional exponent ("e" or "E") followed by an optionally signed integer value. | Pointer to float. | No. |
| n | No input read from stream or buffer. | Pointer to int, into which is stored number of characters successfully read from stream or buffer up to that point in current call to scanf functions or wscanf() functions. | No. |
| s | String, up to first white-space character (space, tab or newline). To read strings not delimited by space characters, use set of square brackets ([ ]), as discussed in scanf() Width Specification. | When used with scanf() functions, signifies single-byte character array; when used with wscanf() functions, signifies wide-character array. In either case, character array must be large enough for input field plus terminating null character, which is automatically appended. | Required. Size includes space for a null terminator. |
| S | Opposite-size character string, up to first white-space character (space, tab or newline). To read strings not delimited by space characters, use set of square brackets ([ ]), as discussed in scanf() Width Specification. | When used with scanf() functions, signifies wide-character array; when used with wscanf functions, signifies single-byte–character array. In either case, character array must be large enough for input field plus terminating null character, which is automatically appended. | Required. Size includes space for a null terminator. |
Set your own date and time format ...
วันจันทร์ที่ 1 มิถุนายน พ.ศ. 2552
C Language on Unix FAQs
- I am trying to use my Windows PC at home to telnet to charlie.it.uts.edu.au and use FTP to transfer my files across for compiling on the university's UNIX computers. I find that with the C language my text editors (I have tried both Wordpad and Notepad) are inserting illegal whitespace characters making my code uncompilable. The message likes: ... : warning: invalid white space character in directive This problem is due to the fact that the DOS-based and UNIX based text editers treat a line of text differently. In the DOS-based text editor, the end of a line is marked by two characters ('\n' and '\r') while the UNIX based text editor use only '\n'. You can get rid of the extra white space using tr (translate characters) tool in UNIX. Syntax used as follows: cat yourCfile.c | tr -d '\015' > yourNewCfile.c Then you can compile your new C file using cc.
- What does the error message "... : warning: newline not last character in file" mean and how to fix it? This error is due to the fact the last line of the program is not terminated by a new line. cc does care about white spaces while gcc doesn't care. Terminate any text line by return in Wordpad/Notepad editor.