C Programming - Strings - Discussion

Discussion Forum : Strings - General Questions (Q.No. 1)
1.
Which of the following function sets first n characters of a string to a given character?
strinit()
strnset()
strset()
strcset()
Answer: Option
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

Discussion:
24 comments Page 1 of 3.

Kiran said:   1 decade ago
Can any one explain me about "strnset"?

Krishna said:   1 decade ago
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:   1 decade ago
Why are using return?

RADHA said:   1 decade ago
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:   1 decade ago
How to add two number in C language without using "+" operator ?

Daniel H said:   1 decade ago
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:   1 decade ago
What is mean of strnset() ?

Prathyusha said:   1 decade ago
Please some one can explain what is "strnset".

IIIT,bas said:   1 decade ago
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:   1 decade ago
//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);
}
}


Post your comments here:

Your comments will be displayed after verification.