Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- void subString(char str[], int len, int indx)
- {
- int i;
- char temp[1001];
- int j = 0;
- int sz = strlen(str);
- if(indx > sz) {
- printf("Invalid input!\n");
- return 0;
- }
- for(i=indx; i<=indx+len; i++){
- temp[j++] = str[i];
- if(i==sz) break;
- }
- temp[j] = '\0';
- printf("%s\n", temp);
- }
- int findSubString(char str[], char substr[])
- {
- int szS = strlen(str);
- int szSb = strlen(substr);
- int i=0, j=0;
- int index = -1;
- int cnt = 0;
- for( ; i<szS; ) {
- cnt = 0;
- j = 0;
- if(str[i]==substr[j]) {
- j = 0;
- while(str[i]==substr[j] && i<szS) {
- i++;
- j++;
- cnt++;
- }
- if(cnt==szSb) {
- index = i-szSb;
- break;
- }
- }
- else i++;
- }
- return index;
- }
- void insert(char str[], char newStr[], int indx)
- {
- int szS = strlen(str);
- int szN = strlen(newStr);
- char temp[szS+szN+5];
- int i = 0;
- for(i = 0; i < indx; i++) {
- temp[i] = str[i];
- }
- int j = 0;
- for(i=indx; i<indx+szN; i++) {
- temp[i] = newStr[j++];
- }
- j = indx;
- for(i=indx+szN; i<szS+szN; i++) {
- temp[i] = str[j++];
- }
- temp[i] = '\0';
- printf("%s\n", temp);
- }
- void deleteStr(char str[], int len, int indx)
- {
- char temp[1001];
- int i;
- int sz = strlen(str);
- for(i=0; i<indx; i++) {
- temp[i] = str[i];
- }
- int j = i;
- for(i=indx+len; i<sz; i++) {
- temp[j++] = str[i];
- }
- temp[j] = '\0';
- printf("%s\n", temp);
- }
- void replace(char str[], char strTR[], char strR[])
- {
- char temp[2002];
- int indx = findSubString(str, strR);
- if(indx==-1) {
- printf("String to be replace is not found!\n");
- return;
- }
- int len = strlen(str);
- int lenR = strlen(strR);
- int lenTR = strlen(strTR);
- int i=0;
- for(i=0; i<indx; i++) {
- temp[i] = str[i];
- }
- int j=i;
- for(i=0; i<lenTR; i++) {
- temp[j++] = strTR[i];
- }
- for(i=indx+lenR; i<len; i++) {
- temp[j++] = str[i];
- }
- temp[j] = '\0';
- printf("%s", temp);
- }
- int main()
- {
- char str[1001];
- int len = 0, indx = 0;
- //Assignment-1
- printf("\tAssignment-1:\n\n");
- printf("Enter a string(within 1000 characters): ");
- gets(str);
- printf("Enter the index(index staring from 0): ");
- scanf("%d", &indx);
- printf("Enter the length of the sub-string: ");
- scanf("%d", &len);
- subString(str, len, indx);
- // Assignment-2
- getchar();
- printf("\n\tAssignment-2:\n");
- printf("Enter the sub-string you are looking for: ");
- char substr[1001];
- gets(substr);
- int x = findSubString(str, substr);
- if(x==-1) printf("Sub-string not found\n");
- else printf("Sub-string found at index %d", x);
- // Assignment-3
- printf("\n\tAssignment-3:\n");
- char stringToInsert[1001];
- printf("Enter the string you want to insert: ");
- gets(stringToInsert);
- printf("Enter the index you want to insert the new string: ");
- scanf("%d", &indx);
- insert(str, stringToInsert, indx);
- // Assignment-4
- printf("\n\tAssignment-4:\n");
- printf("Enter the index from which you want to delete: ");
- scanf("%d", &indx);
- printf("Enter the size of sub-string you want to delete: ");
- scanf("%d", &len);
- deleteStr(str, len, indx);
- // Assignment-5
- printf("\n\tAssignment-5:\n");
- getchar();
- char strR[1001], strTR[1001];
- printf("Enter the sub-string you want to replace with: ");
- gets(strTR);
- printf("Enter the string you want to replace: ");
- gets(strR);
- replace(str, strTR, strR);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment