QUESTION-6 Copy String Manually Without Using strcpy().
Answer-
int main() { char s1[100], s2[100], i; printf("Enter string s1: "); scanf("%s",s1); for(i = 0; s1[i] != '\0'; ++i) { s2[i] = s1[i]; } s2[i] = '\0'; printf("String s2: %s", s2); return 0; }
QUESTION-7 Remove Characters in String Except Alphabets
Answer-
int main() { char line[150]; int i, j; printf("Enter a string: "); gets(line); for(i = 0; line[i] != '\0'; ++i) { while (!( (line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z') || line[i] == '\0') ) { for(j = i; line[j] != '\0'; ++j) { line[j] = line[j+1]; } line[j] = '\0'; } } printf("Output String: "); puts(line); return 0; }
QUESTION-8 Check if a given String is Palindrome
Answer-
void main() { char string[25], reverse_string[25] = {'\0'}; int i, length = 0, flag = 0; fflush(stdin); printf("Enter a string \n"); gets(string); for (i = 0; string[i] != '\0'; i++) { length++; } for (i = length - 1; i >= 0; i--) { reverse_string[length - i - 1] = string[i]; } for (i = 0; i < length; i++) { if (reverse_string[i] == string[i]) flag = 1; else flag = 0; } if (flag == 1) printf("%s is a palindrome \n", string); else printf("%s is not a palindrome \n", string); }
QUESTION-9 Replace Lowercase Characters by Uppercase & Vice-Versa
Answer-
void main() { char sentence[100]; int count, ch, i; printf("Enter a sentence \n"); for (i = 0;(sentence[i] = getchar()) != '\n'; i++) { ; } sentence[i] = '\0'; /* shows the number of chars accepted in a sentence */ count = i; printf("The given sentence is : %s", sentence); printf("\n Case changed sentence is: "); for (i = 0; i < count; i++) { ch = islower(sentence[i])? toupper(sentence[i]) : tolower(sentence[i]); putchar(ch); } }
QUESTION-10 Remove given Word from a String
Answer-
void main() { int i, j = 0, k = 0, count = 0; char str[100], key[20]; char str1[10][20]; printf("enter string:"); scanf("%[^\n]s",str); for (i = 0; str[i]!= '\0'; i++) { if (str[i]==' ') { str1[k][j] = '\0'; k++; j = 0; } else { str1[k][j] = str[i]; j++; } } str1[k][j] = '\0'; printf("enter key:"); scanf("%s", key); for (i = 0;i < k + 1; i++) { if (strcmp(str1[i], key) == 0) { for (j = i; j < k + 1; j++) strcpy(str1[j], str1[j + 1]); k--; } } for (i = 0;i < k + 1; i++) { printf("%s ", str1[i]); } } }