0

شمارش تعداد حروف یک متن

 
rezahashemian1374
rezahashemian1374
کاربر برنزی
تاریخ عضویت : مهر 1391 
تعداد پست ها : 254
محل سکونت : تهران

شمارش تعداد حروف یک متن

دوستان در این مطلب هم سورس برنامه شمارش تعداد حروف یک متن رو برای شما قرار دادم.امیدوارم به دردتون بخوره

#include <stdio.h>
#include <string.h>
#include <malloc.h>

/* define data structure for linked list */
typedef struct tagNode
{
  char ch;
  int freq;
  struct tagNode *next;
} Node;

/* dynamically allocate memory for a new node */
Node *makeNewNode()
{
  Node *newNode;

  newNode = (Node *) malloc(sizeof(Node));
  newNode -> next = NULL;

  return newNode;
}

/* print the list of characters and the number of times
   each character occurs */
void printList(Node *head)
{
  Node *currNode;

  currNode = head -> next;

  while(currNode != NULL)
  {
    printf("\n%c %3d", currNode -> ch, currNode -> freq);
    currNode = currNode -> next;
  }
}

/* if character ch is present in the list
     increase its count       
   else
     add character ch to the list
*/
void addToList(Node *head, char ch)
{
  Node *currNode, *prevNode;

  prevNode = head;
  currNode = head -> next;

  while(currNode != NULL)
  {
    if(currNode -> ch == ch)
    {
      currNode -> freq ++;
      return;
    }

    prevNode = currNode;
    currNode = currNode -> next;
  }

  currNode = makeNewNode();
  if(currNode == NULL)
  {
    perror("malloc");
    return;
  }

  currNode -> ch = ch;
  currNode -> freq = 1;
  prevNode -> next = currNode;
}

void count()
{
  int i, lineLen;
  Node *listHead;
  char ch, line[81];

  /* prompt user to enter a line of text */
  printf("\nEnter a line of text\n");
  fgets(line, 81, stdin);

  lineLen = strlen(line);

  if(line[lineLen - 1] == '\n')
  {
    line[lineLen] = 0;
    lineLen--;
  }

  /* create the list's header */
  listHead = makeNewNode();
  if(listHead == NULL)
  {
    perror("malloc");
    return;
  }
 
  /* parse the inputted line */
  for(i = 0; i < lineLen; i++)
  {
    ch = line[i];

    if(ch == '.')
      break;

    if(ch >= 'a' && ch <= 'z' ||
       ch >= 'A' && ch <= 'Z')
    {
      addToList(listHead, ch);
    }
  }
 
  /* print results */
  printf("\nLetter Occurs");
  printList(listHead);
}

int main()
{
  count();
  printf("\n");
  return(0);
}

Seyyed.Reza.Hashemian@Gmail.Com

دوستانی که سوالی دارند یا مایل به تماس هستند می توانند از اطلاعات بالا استفاده نمایند.

اگر هم تایپکی زدید و احتیاج به پاسخگویی سریع داشتید اطلاع دهید

پنج شنبه 3 اسفند 1391  1:02 PM
تشکرات از این پست
دسترسی سریع به انجمن ها