Finding first/last occurrence of a character in a string
without using strchr( ) /strrchr( ) function.
Answer Posted / daniel
#include <stdio.h>
#include <string.h>
int main()
{
char *string = "This is a simple string";
char x = 's';
int i;
int focc, locc;
for (i=0;i<strlen(string);i++){
if (string[i] == x){
focc = i;
break;
}
}
for (i=0;i<strlen(string);i++){
if (string[i] == x)
locc = i;
}
printf ("First occurrence %d, last occurrence %d\n", focc, locc);
return 0;
}
| Is This Answer Correct ? | 8 Yes | 4 No |
Post New Answer View All Answers
Is c is a low level language?
write an interactive C program that will encode or decode a line of text.To encode a line of text,proceed as follows. 1.convert each character,including blank spaces,to its ASCII equivalent. 2.Generate a positive random integer.add this integer to the ASCII equivalent of each character.The same random integer will be used for the entire line of text. 3.Suppose that N1 represents the lowest permissible value in the ASCII code,and N2 represents the highest permissible value.If the number obtained in step 2 above(i.e.,the original ASCII equivalent plus the random integer)exceeds N2,then subtract the largest possible multiple of N2 from this number,and add the remainder to N1.Hence the encoded number will always fall between N1 and N2,and will therefore always represent some ASCII character. 4.Dislay the characters that correspond to the encoded ASCII values. The procedure is reversed when decoding a line of text.Be certain,however,that the same random number is used in decodingas was used in encoding.
Why we use conio h in c?
How can I implement a delay, or time a users response, with sub-second resolution?
What are header files and what are its uses in C programming?
What is a pointer value and address in c?
What's a good way to check for "close enough" floating-point equality?
Explain the difference between structs and unions in c?
What does void main () mean?
When should the const modifier be used?
What is the difference between if else and switchstatement
What is the package for freshers(Non IIT) in amazon(hyderabad). And what is the same for those who are a contract employee.
What happens if a header file is included twice?
Can a void pointer point to a function?
What is meant by 'bit masking'?