C Programming - Strings - Discussion

1. 

Which of the following function sets first n characters of a string to a given character?

[A]. strinit()
[B]. strnset()
[C]. strset()
[D]. strcset()

Answer: Option B

Explanation:

Declaration:

char *strnset(char *s, int ch, size_t n); Sets the first n characters of s to ch

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

int main(void)
{
   char *string = "abcdefghijklmnopqrstuvwxyz";
   char letter = 'x';

   printf("string before strnset: %s\n", string);
   strnset(string, letter, 13);
   printf("string after  strnset: %s\n", string);

   return 0;
}

Output:

string before strnset: abcdefghijklmnopqrstuvwxyz

string after strnset: xxxxxxxxxxxxxnopqrstuvwxyz


Kiran said: (Apr 14, 2011)  
Can any one explain me about "strnset"?

Krishna said: (Jul 17, 2011)  
int main(void)
{
char *string = "abcdefghijklmnopqrstuvwxyz";
char letter = 'x';

printf("string before strnset: %s\n", string);
strnset(string, letter, 13);
printf("string after strnset: %s\n", string);

return 0;
}

Anjaneyulu said: (Sep 14, 2011)  
Why are using return?

Radha said: (Oct 13, 2011)  
void main()
{
float a=0.7;
if(0.7>a)
printf("hi");
else

printf("bye");
}

Answer is hi and why?

I think, 0.7 is treat as double by default, isn't it?

Could anyone give me perfect answer for this?

Satish Rajnale. said: (Nov 4, 2011)  
How to add two number in C language without using "+" operator ?

Daniel H said: (Nov 20, 2011)  
strnset isn't a standard function. Consider using memset, which is a standard function.

Note also that string points to a string literal, which isn't guaranteed to be modifiable.

Consider using the following program:

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

int main(void)
{
char string[] = "abcdefghijklmnopqrstuvwxyz";

printf("string before memset: %s\n", string);
memset(string, 'x', 13);
printf("string after memset: %s\n", string);
return 0;
}

Selva said: (Jul 5, 2012)  
What is mean of strnset() ?

Prathyusha said: (Aug 24, 2012)  
Please some one can explain what is "strnset".

Iiit,Bas said: (Aug 24, 2012)  
strnset ia a function which sets first n characters of a string to a given character.
For ex:
Look at the above program.
In that example step4:
we gave the letter as 'x'.
And in step 6:we gave n value as'13'.
So 'x' will replace that first 13 characters.
Hope you can understand prathyusha........

Gaurav said: (Nov 19, 2012)  
//This is not working please have a look
#include<stdio.h>
#include<conio.h>
int main()
{
int p=1;
char name[15];
while(p)
{
printf("Enter your name\n");
gets(name);
printf("Hello %s\n",name);
printf("Enter p\n");
scanf("%d",&p);
}
}

Anandrao Sakhare said: (Mar 14, 2014)  
#include <stdio.h>
#include <string.h>

main()
{
char str[]="%f%d%c\n\t%d";
int i;
for(i=0;i<10;i++)
printf("%c",str[i]);
}

Output :
%f%d%c
%d

Gourav said: (Jun 26, 2014)  
Why no output is coming for this code ?

#include<stdio.h>
int xstrlen (char *);
void xstrcat (char*,char*,int );
main ()
{
char source []="hello";
char target [20]="world";
int length= xstrlen(target);
xstrcat(target,source,length);
printf("source string=%s",source);
printf("target string=%s",target);
}

int xstrlen (char *t)
{
int len=0;
while (*t!='\0')
len++;
return (len);

}

void xstrcat (char *t,char *s,int l)

{

while (*s!='\0')
{
t=t+l;
*t=*s;
t++;
s++;
}
*t='\0';
}

Bhargav said: (Feb 27, 2015)  
But in strings strnset is not a predefined functions. By compiling the above program it showing some error.

Can you explain?

Tejaswi said: (Jun 28, 2015)  
This program gives run time error while executing.

Usha said: (Jul 21, 2015)  
What is the difference between strnset and strcset?

Avnish Patel said: (Oct 28, 2015)  
@Tejaswi you are right,

Executing this program, compiler gives following error.

In function 'main': Undefined reference to 'strnset'

Moni said: (Mar 10, 2016)  
What means for the strnset? and where to use strnset?

Kathir said: (Mar 22, 2016)  
It sets the portion of characters in a string to given character.
String before strnset: abcdefghijklmnopqrstuvwxyz

String after strnset: xxxxxxxxxxxxxnopqrstuvwxyz

The user want to replace abcdefghijklm by using 'x' and given position is 13 .so 'a' to 'm' is replaced by 'x'. finally got xxxxxxxxxxxxxnopqrstuvwxyz.
and one thing

Use this program:

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

int main(void)
{
char string[70] = "abcdefghijklmnopqrstuvwxyz";
char letter = 'x';
printf("string before strnset: %s\n", string);
strnset(string, letter, 13);
printf("string after strnset: %s\n", string);
return 0;
}

Sanju said: (Aug 19, 2016)  
As far as I know. We can't modify a string literal. Then how its getting modified without giving any error?

Can anyone explain?

Adrija said: (Mar 24, 2017)  
Note: GCC Compiler (32 Bit Linux Platform).

#include<stdio.h>
#include<string.h>
int main()
{
char s[90]="endl";
printf("%s",strcat (s,strcat(s,s)));
return 0;
}

Why it shows segment fault?

Please, anyone explain me.

Sree Harsha said: (Jun 12, 2017)  
Can anyone tell why illegal action is committing in dev compiler? Please help me.

Abhishek Singh said: (Jul 1, 2017)  
@ALL.

#include<bits/stdc++.h>
using namespace std;
main(){
char s[]="abhishek";
char p[]="abhiaana";
strnset(s,'l',3);
memset(p,'o',2);
cout<<s<<' '<<p;
}

Rathna Suresh said: (Jul 5, 2018)  
What is the meaning of *char?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.