簡體   English   中英

打印C中的最后一個字符串名稱

[英]Print the last string name in C

我正在嘗試學習C,在這里我有一個程序,我們必須在其中將用戶的輸入作為n個os字符串,進行比較並按字母順序排列。 在按字母順序排列它們之后,我只需要打印按該順序出現的姓氏即可。

這是上述問題的代碼:

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

int main()
{
     int i,j,m,n,len;
     char a[50][50],temp[100];
     char last ;

     printf("Enter the number of elements you wish to order : ");
     scanf("%d",&m);
     printf("\nEnter the names :\n");

     for (i=0;i<m;i++){
       scanf("%s",a[i]);
      }

      for (i=0;i<m;i++){
       for (j=i+1;j<m+1;j++) {
        if (strcmp(a[i],a[j])>0) {
           strcpy(temp,a[i]);
           strcpy(a[i],a[j]);
           strcpy(a[j],temp);
          }
         }
        }

    printf("\n\nSorted strings are : ");
     for (i=0;i<m+1;i++){
       printf("%s \n",a[i]);
      }
    return 0;
}


答案是這樣的:

輸入您要訂購的元素數量:4

輸入名稱:領土狀態你好

排序的字符串是:S $ ??? 你好像國家領土

我的問題是,為什么我會收到“ S $ ???”? 我只希望最后一個字“不要打印出所有地區的名稱”。

誰能告訴我我要去哪里錯了? 這將是一個很大的幫助。

謝謝Tanya

您正在按索引[0..m]排序m+1字符串。 但是您只輸入m字符串。

您的索引絕對不能超過m-1

\\檢查此代碼

在您的代碼中輸入為4

然后
一個[0] =領土
一個[1] =狀態
一個[2] =你好
[3] =像

但是您的代碼最多運行3次上循環,然后i = 3

在下一個循環中j = i + 1然后j = 4

但是a [4] =不存在

那就是為什么“ S $ ???” 發生

解:

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

int main() {
  int i, j, m, n, len;
  char a[50][50], temp[100];
  char last;
  printf("Enter the number of elements you wish to order : ");
  scanf("%d", &m);

  printf("\nEnter the names :\n");
  for (i = 0; i < m; i++) {
    scanf("%s", a[i]);
  }

  for (i = 0; i < m - 1; i++) {   // m - 1  enough maximum i = 2;
    for (j = i + 1; j < m; j++) { // j maximum j = i + 1   j = 3
      if (strcmp(a[i], a[j]) > 0) {
        strcpy(temp, a[i]);
        strcpy(a[i], a[j]);
        strcpy(a[j], temp);
      }
    }
  }

  printf("\n\nSorted strings are : ");  // print all words in sorted order
  for (i = 0; i < m; i++) {
    printf("%s \n", a[i]);
  }
  printf("Last Word:\n");
  printf("%s\n", a[m - 1]); // a[3] contains last name 
  return 0;
}

這是我的程序,在Turbo C ++中對我有用

    #include<conio.h>
    #include<iosream.h>
    #include<ctype.h>
    #include<string.h>
    #include<stdio.h>
    #include<stdlib.h>

    void main()
    {
    clrscr();
    char name[100];
    int c=0;

// to take the name including spaces
gets(name);

//this line is to print the first character of the name
cout<<name[0];

//this loop is to print the fist character which is there after every space
for(int l=0;name[l]!='\0';l++)
{
      if(name[l]==' ')
       { 
         cout<<"."<<name[l+1];
//l+1 is used because l is the space and l+1 is the character that we need
       }
}


//this loop is to find out the last space in the entire sting

for(int i=0;name[i]!='\0';i++)
{
     if(name[l+1]==NULL)
     {

      //here we fond the last space in the string
                for(int j=i;name[j]!=' ';j--)
                {
                c=j+1;
                }
// c=j+1 is used bacause we have already taken the first letter of the that word
//no need of taking it again
     }
}
//this loop starts from the last space and the position(of space) is stored in k
for(int k=c;name[k]!='\0';k++)
{
cout<<name[k];
}

    getch();
}

輸出:

安特特·蘭揚·舒克拉
arshukla

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM