Bir Cümle İçindeki En Kısa Ve En Uzun Kelimeyi Bulma Ve Kelimeleri Ayrı Ayrı Yazdırma Fonksiyonları

mustaphos

MB Üyesi
Kayıt
14 Eylül 2015
Mesajlar
35
Tepkiler
8
Yaş
28
Meslek
Öğrenci
Üniv
Anadolu University
Merhaba.
Program bir cümle ve değer ister.
1 ise en uzun kelimeyi bulur.
2 ise en kısa kelimeyi bulur.
3 ise kelimeleri teker teker yazdırır.
4 ise programdan çıkar.
İyi Çalışmalar.

Kod:
#include <stdio.h>
#include <stdlib.h>

char cumle[41];// Define a 1-D string array
char kelime[41][41]={' '};// Define a 2-D array for which words and which characters

int secenek;// Options for call different functions
int uzunluk[41]={0};// Length of each words
int sayac;// Counter to obtain words
int kacincikelime;// Which words
int harfler;// Charecters
int yazdir1;// For loop counter to print words
int yazdir2;// For loop counter to print charecters
int uzunlukbul;// Find word's length
int enuzun;// Longest counter
int enkisa;// Shortest counter
int enuzunkelime=0;// Longest word
int enkisakelime=0;// Shortest word
int enuzunkacinci=0;// Longest is x'th word
int enkisakacinci=0;// Shortest is y'th word

void string_process();// Preprocessor function
void longest_word();// Preprocessor function
void shortest_word();// Preprocessor function
void seperate_words();// Preprocessor function

void main()
{
printf("Enter a string with maximum 40 charecters\n");

printf(">>> ");
gets(cumle);// Get the string

printf("Type [ 1 ] and press [ ENTER ] to find the longest word of this string \n");
printf("Type [ 2 ] and press [ ENTER ] to find the shortest word of this string \n");
printf("Type [ 3 ] and press [ ENTER ] to write the words seperately \n");
printf("Type [ 4 ] and press [ ENTER ] to exit the program \n");

printf(">>> ");
scanf("%d",&secenek);// Which option the user wants to observe
string_process(cumle);// Process the string to find spaces,end,words,characters,lengths

switch(secenek)// Switch the entered value
{

case 1:// If it is one call the longest word function
{
longest_word(cumle);
break;
}

case 2:// If it is two call the shortest word function
{
shortest_word(cumle);
break;
}

case 3:// If it is three call the seperate words function
{
seperate_words(cumle);
break;
}

case 4:// If it is four exit the program
{
printf("Exiting the program\n");
exit(0);
}
}
}

void string_process()
{
kacincikelime=1;// Define first word
harfler=1;// Define first charecter
uzunlukbul=0;// Define lentgh

for(sayac=0;sayac<41;sayac++)
{
if (cumle[sayac]==' '&&cumle[sayac+1]==' '||cumle[sayac]==' '&&cumle[sayac+1]=='\0')// If there is two spaces in a row or space and null in a row continiue with doing nothing
{
continue;
}

kelime[kacincikelime][harfler]=cumle[sayac];// Obtain the words other and their characters
harfler++;// Find the charecters
uzunlukbul++;// Increase the words' lengths
uzunluk[kacincikelime]=uzunlukbul;// Define each words which has how much charecters

if (cumle[sayac]==' ')// If there is space next to word
{
kacincikelime++;// Increase word number
harfler=1;// Define word number 1 to count again
uzunlukbul=0;// Define word's length 0 again
}

if (cumle[sayac]=='\0')// Define a space end of string to count the last word
{
cumle[sayac]=' ';
break;
}
}
}

void longest_word()
{
enuzun=0;// Define length 0

for(yazdir1=1;yazdir1<=kacincikelime;yazdir1++)
{
if(enuzun<uzunluk[yazdir1]-1)// If words's length less than enuzun variable
{
enuzun=uzunluk[yazdir1]-1;// New enuzun variable value
enuzunkelime=yazdir1;// Number of longest word
}
}

printf("%02d. word [ ",enuzunkelime);// Print which word

for(yazdir1=1;yazdir1<=enuzun;yazdir1++)
{
printf("%c",kelime[enuzunkelime][yazdir1]);// Print characters of longest word
}

printf(" ] is the longest word with length of [ %d ] \n",enuzun);// Print length of longest word
getch();
}

void shortest_word()
{
enkisa=41;// Define length 41

for(yazdir1=1;yazdir1<=kacincikelime;yazdir1++)
{
if(enkisa>uzunluk[yazdir1]-1) // If words's length greater than enkisa variable
{
enkisa=uzunluk[yazdir1]-1;// New enuzun variable value
enkisakelime=yazdir1;// Number of shortest word
}
}

printf("%02d. word [ ",enkisakelime);// Print which word

for(yazdir1=1;yazdir1<=enkisa;yazdir1++)
{
printf("%c",kelime[enkisakelime][yazdir1]);// Print charecters of shortest word
}

printf(" ] is the shortest word with length of [ %d ] \n",enkisa);// Print length of shortest word

getch();
}

void seperate_words()
{
for(yazdir1=1;yazdir1<=kacincikelime;yazdir1++)
{
if (uzunluk[yazdir1]<=0)// If statement to do not print spaces as a word
{
break;
}

printf("%02d. word : ",yazdir1);// Print which word

for(yazdir2=1;yazdir2<=uzunluk[yazdir1]-1;yazdir2++)
{
printf("%c",kelime[yazdir1][yazdir2]);// Print charecters of words
}

printf("\n");// Go to next line
}
getch();
}
 
Son düzenleme yönetici tarafından yapıldı:

selcukyerliturk

MB Üyesi
Kayıt
7 Aralık 2015
Mesajlar
9
Tepkiler
1
Yaş
29
Üniv
Kocaeli Üniversitesi
yazdığında kodu tam olarak çözemedim ama sadece ascı karakterlerden boşlukları sorgulatıp arasındaki karakterleri saydırsan daha kısa olmaz mı
 
Konu sahibi
Konu sahibi
mustaphos

mustaphos

MB Üyesi
Kayıt
14 Eylül 2015
Mesajlar
35
Tepkiler
8
Yaş
28
Meslek
Öğrenci
Üniv
Anadolu University
yazdığında kodu tam olarak çözemedim ama sadece ascı karakterlerden boşlukları sorgulatıp arasındaki karakterleri saydırsan daha kısa olmaz mı
ya kanka bunu c programlama laboratuvar dersinde hoca bu şekilde istemişti. o yüzden bu şekilde yazdım. Şu an ben de çözemedim nasıl yapmışım :D :D
 
Yukarı Alt