🌐
Physics Forums
physicsforums.com › other sciences › programming and computer science
How to search just the first two characters in a c-string? • Physics Forums
November 7, 2020 - It used to have strncpy(str1, str2, 2) to copy the first two characters out. BUT sadly VS flag error and insists on using strncpy_s(). strncpy_s() will not allow copy from a long string to a short one to truncate out the rest. Of cause, I can use loop to copy out a character at a time.
🌐
Reactgo
reactgo.com › home › how to get the first n characters of a string in c
How to get the first n characters of a string in C | Reactgo
August 5, 2023 - To get the first n characters of a string, we can use the memcopy() function in C.
Discussions

How to take first letter from C string? - Stack Overflow
I want to take the first letter from my firstname string variable and add it to the second letter of the lastname variable. My program so far is: #include main() { char firstname... More on stackoverflow.com
🌐 stackoverflow.com
c - Getting the first 10 characters of a string? - Stack Overflow
I have not been able to find any information with a web-search. Where should I be looking? More on stackoverflow.com
🌐 stackoverflow.com
function - How do I check the first two characters of my char array in C? - Stack Overflow
This is code to create a similar C library function atoi() without the use of any C runtime library routines. I'm currently stuck on how to check for the first two digits of the char array s to see More on stackoverflow.com
🌐 stackoverflow.com
First character of a string pointer.
I'm trying to have the first character of *p stored in c, or printed directly. I should not use any external libraries I wanna do it using pointer arithmatics and stuff... char *s = "this is a... More on tek-tips.com
🌐 tek-tips.com
4
0
December 14, 2007
🌐
Reddit
reddit.com › r/cprogramming › how can i get the first letter of a char* string?
r/cprogramming on Reddit: How can I get the first letter of a char* string?
April 25, 2022 -

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).

Top answer
1 of 6
4

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.

2 of 6
3

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.

🌐
Code with C
codewithc.com › code with c › c tutorials › c program to read a string and print the first two characters of each word in the string
C Program To Read A String And Print The First Two Characters Of Each Word In The String - Code With C
December 26, 2023 - > C Program to read a string and print the first two characters of each word in the string · C Tutorials · Last updated: December 26, 2023 2:20 pm · CWC · Share · 0 Min Read · SHARE · #include<stdio.h> #include<conio.h> main( ) { char s[100]; int i,l; clrscr( ); printf(“enter a string”); gets(s);l=strlen(s); for(i=0;i<l;i++) { if(s[i]!=’ ‘ && s[i]=’ ‘) { printf(“%c %c”,s[i],s[i+1]) i=i+2; while(s[i]!=’ ‘) i++; }} getch( ); } Share This Article ·
🌐
Quora
quora.com › How-do-I-find-the-first-common-character-and-the-second-in-a-string-in-C
How to find the first common character and the second in a string in C - Quora
... To find the first and second common characters in a string in C, interpret “common” as the first two characters that appear at least twice in the string (i.e., repeated characters).
🌐
Reactgo
reactgo.com › home › how to get the first character of a string in c
How to get the first character of a string in C | Reactgo
June 1, 2023 - Here is an example, that gets the first character p from the following string: #include <stdio.h> int main() { char fruit[4] = "pear"; printf("First character : %c \n", fruit[0]); }
Find elsewhere
🌐
Tek-Tips
tek-tips.com › home › forums › software › programmers › languages
First character of a string pointer. | Tek-Tips
December 14, 2007 - basicaly, you just need to do: char *s = "this is a test"; char c = *p; /* will store the first character of the string 's' in the character variable 'c'*/ printf("%s\n",c);
🌐
Quora
quora.com › How-should-I-print-the-1st-and-last-character-of-a-string-in-C
How should I print the 1st and last character of a string in C? - Quora
So you can access any element(character) of string by knowing its index. Now, as we need 1st character of string, means element present at index 0. We can get it by s[0]. For last element , we have to find tota...
🌐
Sololearn
sololearn.com › en › Discuss › 2923122 › getting-the-first-3-characters-in-c
Getting the first 3 characters in C | Sololearn: Learn to code for FREE!
Int main() { Char mot[5],mot_trois_der[3]; Printf(" entrez votre mot : "); Gets(mot); Strcpy (mot_trois_der,mot); Printf("%s",mot); Printf("\n"); Return 0; } ... Wait a minute, did you mean to copy first 2 characters or last 2 characters ...
🌐
Cplusplus
cplusplus.com › forum › general › 49644
Check first two characters in string - C++ Forum
if(first two characters are 70){int o=2;} else{o=3;} Thanks :D ... I would add a guard using 2 <= someString.length(), as operator[] doesn't like being used on empty strings. ... Alternatively, someString.substr(2)=="70", although it's a little more expensive.
🌐
Quora
quora.com › How-do-I-create-a-function-in-the-C-language-which-returns-the-first-letter-of-a-string
How to create a function in the C language which returns the first letter of a string - Quora
Now, as we need 1st character of string, means element present at index 0. We can get it by s[0]. For last element , we have to find total length of the string. by strlen(s) function present string.h header file.
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 272728 › how-to-extract-the-first-letter-of-a-c-string
c++ - How to extract the first letter of a c-string? [SOLVED] | DaniWeb
See my other post for a clue. ... Additional: Just copy the characters into the character array, adding \0 as the last character. Remember, in C as 'string' is simply an array of characters ending with \0.
Top answer
1 of 3
1

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.

2 of 3
1

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.

🌐
Cprogramming
cboard.cprogramming.com › c-programming › 77045-identifying-first-letter-word.html
identifying first letter of word
In a larger program, with many ... weekend? It's my personal favorite. ... In response to the original post, the previous reply of word[0] will tell you the first character in the string....
🌐
Cplusplus
cplusplus.com › forum › beginner › 116139
display 1st char of string - C++ Forum
I have been trying to find a way to only display the first letter of the middle string but have been having difficulties can someone pleases help · A couple other things, try not to use capital letters for your variable names, those usually imply classes. Also, try not to use system(). Here ...