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?
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 2 of 3.
Anandrao Sakhare said:
1 decade ago
#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 :
#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:
1 decade ago
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';
}
#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:
1 decade ago
But in strings strnset is not a predefined functions. By compiling the above program it showing some error.
Can you explain?
Can you explain?
Tejaswi said:
1 decade ago
This program gives run time error while executing.
(1)
Usha said:
1 decade ago
What is the difference between strnset and strcset?
Avnish Patel said:
10 years ago
@Tejaswi you are right,
Executing this program, compiler gives following error.
In function 'main': Undefined reference to 'strnset'
Executing this program, compiler gives following error.
In function 'main': Undefined reference to 'strnset'
Moni said:
9 years ago
What means for the strnset? and where to use strnset?
Kathir said:
9 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;
}
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:
9 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?
Can anyone explain?
(1)
Adrija said:
8 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.
#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.
(1)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers