String online
#include <stdio.h>
#include <string.h>
int main() {
char str[1001], word[1001], max_word[1001];
int max_len = 0, word_len = 0, i = 0;
fgets(str, 1001, stdin);
if (str[strlen(str)-1] == '\n') str[strlen(str)-1] = '\0';
while (str[i]) {
if (str[i] == ' ') {
word[word_len] = '\0';
if (word_len > max_len) {
max_len = word_len;
strcpy(max_word, word);
}
word_len = 0;
} else {
word[word_len++] = str[i];
}
i++;
}
word[word_len] = '\0';
if (word_len > max_len) strcpy(max_word, word);
printf("The largest word is '%s'\n", max_word);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int hasConsonantOrDigit(char str[]) {
int len = strlen(str);
for (int i = 0; i < len; i++) {
char ch = str[i];
if(str[i]>='0' && str[i]<='9')
{return 0;}
else if(str[i]=='a' ||str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u' )
{return 0;}
}
return 1;
}
int main() {
char str[200];
char substr[200];
char ans[300]="";
printf("Enter string: ");
scanf("%s", str);
int len = strlen(str);
int max=-1;
for (int i = 0; i < len; i++) {
for (int j = i; j < len; j++) {
int l = 0;
for (int k = i; k <= j; k++) {
substr[l++] = str[k];
}
substr[l] = '\0'; // Null terminate!
if (hasConsonantOrDigit(substr)) {
int m= strlen(substr);
if(m>max){max=m; strcpy(ans, substr);}
}
}
}
printf("%s", ans);
return 0;
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#define MAX_LEN 1000
int main()
{
char s1[MAX_LEN]="I scream, you scream, we all scream for ice cream!";
char s2[50]="scream";
char s3[50]= "shout";
char result[MAX_LEN];
int count=0;
int i=0;
int j=0;
while(s1[i]!='\0')
{
if(strncmp(&s1[i], s2, strlen(s2) )==0)
{
strcpy(&result[j], s3);
i+=strlen(s2);
j+= strlen(s3);
count++;
}
else
{result[j++]= s1[i++];
}
}
result[j]=0;
printf("Ans: %s", result);
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#define MAX_LEN 1000
void rotation(char str1[],int n)
{
//abcde bcdea
char temp[100];
int len = strlen(str1);
for(int i=0; i<n; i++)
{temp[i]= str1[i];
}
for(int i=0;i<len-n; i++)
{
str1[i]= str1[i+n];
}
int s= n-1;
for(int i=len-1; i>=len-n;i--)
{
str1[i]= temp[s--];
}
for(int i=0; i<len ; i++)
{
printf("%c", str1[i]);
}
}
void rrotation(char str[], int n)
{ char temp[100];
int k=0;
int len = strlen(str);
for(int i = len-1-n; i<len ; i++)
{
temp[k++]= str[i];
}
for(int i=0; i<n; i++)
{
str[i]= temp[i];
}
for(int i=n; i<len; i++)
{
str[i+n]= str[i];
}
printf("%s", str);
}
int main()
{
char str[100];
scanf("%s", str);
rrotation(str, 2);
}
#include <stdio.h>
#include <ctype.h> // For isdigit
#define MAX_LEN 100
void expand(char str[]) {
char result[MAX_LEN] = "";
int k = 0; // index for result
for (int i = 0; str[i] != '\0'; i++) {
if (isalpha(str[i]) && isdigit(str[i + 1])) {
char ch = str[i];
int count = str[i + 1] - '0'; // Convert char digit to int
for (int j = 0; j < count; j++) {
result[k++] = ch;
}
i++; // Skip the digit
}
}
result[k] = '\0';
printf("Expanded: %s\n", result);
}
int main() {
char str[MAX_LEN];
printf("Enter encoded string: ");
scanf("%s", str);
expand(str);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void print_longest_word(char str[]) {
int max_len = 0, curr_len = 0, start = 0, max_start = 0;
int len = strlen(str);
// Remove trailing newline if present
if (len > 0 && str[len - 1] == '\n') {
str[len - 1] = '\0';
len--;
}
for (int i = 0; i <= len; i++) {
if (str[i] != ' ' && str[i] != '\0') {
if (curr_len == 0) start = i;
curr_len++;
} else {
if (curr_len > max_len) {
max_len = curr_len;
max_start = start;
}
curr_len = 0;
}
}
// Print the longest word
for (int i = 0; i < max_len; i++) {
putchar(str[max_start + i]);
}
putchar('\n');
}
int main() {
char str[100];
fgets(str, 100, stdin);
print_longest_word(str);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX_LINES 100
#define MAX_LEN 1000
void trim(char src[], char dest[]) {
int start = 0;
int end = strlen(src) - 1;
// Skip leading spaces
while (isspace(src[start])) {
start++;
}
// Skip trailing spaces
while (end >= start && isspace(src[end])) {
end--;
}
// Copy trimmed part
int len = end - start + 1;
if (len > 0) {
strncpy(dest, src + start, len);
dest[len] = '\0';
} else {
dest[0] = '\0';
}
}
int main() {
int n;
char lines[MAX_LINES][MAX_LEN];
char trimmed[MAX_LINES][MAX_LEN];
int max_len = -1;
int max_index = -1;
printf("Enter number of lines: ");
scanf("%d", &n);
getchar(); // Remove leftover newline
for (int i = 0; i < n; i++) {
fgets(lines[i], MAX_LEN, stdin);
// Remove newline if exists
int len = strlen(lines[i]);
if (lines[i][len - 1] == '\n') {
lines[i][len - 1] = '\0';
}
trim(lines[i], trimmed[i]);
printf("%s\n", trimmed[i]);
int curr_len = strlen(trimmed[i]);
if (curr_len > max_len) {
max_len = curr_len;
max_index = i;
}
}
printf("Longest line: %s\n", trimmed[max_index]);
printf("Length: %d\n", max_len);
return 0;
}
#include <stdio.h>
#include <string.h>
void decipher(char *str, int key) {
int i = 0;
while (str[i] != '\0') {
if (str[i] >= 'A' && str[i] <= 'Z') {
// Decipher capital letters
str[i] = ((str[i] - 'A' - key + 26) % 26) + 'A';
} else if (str[i] >= 'a' && str[i] <= 'z') {
// Replace lowercase with space
str[i] = ' ';
}
i++;
}
}
int main() {
int key;
char message[1000];
// Read input
scanf("%d", &key);
scanf("%s", message);
// Perform decipher
decipher(message, key);
// Print result
printf("%s\n", message);
return 0;
}
Comments
Post a Comment