learnprogramming123

LEARN PROGRAMMING 123
your resources to programming and free tutition



Navigation

 
C
 
C++ soon here!
VB - soon here!

Search

 

 

 

 

Next Back

 

This lesson will show you how to:

  • write programs using atoi() and atof()
  • use the stdlib.h header file (standard library)
  • write programs using toupper() and tolower()
  • use the ctype.h header file (standard library)
  • use the strcpy() command
  • use the string.h header file (standard library)

    Q: What's the quickest way to get into a blondes pants?
    A: Pick them up off the floor.

Data Conversion

The following functions convert data types:

atof() converts an ASCII character to a double (to float in c++)
atoi() converts an ASCII character to an integer
atol() converts an ASCII to a long integer
itoa() converts an integer to a character array

Example:

/* convert a string to an integer */

#include <stdio.h>
#include <stdlib.h>

char string[] = “1234”;

int main(void)
{

    int sum;
    sum = atoi( string);
    printf(“Sum = %d\n”, sum);

    return 0;

}

Another example:

/* convert a string to an integer */

#include <stdio.h>
#include <stdlib.h>

int main(void)            // remember that the default return data is int
{

    int sum;
    char buff[20];

    printf(“Enter in an integer “);
    scanf (“%d”, &sum);
    printf(“As a string it is %s\n”, itoa(sum, buff, 10));

    return 0;

}

Note that itoa() takes three parameters:
• The integer to be converted
• A character buffer into which the resultant string is stored
• A radix value (10=decimal, 16=hexadecimal);

Built in functions for string handling:
string.h

The following macros are built into the file string.h

strcat           appends a string
strchr           finds first occurrence of a given character
strcmp         compares two strings
strcmpi        compares two strings, non-case sensitive
strcpy          copies one string to another
strlen           finds the length of a string
strlwr           converts a string to lowercase
strncat         appends n characters of string
strncmp       compares n characters of two strings
strncpy        copies n characters of one string to another
strrchr         finds last occurrence of given character in string
strrev          reverses string
strset          sets all characters of string to a given character
strspn         finds first sub string from given character set in string
strupr         converts string to uppercase

To copy one string to another

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

int main(void)
{

    const char yes = ‘y’

    char reply;
    char garment[] = “overcoat”;

    printf(“Is it raining outside? Answer y/n \n”);

    scanf(“%c”, &reply);

    if (reply == yes)
        strcpy (garment, “raincoat”);

    printf(“before you go out today take your %s\n”, garment);

    return 0;

}

To covert a string to uppercase

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

int main(void)
{

    char name[80];           /* declare an array of characters 0 – 79 */

    printf(“Enter in a name in lowercase\n”);
    scanf(“%s”, &name);
    strupr(name);
    printf(“The name in uppercase is %s\n”, name);

    return 0;

}

Sample program output

Enter in a name in lowercase
samuel
The name in uppercase in SAMUEL

Built in functions for character handling

The following character handling functions are defined in ctype.h

isalnum           tests for alphanumeric character
isalpha            tests for alphabetic character
isascii              tests for ASCII character
iscntrl              tests for control character
isdigit              tests for 0 to 9
isgraph            tests for printable character
islower            tests for lowercase
isprint              tests for printable character
ispunct             tests for punctuation character
isspace            tests for space character
isupper            tests for uppercase character
isxdigit             tests for hexadecimal
toascii              converts character to ASCII code
tolower            converts character to lowercase
toupper            converts a character to uppercase

To convert a string array to uppercase one character at a time using touper()

#include <stdio.h>
#include <ctype.h>

int main(void)
{

    char name[80];
    int loop;

    printf(“Enter in a name in lowercase\n”);
    scanf(“%s”, &name);

    for (loop = 0; name[loop] !=0; loop++)
        name[loop] = toupper(name[loop]);

    printf(“The name in uppercase is |%s\n”, name);

    return 0;

}

Sample program output

Enter in a name in lowercase
samuel
The name In uppercase in SAMUEL

Don't worry if this lesson seems too complicated. Pay attention only to the keywords that are summarized in the bullet points on top of this page.

A new section is under construction which will list all the standard libraries (as defined by ASCII) and will sort all keywords alphabetically and by library.

 

back to top          back to lesson 11         go to lesson 13

 

 

 

 






1