C Tutorial
This program is used to find the character at index in a string and string is defined using pointers.
#include<stdio.h> void main() { char *str = "testing codingpointer.com"; int index; char ch; printf("Enter index: "); scanf("%d", &index); ch = str[index]; printf("\ncharacter at index %d: %c\n", index, ch); }
Output:
$ cc string-index.c $ ./a.out Enter index: 5 character at index 5: n
we could also specify the index and string pointer variable reverse order like below which will also produce the same output.
#include<stdio.h> void main() { char *str = "testing codingpointer.com"; int index; char ch; printf("Enter index: "); scanf("%d", &index); ch = index[str]; printf("\ncharacter at index %d: %c\n", index, ch); }
Output:
$ cc string-index.c $ ./a.out Enter index: 8 character at index 8: c
This program is used to find the character at index in a string and string is defined using array.
#include<stdio.h> void main() { char str[] = "testing codingpointer.com"; int index; char ch; printf("Enter index: "); scanf("%d", &index); ch = str[index]; printf("\ncharacter at index %d: %c\n", index, ch); }
Output:
$ cc string-index.c $ ./a.out Enter index: 6 character at index 5: gwe could also specify the index and string array variable reverse order like below which will also produce the same output.
#include<stdio.h> void main() { char str[] = "testing codingpointer.com"; int index; char ch; printf("Enter index: "); scanf("%d", &index); ch = index[str]; printf("\ncharacter at index %d: %c\n", index, ch);
Output:
$ cc string-index.c $ ./a.out Enter index: 9 character at index 8: o
This c program is used to find the character starting index using one line in a string.
#include<stdio.h> void main() { char *str = "testing codingpointer.com"; int index; char ch; printf("Enter character: "); scanf("%c", &ch); for(index=0; *str++!=ch; index++); printf("\ncharacter '%c' starting index: %d\n", ch, index); }
Output:
$ cc string-index.c $ ./a.out Enter character: n character 'n' starting index: 5
#include<stdio.h> void main() { char str[] = "testing codingpointer.com"; int index=0; char ch; printf("Enter character: "); scanf("%c", &ch); while(*str++!=ch) { index++; } printf("\ncharacter '%c' starting index: %d\n", ch, index); }
Output:
$ cc string-index.c $ ./a.out Enter character: t character 'n' starting index: 0
#include<stdio.h> void main() { char str[] = "testing codingpointer.com"; int index = sizeof(str)/sizeof(str[0]); char ch; printf("Enter character: "); scanf("%c", &ch); while(*(str+(index-1))!=ch) { index--; } printf("\ncharacter '%c' ending index: %d\n", ch, index+1); }
Output:
$ cc string-index.c $ ./a.out Enter character: r character 'r' ending index: 20
#include<stdio.h> void main() { char str[] = "testing codingpointer.com"; int index = (sizeof(str)/sizeof(str[0]))-1; char ch; printf("Enter character: "); scanf("%c", &ch); while(*(str+(index--))!=ch); printf("\ncharacter '%c' ending index: %d\n", ch, index+1); }
Output:
$ cc string-index.c $ ./a.out Enter character: r character 'r' ending index: 20
#include<stdio.h> #include<string.h> void main() { char *str = "testing codingpointer.com"; int index = strlen(str); char ch; printf("Enter character: "); scanf("%c", &ch); while(*(str+(index--))!=ch); printf("\ncharacter '%c' ending index: %d\n", ch, index+1); }
Output:
$ cc string-index.c $ ./a.out Enter character: r character 'r' ending index: 20
C Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us
| Report website issues in Github
| Facebook page
| Google+ page