• Home
  • About
  • Contact Us

More Scientific

  • Computer Networks
  • Operating Systems
  • Programming Languages
  • Project Management
  • Software Development
  • Software Testing

Strings, scanf, Standard Library Functions In C Language

April 25, 2019 by admin

Strings, scanf, Standard Library Functions In C Language

In this post, I will discuss about strings, Program to Demonstrate Printing of a String, %s Format Specifier, scanf( ), Standard Library String Functions in C, strlen( ) function, strcpy( ) function, Function to concatenate a string, Function of comparison of two stings in C, Structure (Grouping of data type together in C), and Declaring a Structure in C programming.

What are Strings?

Strings are group of characters which can be stored in character array, just like group of integers can be stored in integer array. Strings are treated by as character arrays in many languages. Strings are used in programs to get desired output from text.

A string is one dimensional array terminated by null (‘\0’).

Char name [] = {‘M’, ‘O’, ‘R’, ‘E’, ‘J’, ‘O’, ‘U’, ‘R’, ‘N’, ‘A’, ‘L’, ‘S’, ‘\0’}

Last character in a string will always be ‘\0’ and each character will be taking one byte of memory. ‘\0’ is called as null characters and ASCII value is 0/ Null character is not equivalent to 0. Below mentioned figure explains about the memory allocation. Characters of array will be stored in contiguous location. Having a null character at the end of the array helps the function to determine where the string or a collection of characters ends.

Strings will be very often used therefore below mentioned is an example from where initializing a string will be easier while initializing we don’t need to enter null character because C will be doing it automatically.

Char x [] = ”MORE JOURNALS” ;

Program to Demonstrate Printing of a String:

#include<stdio.h>

void main( )

{

char x[ ] = “MORE JOURNALS” ;

int i = 0 ;

while ( i <= 16 )

{

printf ( “%c”, X[i] ) ;

i++ ;

}

}

Output of the above mentioned program is :

MORE JOURNALS

The %s Format Specifier:

Special character % used in printf() is a format for printing a string. Below mentioned is a program:

#include<stdio.h>

void main( )

{

char name[30] ;

printf ( “Please enter your name ” ) ;

scanf ( “%s”, name ) ;

printf ( “My name is %s!”, name ) ;

}

Output for the program will be:

Enter your name MORE JOURNALS

My name is MORE JOURNALS

The scanf( ):

Two precautions to be taken upon the entering string using scanf() are as mentioned below:

(a) Length of entered stings should be exceeding the dimensions of character array. Because the compiler will not considering characters beyond the limits and because of which something important will be overwritten or missed.

(b) Scanf cannot read multi word strings. To overcome this barrier there is one more function called as gets.

#include<stdio.h>

void main( )

{

char x[25] ;

printf ( “Enter your Blog’s name ” ) ;

gets ( x ) ;

puts ( X ) ;

puts ( “is really very informative ” ) ;

}

And here is the output…

Enter your Blog’s name MORE JOURNALS

MORE JOURNALS

Is really very informative.

Standard Library String Functions in C:

FunctionUse
StrlenTo determine length of a string
StrlwrConverts a string to Lower Case
StruprConverts a string to Upper Case
StrcatAppends one string at the end of another.
strncatAppends first ‘n’ characters of a string at the end of another string.
strcpyCopies a string into another string.
strncpyCopies first ‘n’ characters of one string into another.
strcmpComparison two strings.
strncmpComparison first ‘n’ characters of two strings.
strcmpiCompares two strings without regard to case (“i” denotes that this function ignores case).
stricmpCompares two strings without regard to case (identical to strcmpi).
strnicmpCompares first ‘n’ characters of two strings without regard to case.
strdupDuplicates a string.
strchrFinds first occurrence of a given character in a string.
strrchrFinds last occurrence of a given character in a string.
strstrFinds first occurrence of a given string in another string.
strsetSets all characters of string to a given character.
strnsetSets first ‘n’ characters of string to a given character.
strrevReverses string.

What is strlen( ) in C:

A function to count the number of characters present in a strings. A program is written to explain the usage.

#include<stdio.h>

#include<string.h>

voidmain( )

{

char arr[ ] = “MORE JOURNALS ” ;

int len1,

len1 = strlen ( arr ) ;

printf ( “\nArray Length= %s length = %d”, arr, len1 ) ;

}

The output would be…

Array = MORE JOURNALS = 13

What is strcpy( ) function in C:

Content of one string is copied to another using strcpy function. Base address of source and destination string should be given to this function. Below mentioned is the program explaining copy functionality:

#include<stdio.h>

#include<string.h>

voidmain( )

{

char origin[ ] = “MORE JOURNALS ” ;

char destination[20] ;

strcpy ( desitination, origin ) ;

printf ( “\Origin String = %s”, origin ) ;

printf ( “\nDestination string = %s”, destination ) ;

}

Outcome of the above mentioned program is as mentioned below:

Origin string = MORE JOURNALS

