Holla Tech - Learn

The sprintf and sscanf Functions
 

A formatted string can be created with the sprintf() function. This is useful for building a string from other data types.

For example: 

#include <stdio.h>
int main()
{
  char info[100];
  char dept[ ] = “HR”;
  int emp = 75;
  sprintf(info, “The %s dept has %d employees.”, dept, emp);
  printf(“%s\n”, info);

  return 0;

 

Another useful function is sscanf() for scanning a string for values. The function reads values from a string and stores them at the corresponding variable addresses.
For example:

#include <stdio.h>
int main()
{
  char info[ ] = “Snoqualmie WA 13190”;
  char city[50];
  char state[50];
  int population;
  sscanf(info, “%s %s %d”, city, state, &population);
  printf(“%d people live in %s, %s.”, population, city, state);

  return 0;

 


The string.h Library
 

The string.h library contains numerous string functions.
The statement #include <string.h> at the top of your program gives you access to the following:
strlen(str) Returns the length of the string stored in str, not including the NULL character.
strcat(str1, str2) Appends (concatenates) str2 to the end of str1 and returns a pointer to str1.
strcpy(str1, str2) Copies str2 to str1. This function is useful for assigning a string a new value.

The program below demonstrates string.h functions:

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

int main()
{
  char s1[ ] = “The grey fox”;
  char s2[ ] = ” jumped.”;
   
  strcat(s1, s2);
  printf(“%s\n”, s1);
  printf(“Length of s1 is %d\n”, strlen(s1));
  strcpy(s1, s2);
  printf(“s1 is now %s \n”, s1);

  return 0;

 

Additional string.h functions include:
strncat(str1, str2, n) Appends (concatenates) first n characters of str2 to the end of str1 and returns a pointer to str1.
strncpy(str1, str2, n) Copies the first n characters of str2 to str1.
strcmp(str1, str2) Returns 0 when str1 is equal to str2, less than 0 when str1 < str2, and greater than 0 when str1 > str2.
strncmp(str1, str2, n) Returns 0 when the first n characters of str1 is equal to the first n characters of str2, less than 0 when str1 < str2, and greater than 0 when str1 > str2.
strchr(str1, c) Returns a pointer to the first occurrence of char c in str1, or NULL if character not found.
strrchr(str1, c) Searches str1 in reverse and returns a pointer to the position of char c in str1, or NULL if character not found.
strstr(str1, str2) Returns a pointer to the first occurrence of str2 in str1, or NULL if str2 not found.

NOTE!
Do you know any other string.h function? Share your discoveries in the comments section below.

Converting a String to a Number
 

Converting a string of number characters to a numeric value is a common task in C programming and is often used to prevent a run-time error.
Reading a string is less error-prone than expecting a numeric value, only to have the user accidentally type an “o” rather than a “0” (zero).

The stdio.h library contains the following functions for converting a string to a number:
int atoi(str) Stands for ASCII to integer. Converts str to the equivalent int value. 0 is returned if the first character is not a number or no numbers are encountered.
double atof(str) Stands for ASCII to float. Converts str to the equivalent double value. 0.0 is returned if the first character is not a number or no numbers are encountered.
long int atol(str) Stands for ASCII to long int. Converts str to the equivalent long integer value. 0 is returned if the first character is not a number or no numbers are encountered.

The following program demonstrates atoi.

#include <stdio.h>
int main()
{
  char input[10];
  int num;
   
  printf(“Enter a number: “);
  gets(input);
  num = atoi(input);

  return 0;

 

NOTE!
Do you know any other string.h function? Share your discoveries in the comments section below.

Array of Strings
 

A two-dimensional array can be used to store related strings.
Consider the following statement which declares an array with 3 elements, each holding 15 characters:

char trip[3][15] = {
  “suitcase”,
  “passport”,
  “ticket”
}; 

 

Although the string lengths vary, it is necessary to declare a size large enough to hold the longest string. Additionally, it can be very cumbersome to access the elements.
Referring to trip[0] for “suitcase” is error-prone. Instead, you must think of the element at [0][0] as ‘s’, the element at [2][3] as ‘k’, and so on.

An easier, more intuitive way to deal with a collection of related strings is with an array of pointers, as in the following program:

char *trip[ ] = {
  “suitcase”,
  “passport”,
  “ticket”
};

printf(“Please bring the following:\n”);
for (int i = 0; i < 3; i++) {
  printf(“%s\n”, trip[ i ]);

 

Because each element can vary in length, the array of string pointers has a more ragged structure as opposed to a two-dimensional grid structure.
With this approach, there is no limit to the string length. And more importantly, items can be referred to by a pointer to the first character of each string.

NOTE!
Keep in mind that a declaration like char *items[3]; only reserves space for three pointers; the actual strings are being referenced by those pointers.

BACK NEXT

CLICK ON THE BUTTON BELOW TO GO TO THE C MAIN COURSE PAGE. 

C MAIN COURSE PAGE

 


© License: All Rights Reserved 


CONTACT HOLLA TECH – LEARN SUPPORT