String to char with function - c

i want to get a string from the keyboard ,like "abcde" , and then insert it in a char,for example b which is char , but there is no function,like atoi(a) for integers.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char a[6];
char b;
scanf("%s",a);
printf("%s",a);
printf("\n");
b=a;
printf("%c",b);
return 0;
}

You cannot insert a string into a char. You can, however, convert the string to an array of chars. If this is what you are looking for, please see this answer

Related

Reversing a String in C. Why is this code not working?

I wrote a simple code to reverse a string and its somehow not returning any result. Could someone please help me understand the problem.
#include <stdio.h>
#include <string.h>
void reverse_character(char *s){
int i;
int p = strlen(s);
for(i=(p-1);i<0;i--){printf("%c",s[i]);}
}
int main(){
char name[20];
printf("Enter a name");
scanf("%s",name);
reverse_character(name);
}
i=(p-1);i<0;i--
Change it to
i=(p-1);i>=0;i--

Declaration char* not working with strcat()

This code compiles without errors, but upon opening the app, it says:
file.exe has stopped working
#include <string.h>
#include <stdio.h>
int main() {
char *a = 'Hello';
char *b = 'World';
strcat(a,b);
puts(a);
}
Where did I go wrong?
You need to allocate sufficient space and use double quote instead of single quote. You could use array.
#include <string.h>
#include <stdio.h>
int main() {
char a[20] = "Hello";
char b[10] = "World";
strcat(a,b);
puts(a);
}
Constant strings are not modifiable. This is a proper way to declare, initialize and modify a string buffer in C:
#include <string.h>
#include <stdio.h>
int main() {
char a[20];
char *b = "World";
strcpy(a,"Hello");
strcat(a,b);
puts(a);
return(0);
}
You can't do strcat on pointer of characters. You only can do strcat on array of characters .... Sorry for my precedent answer, look at the code below :
#include <string.h>
#include <stdio.h>
int main() {
char a [20];
char b[20];
strcpy(a,"Hello");
strcpy(b,"World");
strcat(a,b);
puts(a);
return(0);
}

Unexpected output of printf for a string in C

Case 1:
When I take string input, it successfully gives the output, writing this piece of code:
#include <stdio.h>
int main()
{
char *str;
scanf("%s",&str);
printf("%s",&str);
return 0;
}
Case 2:
On the other hand, it throws a Runtime Error for this snippet:
#include <stdio.h>
int main()
{
char *str;
scanf("%s",&str);
printf("%s",str);
return 0;
}
I found this thing peculiar, and want to know why it happens...
Thanks in advance.
None of those two cases are right.
Case 1 only worked because you got lucky, probably by giving a short string as input. Try something like "bfjabfabjkbfjkasjkvasjkvjksbkjafbskjbfakbsjfbjasbfjasbfkjabsjfkbaksbfjasbfkja" and you'll suffer a seg fault, most likely.
You should have a block of memory associated with str, either on the stack by declaring an array for it or on the heap malloc'ing memory for it.
And you shouldn't use the & operator.
So it would go like this:
#include <stdio.h>
int main()
{
char str[50]; // 50 is arbitrary
scanf("%s",str);
printf("%s",str);
return 0;
}
or like this:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char* str = malloc(50); // 50 is arbitrary
scanf("%s",str);
printf("%s",str);
free(str);
return 0;
}

Array with int and char in C

I need to put 3 strings on an array[3][3].
I tried to do it with pointers, but I only receive a single character.
#include <stdio.h>
int array[3][3]
char thing[5] = "thing";
main()
{
thing = array[0][0];
printf("%s", array[0][0];
}
Try this. With due respect your code absolutely incorrect and need many changes. You need to update your programming skills too.
#include <stdio.h>
#include <string.h>
char array[3][6]={0};
char *thing = "this";
main()
{
strcpy(array[0],thing);
printf("%s\n", array[0]);
}

weird behaviour when working with double pointers

I need help to understand why in this little program i cannot manipulate correctly pointers:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void change(char *s[][15]){
int i=0;
while(i<5){
if(s[i][0]=='B') s[i][0]='v';
i++;
}
}
/*My code is supposed to allocate dynamically 5 arrays of 15 chars each
(like tab[5][15])and then put a message on them and try to modify the messages.
In this particular case i'm trying to change the first letter of each string to 'V'.
I'm doing this little experience because of another program
in which i have difficulties accessing double arrays*/
int main(){
int i;
char **s;
s =malloc(5*sizeof(char*));
for(i=0;i<5;i++){
s[i]=malloc(15*sizeof(char));
sprintf(s[i],"Bonjour%d",i);
}
change(s);
for(i=0;i<5;i++){
printf("%s\n",s[i]);
}
return 0;
}
I was expecting :
Vonjour0
Vonjour1
Vonjour2
Vonjour3
Vonjour4
but I get :
Bonjour0
Bonjour1
Bonjour2
Bonjour3
Bonjour4
I'm testing this little code for another program and I don't get why the arrays don't change.
In my other program I can't access the double pointer or print the content.
so my question is : why in this program I can't modify the content of the arrays ?
Your change method needs to use "char** s" instead of char *s[][15]. This is because your method is expecting a pointer to a multi-dimensional array. This is immutable as a result, since your original data type for the string is a pointer to an array of strings (IE: An array of chars).
Hopefully that was clear.
It should be
char **change(char **s){
int i=0;
while(i<5){
if(s[i][0]=='B') s[i][0]='v';
i++;
}
return s;
}
You only need to change the function argument to char *s[].
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void change(char *s[]){
int i=0;
while(i<5){
if(s[i][0]=='B') s[i][0]='v';
i++;
}
}
int main(){
int i;
char **s;
s =malloc(5*sizeof(char*));
for(i=0;i<5;i++){
s[i]=malloc(15*sizeof(char));
sprintf(s[i],"Bonjour%d",i);
}
change(s);
for(i=0;i<5;i++){
printf("%s\n",s[i]);
}
return 0;
}
Program output:
vonjour0
vonjour1
vonjour2
vonjour3
vonjour4

Resources