Destination string = MORE JOURNALS

Function to concatenate a string:

String at origin is concatenated at the end of the target string. For example “MORE” and “JOURNALS” on concatenation is shown as “MORE JOURNALS ”. A program is written below explaining concatenation of string:

#include<stdio.h>

#include<string.h>

voidmain( )

{

char origin[ ] = “MORE” ;

char destination[30] = “JOURNALS” ;

strcat ( destination,origin ) ;

printf ( “\nOrigin string = %s”, origin ) ;

printf ( “\nDestination string = %s”, destination ) ;

}

Outcome of the above mentioned program is as mentioned below:

Origin string = MORE

Destination string = JOURNALS

Function of comparison of two stings in C:

Comparison of two sting to find out if the two strings are same or different strcmp function is used. Two strings are compared character by character until at least one character mismatches or till the end of the array whichever is occurred. If two strings are identical the value zero is returned. If strings are not identical then numeric difference between the ACII values of the first non matching pairs of character is shown.

/* Program to perform string comparison*/

#include<stdio.h>

#include<string.h>

void main( )

{

char string1[ ] = “Jam” ;

char string2[ ] = “Fan” ;

int i, j ;

i = strcmp ( string1, “Jam” ) ;

j = strcmp ( string1, string2 ) ;

printf ( “\n%d %d %d”, i, j, ) ;

}

Out come of the above mentioned program is as mentioned below:

0 4

For variable I we are storing the return value of two identical string comparison, the comparison was between “Jam” and “Jam” therefore the returned value is zero. In variable J we are storing the value for comparison of two unidentical strings “Jam” and “Fan” and the return values the numeric difference between ASCII value of “J” and ASCII vale of “F”. However difference of ASCII values is not important for us but more relevant is the result of the comparison.

Structure (Grouping of data type together in C):

Grouping together of number of data types is contained in structure. Data type might or might not be of same type. Below mentioned is program:

#include<stdio.h>

viod main( )

{

struct collection

{

char name ;

float salary ;

int age ;

} ;

struct collection b1, b2;

printf ( “\nEnter names, salary & age of 2 employees\n” ) ;

scanf ( “%c %f %d”, &b1.name, &b1.salary, &b1.age ) ;

scanf ( “%c %f %d”, &b2.name, &b2.salary, &b2.age ) ;

printf ( “\nAnd this is what you entered” ) ;

printf ( “\n%c %f %d”, b1.name, b1.salary, b1.age ) ;

printf ( “\n%c %f %d”, b2.name, b2.salary, b2.age ) ;

}

And here is the output…

Enter names, prices and no. of pages of 3 books

A 100.00 354

C 256.50 682

F 233.70 512

Declaring a Structure in C programming:

Below mentioned is an example of defining structure:

struct emp

{

char name ;

float salary ;

int age ;

} ;

Every variable of data type will consist of a character called name, a float variable called as salary and an integer variable called as age. General structure form is mentioned below:

struct <structure name>

{

structure element 1 ;

structure element 2 ;

structure element 3 ;

……

……

} ;

When new structure data type is defined one or more variables can be declared to be of that type. For e.g.

struct emp b1, b2, b3 ;

Statement mentioned above will be having space in memory. It makes available space to hold all the elements in the structure—in this case, 7 bytes—one for name, four for salary and two for age. These bytes are always in adjacent memory locations.

Filed Under: Programming Languages Tagged With: array, ASCII, character, code, coding, comparison, concatenate, data type, declaring, define, defining, elements, float, format, function, grouping, initializing, input, integer, memory, null, output, printing, program, programming, results, specifier, strcmp, strcpy, strlen, structure, tips, tutorial, variable

Project Management

Importance Of Planning In Project Management

Planning means “Deciding what has to done, Who has to do, when it has to done, how it to be done, Where it has be done. Planning is an intellectual process, which requires anyone to think before acting. To plan one would need objective, goal, way to perform and means of to perform. Planning would […]

Operating Systems

C Programming Under Linux – Zombies And Orphans Process

What is Linux? Linux operating system is clone of Unix operating system. Kernel of Linux was developed from scratch by Linus Torvalds with assistance from a loosely-knit team of programmers across the world on internet. It has got all the features that would be expected in a modern operating system. Unlike windows or Unix, Linux […]

Web Development

Website Testing Methods: Functionality, Usability, Interface

In this post, I will describe major web testing methods which are useful to test a website: Functionality Testing. Usability Testing. Interface Testing. Compatibility Testing. Performance Testing. Security Testing. 1) Functionality Testing Functionality testing involves testing of all the links shown are functional. Navigating to target pages. Test links will remain in the same page. Detail […]

Data Modeling

How To Identify Entity Type In Database Designing

In this post, I will discuss about how to identify Entity Type in Data Base Designing, Property of … [Read More...]

  • Software Development
  • Programming Languages
  • Project Management
  • Operating Systems
  • Computer Networks
  • Software Testing

Copyright © 2025 More Scientific