String in C – Set 2

In the last instructional exercise, I gave you an outline of the strings in C language. I educated you concerning the essential projects to print the components inside a string.

In the present instructional exercise, I will educate you regarding the most ideal approach to print the string on the screen. I will likewise enlighten you concerning the restriction of scanf() work with strings. So let’s begin.

String in C

Content control is one of the most every now and again utilized capacity in any language. In the last program, I have printed the string components by getting to them individually.

Anyway, in genuine programming, it is hard to run circles when you need to print some series of characters on screen.

To defeat this issue Dennis Ritchie presented %s group specifier. It ends up being exceptionally helpful while printing the string in a hurry. Let’s make one program to get it.

#include<stdio.h>
 
void main() 
{  
 char name[]="Justtechreview";
 printf("%s",name);
}

Output

Justtechreview

Above program is plain as day. I have just utilized %s position specifier to print the name string.

Perusing String from User

So far I educated you concerning printing the yield on the screen. Anyway taking contribution from the client to a string is similarly significant as well.

To play out this undertaking, the first answer that will hit your psyche is scanf() work. Indeed scanf() can be utilized to take the information. In any case, it isn’t ideal for this assignment. Let’s comprehend it with one program.

#include<stdio.h>
 
void main( ) 
{  
 char name[100];
 printf("Enter one wordn");
 scanf("%s",name);
 printf("%sn",name);
 printf("nEnter at least two wordsn");
 scanf("%s",name);
 printf("%s",name);
}

Output

Enter one word 
hello 
hello 

Enter at least two words 
hello world 
hello 
Press any key to continue . _ 

Clarification

As should be obvious I have utilized scanf() work multiple times. First time I have requested that the client enters just a single word. So scanf() work can play out that assignment effectively.

Be that as it may, in the second time I have requested that the client enters at any rate two words. However, all things considered, printf() has just printed “I”.

Well, the explanation for it is that scanf() capacity can just take the single word. It shuts the string when it establishes any clear space. To defeat this issue we need to utilize another capacity.

gets() work in C

This capacity is utilized to take the contribution from the client to the string. It conquers the issue of putting away just a single word.

With the use of this capacity, the client can store any number of words or even sentences. Let’s comprehend it with one program.

#include<stdio.h>
 
void main( ) 
{  
 char name[100];
 printf("Enter at least two wordsn");
 gets(name);
 printf("Now its working fine: %s",name);
}

Output

Enter at least two words 
hello world 
Now its working fine: hello world 
Press any key to continue..... 

In the above program, I have supplanted scanf() work with gets work. As should be obvious now everything is working fine.

puts() work in C

As its name recommends puts() work is a partner of gets(). It is utilized to print each string in turn on the screen. Recall that we can just utilize each string, in turn, to print it on the screen.

Then again printf() capacity can be utilized to print any number of strings. Let’s make one straightforward program to comprehend its working.

Leave a Comment

error: Alert: Content is protected!!