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.

S.SAHA said:   3 years ago
This function return values that are as follows;

if Return value < 0 then it indicates str1 is less than str2.
if Return value > 0 then it indicates str2 is less than str1.
if Return value = 0 then it indicates str1 is equal to str2.

Rathna suresh said:   6 years ago
What is the meaning of *char?
(2)

Abhishek Singh said:   7 years ago
@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;
}

Sree harsha said:   7 years ago
Can anyone tell why illegal action is committing in dev compiler? Please help me.

Adrija said:   7 years ago
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.

Sanju said:   8 years ago
As far as I know. We can't modify a string literal. Then how its getting modified without giving any error?

Can anyone explain?

Kathir said:   8 years ago
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;
}

Moni said:   8 years ago
What means for the strnset? and where to use strnset?

Avnish Patel said:   8 years ago
@Tejaswi you are right,

Executing this program, compiler gives following error.

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

Usha said:   9 years ago
What is the difference between strnset and strcset?


Post your comments here:

Your comments will be displayed after verification.