Categories

แสดงบทความที่มีป้ายกำกับ C แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ C แสดงบทความทั้งหมด

วันจันทร์ที่ 13 กรกฎาคม พ.ศ. 2552

C Convert cases of strings (tolower/toupper)

Two functions, used to convert c++ strings from upper case to lowercase and vice versa. Each takes a string argument, and returns the converted string. Please note that parameters should be validated before passing them to the functions.
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
}

[+/-] Show Full Post...

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;
}

[+/-] Show Full Post...

C scanf(), scanf_s()

scanf() and scanf_s() - อ่านข้อมูลจาก standard input char a, b[20]; int i; scanf("%c %s %d", &a, b, &i); scanf_s("%c %s %d", &a, b, 20, i); /* scanf_s ทำมากัน buffer overflow เวลารับ string ต้องใสขนาดด้วย*/

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.

[+/-] Show Full Post...

Set your own date and time format ...

Does NOT need windows.h (Even though this uses a library call, it may not be so well known these days.) This demos using 'time.h' and a call to strftime( bufferStr, bufsize, formatString, pointer_to_struc_tm_info ) ... and provides sufficient info for you to set your own date and time formats ... or design a class dateTime /* * Doesn't use windows.h .... Uses a call to a NOT so well known * library function ... * strftime( bufferStr, bufsize, formatString, pointer_to_struc_tm_info ) */ #include #include int main () { time_t rawtime; time( &rawtime ); struct tm * timeinfo; timeinfo = localtime( &rawtime ); /* to access LOCAL date/time info*/ char buffer [128]; strftime( buffer, 128, "Time now is %I:%M%p", timeinfo ); printf( "%s ...\n\n", buffer ); strftime( buffer, 128, "%A %B %d, day %j of year %Y, %X %Z.", timeinfo ); printf( "%s\n\n", buffer ); strftime( buffer, 128, "%Y-%m-%d %X or if you like 'am/pm' it is %I:%M:%S %p", timeinfo ); printf( "%s\n\n", buffer ); strftime( buffer, 128, "%a %b %d, %y at %I:%M:%S%p", timeinfo ); printf( "%s\n\n", buffer ); /* Or ... get the 'bits' just the way you like ... */ strftime( buffer, 128, "%A %B", timeinfo ); printf( "%s", buffer ); /* print the fullDay fullMonth*/ strftime( buffer, 128, "%d", timeinfo ); printf( " %d, ", atoi(buffer) ); /* print the day number as an integer so NO leading '0' */ strftime( buffer, 128, "%Y at %I:%M:%S%p", timeinfo ); printf( "%s ", buffer ); /* print 'YYYY at hh:mm:ssAM/PM' */ getchar(); /* To keep Window open ... if needed. */ return 0; } /* %a Abbreviated weekday name * Thu %A Full weekday name * Thursday %b Abbreviated month name * Aug %B Full month name * August %c Date and time representation * Thu Aug 23 14:55:02 2001 %d Day of the month (01-31) 23 %H Hour in 24h format (00-23) 14 %I Hour in 12h format (01-12) 02 %j Day of the year (001-366) 235 %m Month as a decimal number (01-12) 08 %M Minute (00-59) 55 %p AM or PM designation PM %S Second (00-61) 02 %U Week number with the first Sunday as the first day of week one (00-53) 33 %w Weekday as a decimal number with Sunday as 0 (0-6) 4 %W Week number with the first Monday as the first day of week one (00-53) 34 %x Date representation * 08/23/01 %X Time representation * 14:55:02 %y Year, last two digits (00-99) 01 %Y Year 2001 %Z Timezone name or abbreviation CDT %% A % sign * The above specifiers whose description is marked with an asterisk (*) are locale-dependent. */

[+/-] Show Full Post...

วันจันทร์ที่ 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.

[+/-] Show Full Post...

Search