How to take first letter from C string? - Stack Overflow
c - Getting the first 10 characters of a string? - Stack Overflow
function - How do I check the first two characters of my char array in C? - Stack Overflow
First character of a string pointer.
I need to compare a char* string letter by letter with another string in a for loop, but I can’t get access to the first index.
int buscar_letra(char *secreta, char **adivinada, char c){
int ocurrencias = 0;
int i;
int largo = strlen(secreta);
if(strchr(secreta,c)){
for(i=0;secreta[i];i++){
if(secreta[i]==c){
*adivinada[i]=c;
ocurrencias++;
}
}
}
return ocurrencias;
}Basically, is a function that get a secret word, a word fill with * and a letter, the function must compare the secret word with the letter and if a coincidence is found, the function must replace the letter in the ******** word, in the same index. A hangman game.(the parameters must be those).
As strings are array of characters, you need to take the first element from the array:
char firstname_initial;
firstname_initial = firstname[0]
Also note that since lastname and firstname are buffers, you don't need to pass a pointer to them in scanf:
scanf( "%s", firstname );
scanf( "%s", lastname );
And one last thing - scanf is a dangerous function and you should not use it.
Suppose the user types:
Michael
in response to the first prompt. The %c format reads the M; the %s format reads ichael without bothering to get any new data.
Also, you should not be passing &firstname or &lastname; you should be passing just firstname and lastname to scanf(). The difference is in the type; with the ampersand, you're passing a char (*)[256] which is not the same as the char * that scanf() expects. You get away with it, but 'get away with it' is the operative term.
Use a %s format (or, better, %255s format) for the two scanf() calls. Then pass firstname[0] and lastname to printf(). You might want to think about using tolower() from <ctype.h> on the first letter, and maybe on the last name too.
This is a reasonable approximation to a good program:
#include <stdio.h>
int main(void)
{
char firstname[256];
char lastname[256];
printf("What's your first name? ");
if (scanf("%255s", firstname) != 1)
return 1;
printf("What's your last name? ");
if (scanf("%255s", lastname) != 1)
return 1;
printf("Your school.edu e-mail address is: %c%[email protected]\n",
firstname[0], lastname);
return 0;
}
It avoids quite a lot of problems, one way or another. It is not completely foolproof, but most people won't run into problems with it.
Here is the method that I used:
string key = argv[1];
char c[3] = {key[0], key[1], '\0'};
string s = c;
printf("string: %s\n", s);
Given what you've provided in the question and from interpreting your meaning I can only assume that argv is a two dimensional array of either int or char.
string a = argv[1][0]; // so this has 1
string b = argv[1][1]; // this has 2 in it
You want c to be 12.
int[,] t = new int[2, 6] {{1,2,3,4,5,6},
{1,2,2,0,8,5}};
string a = Convert.ToString(t[1,0]);
string b = Convert.ToString(t[1,1]);
string c = a+b; // "12"
from here you can convert it to a number if you need it as 12.
Hi,
Is there a way to see if in the string "hello world" that 'h' is the first character in the string? Using something like strcmp or similar.
Thank you!
Because str[0] is a character, not a string. And function strcpy must use two string (char *) as argument.
To resolve your problem, you can set temp[0] = str[0]; or use sprintf function, or use strncpy function
But you must allocate temp array before you want to use it.
Regarding char *temp6=NULL;
Here you're trying to tell the compiler, "Hey ! set up temp6 as a pointer to char but don't allocate memory for it. "
If you do something like strcpy(temp,str); later you will get segmentation fault because you are trying to write to memory you don't own.
In your case you didn't go so far to see the segmentation fault, the compiler caught another error which is mentioned by the other answerer, ie in the line :
strcpy(temp6,str[0]);
where the compiler expected second argument to be char* but you passed char.
You have to allocate memory to the pointer first, or point it to an array.
It is also possible to de-allocate the memory you have allocated for a pointer.
char* temp= malloc(sizeof(char) * 10) ; // allocating memory
temp='\0'; // In essence de-allocating the memory.
Below is a complete example.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main()
{
char* temp= malloc(sizeof(char) * 10) ; //allocating memory enough to store 10 chars
char* str="abcdefgh"; // play safely - always store less than 10 characters. Consider that \0 will be appended to the end.
strcpy(temp,str);
printf("Temp : %s\n",temp);
char* str1="ijklmnop";
strcpy(temp,str);
printf("Temp : %s\n",temp);
temp='\0'; // In essence deallocating the memory.
printf("Temp : %s\n",temp);
strcpy(temp,str);
printf("Temp : %s\n",temp);
}
will give you the below result.
Temp : abcdefgh
Temp : abcdefgh
Temp : (null)
Segmentation fault (core dumped)
Also make sure that free(temp6) is placed at the end of your code to clean
up the memory.
Though this doesn't directly answer your question, hope it will be useful.