Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
How can I check if a string contains a particular, given character?
Quite trivially, actually. Here is a sample snippet. This will locate first occurrence of a character in string.
#include <stdio.h>
#include <string.h>
int main()
{
const char* haystack = "self";
const char needle = 'l';
size_t len = strlen(haystack);
size_t i;
for(i = 0; i < len; i++) {
if(haystack[i] == needle) {
fprintf(stdout, "Found char '%c' in '%s' at position %d\n", needle, haystack, (i+1));
break;
}
}
return 0;
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 11 months ago.
Improve this question
For example, in a string, you'll be asked to find and print the index of a capital character:
char str[100] = "helloWorld";
the capital letter is found at index 5 so printf("Index of capital letter: Index %d") in which %d should be 5
How do you create a code to print the index?
Use strcspn():
Synopsis
1
#include <string.h>
size_t strcspn(const char *s1, const char *s2);
Description
2 The strcspn function computes the length of the maximum initial
segment of the string pointed to by s1 which consists entirely of
characters not from the string pointed to by s2.
Returns
3 The strcspn function returns the length of the segment.
For example:
// get the index of the first upper-case character
// return ( ssize_t ) -1 if there are none
ssize_t firstUpperCase( const char *str )
{
ssize_t index = strcspn( str, "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
if ( index >= strlen( str )
{
index = ( ssize_t ) -1;
}
return( index );
}
To locate the first uppercase letter in the string, you must use a loop:
#include <ctype.h>
#include <stdio.h>
int main() {
char str[100] = "helloWorld";
for (int i = 0; str[i] != '\0'; i++) {
if (isupper((unsigned char)str[i]) {
printf("Index of capital letter '%c': %d\n", str[i], i);
return 0;
}
}
printf("No capital letter\n");
return 1;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am having this issue, where I use a command-line interface.
I am trying to store the input in an array of strings. But When I run my code
I get a segmentation fault?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int num, char **str) {
int i;
char owner[20];
char *keys[5];
int j = 0;
for (i = 1; i < num; i++) {
if (i == 1) {
strcpy(owner, str[i]);
printf("%s", owner);
}
else {
keys[i] = malloc(10 * sizeof(char));
strcpy(keys[j], str[i]);
printf("%s", keys[j]);
j++;
}
}
}
In the else statement you should write
else {
keys[j] = malloc( strlen( str[i] ) + 1 );
strcpy(keys[j], str[i]);
printf("%s", keys[j]);
j++;
}
Also you should provide that the array owner is large enough to store the string str[1].
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm trying to make something that changes only one character in a string. As an example.
char str[10];
fgets(str,10,stdin); //input hello
// do something to change 'hello to he1lo'
All I can found is some functions change all of same letters.
You can modify by searching through a string then replace it with new char.
#include<stdio.h>
int replace(char *str,const char old_char,const char new_char,const int replace_char_count) {
int count_replaced_chars = 0;
for(int i = 0; str[i] != '\0';i++) {
if(str[i] == old_char) {
str[i] = new_char;
count_replaced_chars++;
if(count_replaced_chars == replace_char_count)
return 0;
}
}
return -1;
}
int main()
{
char str[10];
char new_char = '1';
char old_char = 'l';
int replace_char_count = 0;
printf("Enter the string in which chars to be replaced\n");
fflush(stdout);
fgets(str,10,stdin); //input hello
printf("Enter how many chars to replaced\n");
fflush(stdout);
scanf("%d",&replace_char_count);
if(!replace(str,old_char,new_char,replace_char_count))
printf("Successfully %d number of replaced char\n",replace_char_count);
else
printf("char not found\n");
printf("%s\n",str);
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Program to take the input and output the reverse of it:
#define MAX 1000
int readtext(char[],int); /*used to store text in array and
returns the size of the line*/
void reverse(char[]); /*used to reverse the text in the line and
returns 0*/
int main(void)
{
char text[MAX];
printf("Enter text, press Ctrl+d when done \n"); /*prompt user input*/
while((redtext(text, sizeof text)>0)) /*loop repeats until text size is >0*/
{
reverse(text);
printf("%s\n\n",text);
}
return 0;
}
int readtext(char a[],int len)
{
int letchar,i;
for(i=0;i<len-1 && (letchar=getchar())!=EOF && letchar!='\n';i++) /*for loop repeats until end of line*/
a[i]=letchar;
if(letchar=='\n') /*checks if letchar is \n. if true, changes it to null and returns i value*/
a[i++]=letchar;
a[i]='\0';
return i;
}
void reverse(char a[])
{
char t;
int x,y;
for(y=0;a[y]!='\0';y++) /*loop used to get the last element of the array*/
--y;
for(x=0;x<y;x++) /*loop used to reverse the array 'a'*/
{
t=a[x];
a[x]=a[y];
a[y]=t;
--y;
}
}
expected input/output:
happy birthday
yadhtrib yppah
I am getting this error message, but do not know what it means:
/tmp/ccA71SDX.o: In function `main':
1-19.c:(.text+0x63): undefined reference to `redtext'
collect2: ld returned 1 exit status
You have made a mistake in function call (redtext instead of readtext). However you can use my solution:
#include <stdio.h>
#include <strings.h>
#define MAXSTRLEN 256
void Reverse(char* str);
int main()
{
printf("Enter string below:\n");
char str[MAXSTRLEN];
fgets(str, MAXSTRLEN, stdin);
Reverse(str);
printf("Result:\n%s\n", str);
return 0;
}
void Reverse(char* str)
{
char tmp;
int length = strlen(str) - 1;
int i;
for(i = 0; i < length / 2; i++)
{
tmp = str[i];
str[i] = str[length - i - 1];
str[length - i - 1] = tmp;
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
Suppose I get an input such as q3 from the user. I need to take this as input and store q as a letter separately and 3 as a number. Please show me how to do this in c using code?
#include <stdio.h>
int main(int argc, char *argv[])
{
const char str[] = "q3";
char ch;
int num;
sscanf(str,"%c%d",&ch,&num);
printf("char: '%c' num: '%d'\n", ch, num);
return 0;
}
N.B. Watch the ampersands (&) in the sscanf(). Forgetting to put those in is a really common coding mistake.
#include <stdio.h>
#include <stdlib.h>
int main() {
char line[BUFSIZ];
char c;
int n;
printf("Enter: ");
fgets(line, sizeof line, stdin); // read a line of input
c = line[0]; // put first letter from line into c
n = atoi(line + 1); // interpret second letter onwards as an integer
printf("Letter: %c\n", c);
printf("Number: %d\n", n);
return 0;
}